Re: Recursion and ${*digit*} variables
- From: sln@xxxxxxxxxxxxxxx
- Date: Fri, 14 Aug 2009 18:30:51 -0700
On Fri, 14 Aug 2009 16:17:32 -0700 (PDT), "C.DeRykus" <derykus@xxxxxxxxx> wrote:
On Aug 14, 2:46 pm, "C.DeRykus" <dery...@xxxxxxxxx> wrote:
On Aug 14, 11:49 am, Mark <goo...@xxxxxxxxxxxxxxxx> wrote:
It appears that new ${*digit*} variables are not created when the same
subroutine is re-entered. Is this to be expected?
use strict ;
use warnings;
my $line = 'abc';
doit();
exit;
sub doit {
# local($1); # adding this doesn't appear to have an effect
$line =~ /(.)/ || die "no match";
print "bef \$1 =$1\n";
$line = substr($line,1);
doit() if length($line);
my $thing = $1; # WRONG! $1 has been changed by recursive call to
doit()
print "aft \$1 =$1\n";
}
Output:
bef $1 =a
bef $1 =b
bef $1 =c
aft $1 =c
aft $1 =c
aft $1 =c
Expected output:
bef $1 =a
bef $1 =b
bef $1 =c
aft $1 =c
aft $1 =b
aft $1 =a
No, this came up in a recent thread. Even if a match fails
within a given scope, a previously successful backref isn't
cleared:
my $s='a';
for (1..2){
print $s=~/(.)/ ? "match:$1" : "no match:$1";
$s=substr($s,1);}
__END__
match:a
no match:a
Just to expand that explanation a bit more:
sub doit {
$line =~ /(.)/ || die "no match";
print "bef \$1 =$1\n"; # matches succeed initially
$line = substr($line,1); # as $line gets emptied
doit() if length($line); # and doit() recurses
my $thing = $1; # WRONG! ..
print "aft \$1 =$1\n";
}
By the time the recursion starts to unwind, $line has
been drained and the match fails. Because the final "c"
matched successfully though, $1 doesn't get cleared.
So you see the trailing series of "c" printed.
Matching had nothing to do with why his $1 contained 'c'
when the stack unwound.
The reason is he keeps reenterring the same scope block.
In terms of N vars, there is only one scope block so
the N vars keep thier last state.
N var state is not stacked, it only has scope.
-sln
.
- Follow-Ups:
- Re: Recursion and ${*digit*} variables
- From: sln
- Re: Recursion and ${*digit*} variables
- References:
- Recursion and ${*digit*} variables
- From: Mark
- Re: Recursion and ${*digit*} variables
- From: C.DeRykus
- Re: Recursion and ${*digit*} variables
- From: C.DeRykus
- Recursion and ${*digit*} variables
- Prev by Date: Re: Recursion and ${*digit*} variables
- Next by Date: Re: Recursion and ${*digit*} variables
- Previous by thread: Re: Recursion and ${*digit*} variables
- Next by thread: Re: Recursion and ${*digit*} variables
- Index(es):
Relevant Pages
|