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";
}
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.