develooper Front page | perl.perl5.porters | Postings from May 2003

Re: [perl #22369] $1 dynamic scoping breaks with recursion

Thread Previous
From:
Rafael Garcia-Suarez
Date:
May 30, 2003 00:58
Subject:
Re: [perl #22369] $1 dynamic scoping breaks with recursion
Message ID:
20030530095519.3f049686.rgarciasuarez@free.fr
"frederik@ugcs.caltech.edu (via RT)" <perlbug-followup@perl.org> wrote:
> 
> sub r
> { 
>   shift =~ /(.*)/;
>   if(shift) {
>     r("bar", 0);
>     print "$1\n";
>   }
> }
> r("foo", 1);
> 
> # prints: bar
> # (expected: foo)

As I *know* how the digit variables are implemented, I actually expect
"bar" there, but you're right, most people probably expect "foo".

Basically regexp vars ($1 and alii) are associated to regexps. There's
one set of such variables per regexp. In addition, there's some magic
to change the "current" regexp -- the one in scope -- at scope exit.
There's only one regexp in your code snippet I quoted, thus only one $1
variable slot, so it's normal that you get only one value out of $1.

This could be better documented.

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