Hi guys !
A little problem. I have a parser (not mine) for a configuration file (smb.conf if someone knows). this file as from the format:
[name1]
key1 = value1
key2 = value2
...
[name2]
key1 = value1
key2 = value2
...
# and so on
The problem is when I have something like this:
[name1]
key1 = value1
key2 = value2
...
[name2]
[name3]
key1 = value1
key2 = value2
...
as you can see
name2 has no values, this thing is ok (default values are taken), but the parser I have doesn't return it, here is it's code:
sub smbconf_parse {
my $smbconf = shift;
my %smbconf;
my $share = '';
if (! open(SMB, $smbconf)) {
warn "Couldn't read smbconf file $smbconf\n";
warn "$!\n";
return 0;
}
while (<SMB>) {
s/^\s+//g;
s/\s+$//g;
next if (/^$/);
next if (/^\#/);
next if (/^\;/);
if (/^\[(.*)\]/) {
$share = $1; #######
} else {
my ($key, $value) = (/^(.*) ?\= (\S.*)/);
$key =~ s/\s+$//;
if ($value =~ /^\"(.*)\"$/) {
$smbconf{$share}{$key} = $1;
} elsif ($value =~ /\,/) {
my @value = split(/\,/, $value);
$smbconf{$share}{$key} = [ @value ];
} else {
$smbconf{$share}{$key} = $value;
}
}
}
close SMB;
return \%smbconf;
}
I can see the problem, no hash entry is opened if it didn't find any key=value pairs, so I tried adding something like
$smbconf{$share} = ''; # just to open the entry
right after the line with
$share = $1;, but that seemed to mess the whole parsing.
anyone has an idea ?
Hotshot
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.