> The more interesting case is this:
>
> #!/my/path/to/perl
> sub foo_generator { my $a = shift; sub { print $a++ } }
> my $foo = foo_generator(1);
> $foo->();
> Thread->new($foo);
> Is $a shared between threads or not?
$a is shared between threads.
The anonymous subroutine is a closure.
Closures work across threads.
> IMHO the rule is not as simple as this RFC states. (Partly because of
> confusion about "executing" my.)
It is almost as simple. I should add an example (like this one)
showing the behavior of closures.
> Perhaps Thread->new should deep copy the code ref before executing
> it? Deep copy lexicals but not globals? Deep copy anything that doesn't
> already have a mutex lock?
no no no...perlref.pod, Making References, item 4 says
A reference to an anonymous subroutine can be created by using sub
without a subname:
$coderef = sub { print "Boink!\n" };
...no matter how many times you execute that particular line
(unless you're in an eval("...")), $coderef will still have a
reference to the same anonymous subroutine.)
We can, and should, retain this behavior.
- SWM