Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^2: Are we seeing syntax inconsistency?

by Errto (Vicar)
on Nov 13, 2005 at 21:46 UTC ( [id://508153]=note: print w/replies, xml ) Need Help??


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

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.

Replies are listed 'Best First'.
Re^3: Are we seeing syntax inconsistency?
by ff (Hermit) on Nov 15, 2005 at 17:42 UTC
    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";

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://508153]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2024-04-24 23:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found