This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Clone state subs in anon subs
authorFather Chrysostomos <sprout@cpan.org>
Mon, 9 Jul 2012 13:29:09 +0000 (06:29 -0700)
committerFather Chrysostomos <sprout@cpan.org>
Sun, 16 Sep 2012 05:45:01 +0000 (22:45 -0700)
commite07561e6ac7f381061f112bee32ebc779683a84c
tree86ad55151c90232dceb0b431f846beafe5434a56
parent20d337866901b1d0118edc4ff2cb2407b27e0275
Clone state subs in anon subs

Since state variables are not shared between closures, but only
between invocations of the same closure, state subs should behave
the same way.

This was a little tricky.  When we clone a sub, we now clone inner
state subs at the same time.  When walking through the pad, cloning
items, we cannot simply clone the inner sub when we see it, because it
may close over things we haven’t cloned yet:

    sub {
        state sub foo;
        my $x
        sub foo { $x }
    }

We can’t just delay cloning it and do it afterwards, because they may
be multiple subs closing over each other:

    sub {
       state sub foo;
       state sub bar;
       sub foo { \&bar }
       sub bar { \&foo }
    }

So *all* the entries in the new pad must be filled before any inner
subs can be cloned.

So what we do is put a stub in place of the cloned sub.   And then
in a second pass clone the inner subs, reusing the stubs from the
first pass.
pad.c
perly.act
perly.h
perly.tab
perly.y
t/cmd/lexsub.t