Given that some entire config file has been loaded into $buffer, it would seem easiest to split that into lines, and then behave pretty much the same way as reading from a file, except that "local $/" is of no use in this case. The following is untested:
@lines = split /\n/, $buffer;
my ( $name, $end ) = ( '', '' );
for ( @lines ) {
next if (/^\s*#/ or /^\s*$/ ); # skip comments, blank lines
if ( $name ) {
if ( /^$end$/ ) {
chomp $config{$name}; #remove trailing "\n"
$name = '';
} else {
$config{$name} .= "$_\n";
}
}
elsif ( /(\w+)\s*=\s*(.*?)\s*$/ ) { # regular pair
$config{$1} = $2;
}
elsif ( /(\w+)\s*=\s*<<(\w+)/ ) { # heredoc
( $name, $end ) = ( $1, $2 );
}
else {
warn "Ptooey: Could not parse config line: $_\n";
}
}
It's a little grotty, in the sense that you have to put the "within a HERE doc" behavior first in the "if...elsif..." series, because who knows whether/when the contents of a here-doc might trigger a false-alarm match on one of the other conditions. Also, as written above, there's nothing to warn about a here-doc that is not terminated at the last line in $buffer -- but that should be easy to figure out.
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.