in reply to Are we seeing syntax inconsistency?

Because $index is lexically scoped, it follows different rules than $_, which is dynamically scoped.

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

  • Comment on Re: Are we seeing syntax inconsistency?

Replies are listed 'Best First'.
Re^2: Are we seeing syntax inconsistency?
by Errto (Vicar) on Nov 13, 2005 at 21:46 UTC
    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.
    This is true, but there is a twist. For anonymous subroutines, Perl is smart enough to notice whether they are closures or not and if not it will actually reuse the same sub reference and its associated structures each time. See the following:
    my (%withlex, %withglobal); for (1 .. 10) { my $ref = sub { return $_ }; $withglobal{$ref} = $ref; } for my $i (1 .. 10) { my $ref = sub { return $i }; $withlex{$ref} = $ref; } print "Using lex num of subs is " . (keys %withlex) . "\n"; print "Using global num of subs is " . (keys %withglobal) . "\n";
    which prints
    Using lex num of subs is 10 Using global num of subs is 1
    I also learned recently that a closure only captures the lexical variables the inner subroutine actually uses, so you can have issues like Re: Eval doesn't see lexicals. So in the OP's case, there really is only one anonymous subroutine in the $_ version.
      And just to fool around with the above code, the following example emphasizes the previous point that what matters is the current value of the global $_ when the anonymous sub runs, not its value at its time of creation. Whereas with the lexical version, what matters is the value of lexical $i at time of creation

      my (%withlex, %withglobal); for (1 .. 10) { my $ref = sub { return $_ }; $withglobal{$ref} = $ref; print $ref->(), "\n"; } print keys %withglobal; print "\n"; $_ = 'foo'; foreach my $key ( keys %withglobal ) { print "k: $key\tv: ", &{ $withglobal{$key} }(), "\n"; } for my $i (1 .. 10) { my $ref = sub { return $i }; $withlex{$ref} = $ref; print $ref->(), "\n"; } print sort { $withlex{$a} <=> $withlex{$b} } keys %withlex; print "\n"; my $i = 'bar'; foreach my $key ( sort { $withlex{$a} <=> $withlex{$b} } keys %withlex + ) { print "k: $key\tv: ", &{ $withlex{$key} }(), "\n"; } print "Using lex num of subs is " . (keys %withlex) . "\n"; print "Using global num of subs is " . (keys %withglobal) . "\n";