Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: addn'l help with parsing here doc

by graff (Chancellor)
on Oct 16, 2003 at 23:29 UTC ( [id://299930]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


in reply to addn'l help with parsing here doc

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://299930]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.