in reply to Re^3: Using an "outer" lexical in a sub?
in thread Using an "outer" lexical in a sub?
It doesn't start with it's own version of $n, because at the time you're creating $ref1 and $ref2 $n is the same variable.
Doh! Thanks Joost (and imp). :)
#!/usr/bin/perl use strict; use warnings; my $ref1; my $ref2; { my $n = 10; $ref1 = sub { print( $n++, "\n" ) }; } { my $n = 10; $ref2 = sub { print( $n++, "\n" ) }; } $ref1->(); # --> 10 $ref1->(); # --> 11 $ref1->(); # --> 12 print "\n"; $ref2->(); # --> 10 $ref2->(); # --> 11 $ref2->(); # --> 12
|
|---|