summaryrefslogtreecommitdiff
path: root/embed.h
diff options
context:
space:
mode:
authorFather Chrysostomos <sprout@cpan.org>2014-12-08 21:59:22 -0800
committerFather Chrysostomos <sprout@cpan.org>2014-12-09 05:42:47 -0800
commita70f21d0d169a526a6bafd2465e01e1ca8d16234 (patch)
tree140c9962225a341472bed59fd5669fee314f8419 /embed.h
parent071d040c70fbd1bbb40d7589e2a35485a11508b5 (diff)
downloadperl-a70f21d0d169a526a6bafd2465e01e1ca8d16234.tar.gz
Fix OUTSIDE for named subs inside predeclared subs
This prints 42 as expected (foo initialises $x and bar reads it via eval): use 5.01; sub foo { state $x = 42; sub bar { eval 'print $x // "u", "\n"'; } } foo(); bar(); If you predeclare foo and vivify its glob, use 5.01; *foo; # vivifies the glob at compile time sub foo; sub foo { state $x = 42; sub bar { eval 'print $x // "u", "\n"'; } } foo(); bar(); then the output is ‘u’, because $x is now undefined. What’s happening is that ‘eval’ follows CvOUTSIDE pointers (each sub points to its outer sub), searching each pad to find a lexical $x. In the former case it succeeds. In the latter, bar’s CvOUTSIDE pointer is pointing to the wrong thing, so the search fails and $x is treated as global. You can see it’s global with this example, which prints ‘globular’: use 5.01; *foo; # vivifies the glob at compile time sub foo; sub foo { state $x = 42; sub bar { eval 'print $x // "u", "\n"'; } } foo(); $main::x = "globular"; bar(); When a sub is compiled, a new CV is created at the outset and put in PL_compcv. When the sub finishes compiling, the CV in PL_compcv is installed in the sub’s typeglob (or as a subref in the stash if pos- sible). If there is already a stub in a typeglob, since that stub could be referenced elsewhere, we have to reuse that stub and transfer the contents of PL_compcv to that stub. If we have any subs inside it, those will now have CvOUTSIDE point- ers pointing to the old PL_compcv that has been eviscerated. So we go through the pad and fix up the outside pointers for any subs found there. Named subs don’t get stored in the pad like that, so the CvOUTSIDE fix-up never happens. Hence the bug above. The bug does not occur if the glob is not vivified before the sub def- inition, because a stub declaration will skip creating a real CV if it can. It can’t if there is a typeglob. The solution, of course, is to store named subs in the outer sub’s pad. We can skip this if the outer ‘sub’ is an eval or the main pro- gram. These two types of CVs obviously don’t reuse existing stubs, since they never get installed in the symbol table. Since named subs have strong outside pointers, we have to store weak refs in the pad, just as we do for formats.
Diffstat (limited to 'embed.h')
-rw-r--r--embed.h1
1 files changed, 1 insertions, 0 deletions
diff --git a/embed.h b/embed.h
index 02d25be1cb..b24a093938 100644
--- a/embed.h
+++ b/embed.h
@@ -1261,6 +1261,7 @@
#define op_unscope(a) Perl_op_unscope(aTHX_ a)
#define package(a) Perl_package(aTHX_ a)
#define package_version(a) Perl_package_version(aTHX_ a)
+#define pad_add_weakref(a) Perl_pad_add_weakref(aTHX_ a)
#define pad_block_start(a) Perl_pad_block_start(aTHX_ a)
#define pad_fixup_inner_anons(a,b,c) Perl_pad_fixup_inner_anons(aTHX_ a,b,c)
#define pad_free(a) Perl_pad_free(aTHX_ a)