develooper Front page | perl.perl5.porters | Postings from July 2001

Re: ping

Thread Previous
From:
Mark-Jason Dominus
Date:
July 30, 2001 05:04
Subject:
Re: ping
Message ID:
20010730120417.29893.qmail@plover.com

> Eradicate-$^N-named-captures-reminder to sit in your inbox until
> you feel guilty enough.

On Thursday I had a long conversation with Autrijius Tang, who says
he is using $^N.  I tried to understand why, and if an alternative
solution would do.  My conclusion was that $^N is the correct solution
to his problem.  

For Friedl's suggestion, $^N is unnecessary, because it's not hard to
transform a regex like

        /...(?$foo...).../

into 

        /...(...(?{$foo=$37})).../

where the 37 can be generated at compile time.  A module which
overloads regex constants can do a lexical analysis of the incoming
regex, count the parentheses, and generate the correct $37 variable.
$^N would make this simpler, but is not necessary.

In Autrijius's example, this is not enough.  Autrijius is taking a
Template Toolkit template specification, and the output from the
template operation, and trying to reconstruct the values that were
substituted for the escape sequences in the template.  A simple
example is: Given the template

        <a href='[% URL %]'>[% TEXT %]</a>

and the output

        <a href='http://www.perl.com'>Click Here</a>

He wants to generate the data structure:

        ( URL  => "http://www.perl.com/",
          TEXT => "Click Here",
        )

To do this, he transforms the template into a dynamic regex something
like this:

        /<a\shref='
         (.*?)(?{assign('URL', $1)})
         '>
         (.*?)(?{assign('TEXT', $2)})
         </a>
        /x

For this simple example, it's easy to generate the appropriate
numerals for the $1, $2, and so on.  But templates may also include
'for' loops, and nested 'for' loops:

        [% foreach i %]
          [% foreach j %]
            i/j: <a href='[% URL %]'>[% TEXT %]</a>
          [% endfor %]
        [% endfor %]
        
        1/1: <a href='http://www.perl.com'>Perl</a>
        1/2: <a href='http://www.plover.com'>Plover Systems</a>
        2/1: <a href='http://www.pm.org>Perl Mongers</a>
        2/2: <a href='http://www.google.com'>Google</a>

In this case the dynamic pattern is much more complicated and it
becomes difficult to keep track of the numbers while generating it.
Being able to use $^N  in place of $1, $2, ... is a major convenience.

On the other hand, I still wonder why there needs to be a new variable
for this.  Could $^N be eliminated, and the functionality were
transferred to $+?  That seems to me like a natural extension of what
$+ already does.


Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About