my $anon1 = outer(1);
my $anon2 = outer(2);
inner();
$anon1->(3);
$anon2->(4);
inner();
$anon1->();
$anon2->();
sub outer {
my $y = my $x = shift;
sub inner {
print "Inner thinks x is '$x'.\n";
}
return sub {
print "Anon $y thinks x is '$x'.\n";
if (@_) {
$x = shift;
print "Anon $y reset x to '$x'.\n";
}
};
}
__DATA__
Inner thinks x is '1'.
Anon 1 thinks x is '1'.
Anon 1 reset x to '3'.
Anon 2 thinks x is '2'.
Anon 2 reset x to '4'.
Inner thinks x is '3'.
Anon 1 thinks x is '3'.
Anon 2 thinks x is '4'.