Well if you code up an alternate way to parse this, then I'd be interested in seeing a benchmark comparison. I think my solution was pretty simple, efficient, and correct.
My suggestion would be to use a templating module (probably Template Toolkit based on what others say about these things). But if that isn't acceptable for whatever reasons, I still stand by my suggestion as quite reasonable.
I don't see how riping out brackets would help much since there will probably be plenty of brackets that aren't delimiters both inside and outside of the delimited blocks.
Thinking of how I'd catch all unmatched delimiters, I'd probably do this:
my %start= qw( [( )] [| |] [{ }] );
my %end= reverse %start;
my $expect= ""; # Closing delimiter we expect, if any.
my $code;
for my $chunk ( split m#( \[ [(|{] | [)|}] \] )#x, $template ) {
if( "" eq $expect ) {
if( $start{$chunk} ) {
$expect= $start{$chunk};
$code= "";
} else {
warn "Unmatched $chunk\n" if $end{$chunk};
print $chunk;
}
} else {
if( $chunk eq $expect ) {
print expand( $code );
$expect= "";
} else {
$code .= $chunk;
warn "Found $chunk inside $end{$expect} $expect block\n"
if $start{$chunk} || $end{$chunk};
}
}
}
-
tye
(but my friends call me "Tye") |