Syntactic Confectionery Delight PerlMonks

### Re: 'Dynamic scoping' of capture variables (\$1, \$2, etc.)

by moritz (Cardinal)
 on Dec 16, 2012 at 20:42 UTC ( #1009092=note: print w/replies, xml ) Need Help??

What you are observing is exactly what dynamic scoping is about: Code called from where the variable is defined can see it, even though it's not in the same lexical scope.

This can be demonstrated without recursion:

```use 5.010;
use strict;
use warnings;

sub sayit {
say \$1 // 'undef';
}

do {
'42' =~ /(\d+)/ and sayit();
};
sayit();
__END__
42
undef

Subroutine sayit reads \$1 outside of the lexical scope of the block where \$1 is set. But since it's in the dynamic scope, it can still see the value.

The sayit call outside the block prints undef\n, which demonstrates that \$1 isn't merely a global variable.

However, \$1, set to '1' by the last successful match at the lowest-but-one level of recursion, is propagated upward unchanged through several levels of subroutine 'blocks'

I see no evidence for that. After \$1 is set to '1', exactly one more recursive call happens, and there it is printed out. Then the recursion ends, and you don't print \$1 anymore.

Update: after experimenting a bit, I can provide evidence of the phenomen you mentioned:

```sub R {
printf qq{before: \\$_ is '\$_'};
printf qq{  \\$1 is %s \n}, defined(\$1) ? qq{'\$1'} : 'undefined';
s/(\d+)// ? \$1 + R() : 0;
printf qq{after: \\$_ is '\$_'};
printf qq{ \\$1 is %s \n}, defined(\$1) ? qq{'\$1'} : 'undefined';
}

\$_ = 'x55x666x7777x1x';
R();
__END__
before: \$_ is 'x55x666x7777x1x'  \$1 is undefined
before: \$_ is 'xx666x7777x1x'  \$1 is '55'
before: \$_ is 'xxx7777x1x'  \$1 is '666'
before: \$_ is 'xxxx1x'  \$1 is '7777'
before: \$_ is 'xxxxx'  \$1 is '1'
after: \$_ is 'xxxxx' \$1 is '1'
after: \$_ is 'xxxxx' \$1 is '1'
after: \$_ is 'xxxxx' \$1 is '1'
after: \$_ is 'xxxxx' \$1 is '1'
after: \$_ is 'xxxxx' \$1 is '1'

This is because there is only one variable \$1. It is dynamically scoped, so once it is set in an inner scope, an outer scope sees the modification too.

Replies are listed 'Best First'.
Re^2: 'Dynamic scoping' of capture variables (\$1, \$2, etc.)
by AnomalousMonk (Chancellor) on Dec 16, 2012 at 22:13 UTC
However, \$1, set to '1' by the last successful match at the lowest-but-one level of recursion, is propagated upward unchanged through several levels of subroutine 'blocks'
I see no evidence for that. After \$1 is set to '1', exactly one more recursive call happens, and there it is printed out. Then the recursion ends, and you don't print \$1 anymore.

I don't print \$1, but it must be '1' at all higher recursion levels because only that value will result in a sum total of 4.

However, I have not had (and will not immediately have) a chance to ponder your link and other responses.

I suppose my chief confusion stems from the fact that \$1 starts out undefined, takes on a bunch of other values, then winds up as it started. If it had finished up as '1', my mind would rest a bit easier, but at some point, and only one point mind you, it's leaving some kind of scope and being restored to the value it had upon entry. I could understand all scopes, I could understand none, but I can't (yet) understand just one!

You are right be worried, your example code is just a bit to complex to make it evident at first glance.

There is no logical reason why nested calls of the same function (i.e. recursions) should act differently to nested calls of different functions.

see updated code, especially the second paragraph contrasting the bug.

Cheers Rolf

UPDATE:

to be sure to avoid any side effects from eval within the debugger here a standalone file for testing:

```use warnings;
use strict;
use 5.10.0;

my \$x;

sub delchar { \$x =~ s/(\w)// ?  \$1 . delchar() . \$1  : "x" }

\$x='abc';
say delchar();                            #  => "cccxccc"

sub del1 { \$x =~ s/(\w)// ? \$1 . del2() . \$1 : "x"  }
sub del2 { \$x =~ s/(\w)// ? \$1 . del3() . \$1 : "x"  }
sub del3 { \$x =~ s/(\w)// ? \$1 . del4() . \$1 : "x"  }
sub del4 { \$x =~ s/(\w)// ? \$1 . del5() . \$1 : "x"  }

\$x='abc';
say del1();                              # => "abcxcba"

UPDATE:

Best practice is to copy captures like \$1 ASAP! (not only in recursions)

```  DB<105> sub delchar { my \$m; \$m = \$1 if \$x =~ s/(\w)//;  \$m ?  \$m .
+delchar() . \$m : "-"  }
=> 0

DB<106> \$x='abc'; delchar()
=> "abc-cba"

DB<107> sub delchar { local \$m; \$m = \$1 if \$x =~ s/(\w)//;  \$m ?  \$m
+ . delchar() . \$m : "-"  }
=> 0

DB<108> \$x='abc'; delchar()
=> "abc-cba"
Re^2: 'Dynamic scoping' of capture variables (\$1, \$2, etc.)
by AnomalousMonk (Chancellor) on Dec 16, 2012 at 23:17 UTC

moritz:
Really gotta go now, but just one more experiment: without recursion (actually, along the lines LanX has already posted).

```>perl -wMstrict -le
"\$_ = 'x77x888x1x';
;;
'zonk' =~ m{(\w+)}xms;
;;
print 'before: \$1 is ', defined(\$1) ? qq{'\$1'} : 'undefined';
print X1();
print 'after: \$1 is ', defined(\$1) ? qq{'\$1'} : 'undefined';
;;
sub X1 { s/(\d+)//;  print qq{  '\$1'};  return \$1 + X2(); }
sub X2 { s/(\d+)//;  print qq{  '\$1'};  return \$1 + X3(); }
sub X3 { s/(\d+)//;  print qq{  '\$1'};  return \$1;        }
"
before: \$1 is 'zonk'
'77'
'888'
'1'
966
after: \$1 is 'zonk'

Without recursion, value of \$1 seems to be scoped to the subroutine 'block'.
No difference in results for  Xn() + \$1 versus  \$1 + Xn() or for \$1 being undefined initially.

Re^2: 'Dynamic scoping' of capture variables (\$1, \$2, etc.)
by nvivek (Vicar) on Dec 18, 2012 at 12:30 UTC
For clear definition, as \$_ value got changed in every recursive call of R() function, \$1 value also got changed and in return of every recursive call, the last changed value available and that is the reason for getting '1' 5 times in \$1. For better understanding, here below I have shown how that recursive calls and values of \$_ and \$1 will be.

Initial ( First ) callback of R() function:
\$_='x55x666x7777x1x'; \$_=undefined;
Inside code, after evaluating s/(\d+)// ? \$1 + R() : 0;
\$1=55; #immediately after executing s// command
Second callback of R():
\$_='xx666x7777x1x'; \$1=55;
Inside code, after evaluating s/(\d+)// ? \$1 + R() : 0;
\$1=666; #immediately after executing s// command
Third callback of R():
\$_='xxx7777x1x'; \$1=666;
Inside code, after evaluating s/(\d+)// ? \$1 + R() : 0;
\$1=7777; #immediately after executing s// command
Fourth callback of R():
\$_='xxxx1x'; \$1=7777;
Inside code, after evaluating s/(\d+)// ? \$1 + R() : 0;
\$1=1; #immediately after executing s// command
Fifth callback of R():
\$_='xxxxx'; \$1=1;
Inside code, after evaluating s/(\d+)// ? \$1 + R() : 0;
# no callback executed as ( there are no digits ) pattern not matched
\$1=undefined; #immediately after executing s// command
# after prints 'xxxxx' and '1' for fifth callback
# after prints 'xxxxx' and '1' for fourth callback
# after prints 'xxxxx' and '1' for third callback
# after prints 'xxxxx' and '1' for second callback
# after prints 'xxxxx' and '1' for first callback

Kindly regret me for format. I unable to get exact stack of recursive calls for presentation.

It's a good guess, because there is a the fact that \$1 points into the original string, and can change when the original string changes. But it's not the source of the confusion here.

You can see that by changing the original code to use m/(\d+)/g instead of s/(\d+)//, thus not modifying the original string at all:

```sub R {
printf qq{before: \\$_ is '\$_'};
printf qq{  \\$1 is %s \n}, defined(\$1) ? qq{'\$1'} : 'undefined';
m/(\d+)/g ? \$1 + R() : 0;
printf qq{after: \\$_ is '\$_'};
printf qq{ \\$1 is %s \n}, defined(\$1) ? qq{'\$1'} : 'undefined';
}
\$_ = 'a81b2d34c1';
R();
__END__
before: \$_ is 'a81b2d34c1'  \$1 is undefined
before: \$_ is 'a81b2d34c1'  \$1 is '81'
before: \$_ is 'a81b2d34c1'  \$1 is '2'
before: \$_ is 'a81b2d34c1'  \$1 is '34'
before: \$_ is 'a81b2d34c1'  \$1 is '1'
after: \$_ is 'a81b2d34c1' \$1 is '1'
after: \$_ is 'a81b2d34c1' \$1 is '1'
after: \$_ is 'a81b2d34c1' \$1 is '1'
after: \$_ is 'a81b2d34c1' \$1 is '1'
after: \$_ is 'a81b2d34c1' \$1 is '1'

Let me repeat what I wrote earlier, but hopefully a bit clearer this time: There is only one variable \$1. The first call to m/()/ or s/()// creates the dynamic variable \$1, and all subsequent calls modify the existing variable \$1. Since there is no mechanism for resetting \$1 to a previous value, you can see the last value of \$1 in all stack frames that have access to it.

Thanks for your explanation Moritz. I meant \$1 value got changed similar to \$_ value for better understanding. I know that \$_ value getting changed due to substitute operator replaces matched digits to null string. I have a doubt in your example, how \$1 value getting changed every recursive call. As per your code, your m// expression will match first digits ( 55 ) at all time as per my understanding. Of course, when I checked the code, it gives \$1 value as 55, 666, 7777 and 1 respectively. Kindly give me some detail about it.

Create A New User
Node Status?
node history
Node Type: note [id://1009092]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2018-05-23 11:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
World peace can best be achieved by:

Results (168 votes). Check out past polls.

Notices?