Hi Monks, I had some
great help the other day from jonadab and others and would like a little more on adapting this code...
It works great for parsing a file, but sometimes I have already read the file into a $buffer and need to do the same processing.
If there is a way to easily convert the neat "local $/" file-handle trick, I don't see it. Do I need to somehow keep track of the character count in the $buffer?
The code parses k=v and heredoc config files
while (<CONFIG>) {
if (/^\s*#/) { # ignore comment line
} elsif (/^\s*$/) { # ignore blank line
} elsif (/(\w+)\s*=\s*[<]{2}(\w+)/) { # heredoc
(my $name, local $/) = ($1, "\n$2"); # ++ysth
$config{$name} = <CONFIG>;
chomp $config{$name}; # as etcshadow points out.
} elsif (/(\w+)\s*=\s*(.*?)\s*$/) { # regular pair
$config{$1}=$2;
} else {
warn "Ptooey: Could not parse config line: $_\n";
}
}