in reply to Re^2: Are we seeing syntax inconsistency?
in thread Are we seeing syntax inconsistency?

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";