in reply to Re: Using an "outer" lexical in a sub?
in thread Using an "outer" lexical in a sub?
#!/usr/bin/perl use strict; use warnings; my $ref1; my $ref2; { my $n = 10; $ref1 = sub { print( $n++, "\n" ) }; $ref2 = sub { print( $n++, "\n" ) }; } $ref1->(); # --> 10 $ref1->(); # --> 11 $ref1->(); # --> 12 print "\n"; $ref2->(); # --> 13 Huh? $ref2->(); # --> 14 ? $ref2->(); # --> 15 ?
I'd expect that first call to $ref2->(); to start up with its own squirreled-away $n (starting at 10). Why doesn't it?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Using an "outer" lexical in a sub?
by Joost (Canon) on Nov 01, 2006 at 22:03 UTC | |
by cornballer (Novice) on Nov 01, 2006 at 22:48 UTC | |
|
Re^3: Using an "outer" lexical in a sub?
by imp (Priest) on Nov 01, 2006 at 22:00 UTC |