{
my $x = 20;
sub do_stuff {
print "$x \n";
}
}
do_stuff(); #$x has gone out of scope here
--output:--
20 #…yet the sub can still see $x
####
use threads;
use threads::shared;
my $x = 20;
sub do_stuff{
print "$x \n"; #closes over $x (as above)
}
threads->create(\&do_stuff)->join();
--output:--
20
####
use threads;
use threads::shared;
sub do_stuff{
print "$x \n"; #doesn't close over $x
}
my $x = 20;
threads->create(\&do_stuff)->join(); #perl copies $x to the thread
--output:--
#but the thread can't see $x