From: Vincent Pit Date: Sun, 23 Aug 2009 15:28:58 +0000 (+0200) Subject: Add the PERL_INDIRECT_PM_DISABLE environment variable X-Git-Tag: v0.18~2 X-Git-Url: http://git.vpit.fr/?a=commitdiff_plain;h=9e1b933a7cdeed368a062f29cfe995d06416b714;p=perl%2Fmodules%2Findirect.git Add the PERL_INDIRECT_PM_DISABLE environment variable --- diff --git a/MANIFEST b/MANIFEST index 1bf2e07..5090e23 100644 --- a/MANIFEST +++ b/MANIFEST @@ -10,6 +10,7 @@ samples/indirect.pl t/00-load.t t/10-args.t t/11-line.t +t/12-env.t t/20-good.t t/21-bad.t t/22-bad-mixed.t diff --git a/lib/indirect.pm b/lib/indirect.pm index 27c37b4..c6ae98a 100644 --- a/lib/indirect.pm +++ b/lib/indirect.pm @@ -58,8 +58,12 @@ This module is B a source filter. =cut BEGIN { - require XSLoader; - XSLoader::load(__PACKAGE__, $VERSION); + if ($ENV{PERL_INDIRECT_PM_DISABLE}) { + *_tag = sub ($) { 1 }; + } else { + require XSLoader; + XSLoader::load(__PACKAGE__, $VERSION); + } } =head1 METHODS @@ -153,6 +157,16 @@ The default warning/exception message thrown when an indirect call on an object The default warning/exception message thrown when an indirect call on a block is found. +=head1 ENVIRONMENT + +=head2 C + +If this environment variable is set to true when the pragma is used for the first time, the XS code won't be loaded and, although the C<'indirect'> lexical hint will be set to true in the scope of use, the pragma itself won't do anything. +This is useful for disabling C in production environments. + +Note that clearing this variable after C was loaded has no effect. +If you want to reenable the pragma later, you also need to reload it by deleting the C<'indirect.pm'> entry from C<%INC>. + =head1 CAVEATS The implementation was tweaked to work around several limitations of vanilla C pragmas : it's thread safe, and doesn't suffer from a C bug that causes all pragmas to propagate into Cd scopes. diff --git a/t/12-env.t b/t/12-env.t new file mode 100644 index 0000000..2e3584b --- /dev/null +++ b/t/12-env.t @@ -0,0 +1,19 @@ +#!perl + +use strict; +use warnings; + +use Test::More tests => 3; + +{ + local $ENV{PERL_INDIRECT_PM_DISABLE} = 1; + my $err = 0; + my $res = eval <<' TEST_ENV_VARIABLE'; + return 1; + no indirect hook => sub { ++$err }; + my $x = new Flurbz; + TEST_ENV_VARIABLE + is $@, '', 'PERL_INDIRECT_PM_DISABLE test doesn\'t croak'; + is $res, 1, 'PERL_INDIRECT_PM_DISABLE test returns the correct value'; + is $err, 0, 'PERL_INDIRECT_PM_DISABLE test didn\'t generate any error'; +}