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.