in reply to (tedv)Re2: Template system and substitution
in thread Template system and substitution

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")

Replies are listed 'Best First'.
(tye)Re4: Template system and substitution
by tye (Sage) on Nov 17, 2000 at 10:35 UTC

    ...and I just realized one minor way in which this is inferior to my original solution.

    This solution always interprets [|] as a starting delimiter, never as an ending delimiter. But given the unlikelyhood of needing to embed code that ends in [ and the (moderate) difficulty in "fixing" this solution in this respect, I'd just document this as a limitation.

            - tye (but my friends call me "Tye")