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

Re: ping

Thread Previous
Mark-Jason Dominus
July 30, 2001 05:04
Re: ping
Message ID:

> 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




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=''>Click Here</a>

He wants to generate the data structure:

        ( URL  => "",
          TEXT => "Click Here",

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

         (.*?)(?{assign('URL', $1)})
         (.*?)(?{assign('TEXT', $2)})

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=''>Perl</a>
        1/2: <a href=''>Plover Systems</a>
        2/1: <a href='>Perl Mongers</a>
        2/2: <a href=''>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 Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About