Well, technically it doesn't get its own copy, it shares its copy with stuff declared in the same scope:
use strict;
use warnings;
sub funcs {
my $a = 0;
return ( sub { $a++ }, sub { $a } );
}
my ( $f11, $f12 ) = funcs();
my ( $f21, $f22 ) = funcs();
print join( " ", $f11->(), $f12->(), $f22->() ), "\n"; #prints "0 1 0"
My point was that if someone wants to change state, he should understand what he's doing. And he probably does. Preventing that is rude.
The OP may be in some special situation, however. That's what my question was about.
|