This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Localise PL_curcop for BEGIN blocks
authorFather Chrysostomos <sprout@cpan.org>
Thu, 17 Nov 2011 16:34:31 +0000 (08:34 -0800)
committerFather Chrysostomos <sprout@cpan.org>
Thu, 17 Nov 2011 17:18:02 +0000 (09:18 -0800)
commit16c632750a8d65ba39bf6c2e2b149e3ffebf467a
treecea370764d98d5bc12135180d4750fb2e46c0d78
parentcd6472fc34fd70358c7c0cf2c39a2eb7fe57114d
Localise PL_curcop for BEGIN blocks

Usually when a BEGIN block exits it has to set PL_curcop to
&PL_compiling, so that subsequent compiled code in the surrounding
scope will have the right warning hints during compilation.

If an XS function creates a BEGIN block via newXS or newATTRSUB, how-
ever, the assumption that compilation will resume as soon as the block
exits is false.

This can be demonstrated with this code, which warns about CHECK and
INIT blocks created too late when it shouldn’t due to ‘no warnings’:

use warnings;
eval q|
  BEGIN{
    no warnings;
    package XS::APItest; require XSLoader; XSLoader::load()
  }
|;

In every case where it is correct for BEGIN to set PL_curcop to
&PL_compiling when it exits it is actually just restoring it to its
previous value, so localisation is the right fix.
MANIFEST
ext/XS-APItest/t/check_warnings.t [new file with mode: 0644]
ext/XS-APItest/t/xs_special_subs_require.t
op.c