Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Template system and substitution

by Anonymous Monk
on Nov 17, 2000 at 02:00 UTC ( [id://42091]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have a template system and need to find templated areas of my code.

The tags that mark types of templated sections are as follows:

open close [( )] [| |] [{ }]
What I want to find is the code within these tags to pass to a subroutine

    Things I need to do
  • Find [
  • Find ( or | or {
  • Match anything that isn't the mirror of the tag above
  • Match the mirrored tag
  • Match the closing ]

I've been playing around with the regex but am having a tough time.

Any help would be much appreciated

Replies are listed 'Best First'.
Re: Template system and substitution
by mirod (Canon) on Nov 17, 2000 at 02:08 UTC

    Why o Why don't you use Text::Template or one of the 263 other template modules on CPAN? Do you think the World need another template system?
    Seriously, you will start with a simple template mechanism, then you will add function support, then something else, then one more feature... and before you know it you have built a package with most of the features of the CPAN ones, just in a much more clunky way and without their robustness.

    Please do yourself a favor and use a module!

      I don't think Text::Template will do what this guy wants. With Text::Template you can make the delimiters anything you want, but you can't have more than one set of delimiters. So you could have [(...)] or [|...|], but not both.

      And actually, I only know of 29 template modules. Are you sure there are 263?

(tye)Re: Template system and substitution
by tye (Sage) on Nov 17, 2000 at 02:13 UTC

    s# \[ ( \( .*? \) | \| .*? \| | \{ .*? \} ) \] # expand( substr($1,0,1), substr($1,1,-1) ) #gex;
    seems okay.

            - tye (but my friends call me "Tye")
      Haven't you read Death to Dot Star yet? You will have much trouble matching something like [{ [{ }] }]

      -Ted

        Um, if we had single-character delimiters, then the suggestion in Death to Dot Star would apply; use a negated character class in place of "dot":

        m# \[ [^\]]* \] #x
        but that isn't the case here.

        If you are wanting me to do that complex trick to avoid matching a closing (or even opening) delimiter in the middle, then I don't think you read the thread. Lots of good people tried and failed to get it right. I don't recommend doing that trick since I've never seen it proposed (even by amazing people) without someone finding an error in it.

        Plus, it wouldn't gain us anything in this case. If I see an opening delimiter in this situation, I want to force the match to start there. It would be an error to have extra, unmatch starting delimiters. We aren't trying to parse English here.

        As for matching nested blocks, that pretty much violates the original design. For defensive programming, I'd probably have the expand() routine look for and warn of opening delimiters in the match string since these might indicate that a closing delimiter was dropped or munged. But to be completely defensive would require more work than that.

                - tye (but my friends call me "Tye")
        In the event that you did have nested tags, you would probably want to go for a recursive function. Something like:

        sub parse { if ( $_[0] =~ /\[(.*)$/ ) { parse($1)} else { if ($_[0] =~ /(.*)\]/ ) { #Do your replace on $1 }else{ die "Unmatched brackets found"; } } }

        or something like that anyway. But you probably wouldn't want nested brackets in a template system.

        ____________________
        Jeremy

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://42091]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-04-24 13:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found