in reply to Are we seeing syntax inconsistency?
Each time you have a "my" variable declaration (a for-loop with a "my" variable essentially does this internally at the beginning of each iteration), it allocates a fresh memory location and binds it to the variable's name. Each pass through your loop, you create an anonymous sub that refers to the memory location of $index. But in each iteration, the name "$index" refers to a different memory location, so of your 11 different anonymous subs is pointing to a different memory location -- they don't interfere with each other... The name $index is bound to different memory locations depending on your lexical scope, so once you leave the scope where the anonymous sub was created, you can't change the value that the anonymous sub will see by changing what's in $index (well, without magic), because $index won't be pointing to the same memory location.
On the other hand, dynamic scoping works more like global variables. $_ is always bound to the same memory location, it doesn't matter what scope you are in. So when you create 11 different anonymous subs that use $_, they are all pointing to the same memory location. If some code in some other place changes $_ (a likely scenario), then these anonymous subs will use that new value as well.
blokhead
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Are we seeing syntax inconsistency?
by Errto (Vicar) on Nov 13, 2005 at 21:46 UTC | |
by ff (Hermit) on Nov 15, 2005 at 17:42 UTC |