|Perl: the Markov chain saw|
Re: 'Dynamic scoping' of capture variables ($1, $2, etc.)by moritz (Cardinal)
|on Dec 16, 2012 at 20:42 UTC||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:
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.
As to your actual question:
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:
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.