Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

(tedv)Re2: Template system and substitution

by tedv (Pilgrim)
on Nov 17, 2000 at 04:12 UTC ( #42127=note: print w/replies, xml ) Need Help??

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

Well understandably the paranethesis matching of this is extremely difficult. It's my intuition that regex isn't the right solution-- reformulating the problem is. :) At the very least, you could just rip out all []s and get back to single character pairing with nesting. What strikes me as odd is why the data would even be in that format to begin with... Surely you could come up with a more meaningful output format...


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

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

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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://42127]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (10)
As of 2017-05-26 19:01 GMT
Find Nodes?
    Voting Booth?