Nope. Each coderef (set of coderefs in your example - it's the time that the lexical is in scope that matters) has it's own copy.
sub make_closures { my $value = shift; return sub { $value++ }, sub { print "$value\n" }, sub { $value-- }; } my ($increment, $display, $decrement) = make_closures( 3 ); print "first\n"; $display->(); $increment->(); $display->(); $decrement->(); $display->(); my ($increment2, $display2, $decrement2) = make_closures( 9 ); print "second\n"; $display2->(); $increment2->(); $display2->(); $decrement2->(); $display2->(); print "first again\n"; $display->(); $increment->(); $display->(); $decrement->(); $display->();
Result:
first 3 4 3 second 9 10 9 first again 3 4 3
That's why it needs to be lexical.
In reply to Re: Re: Re: Help with the concept of closures.
by bobn
in thread Help with the concept of closures.
by DigitalKitty
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |