From: Lukas Mai Date: Sun, 21 Feb 2016 19:36:19 +0000 (+0100) Subject: use a lexical variable to prevent external fondling X-Git-Tag: v0.25~3 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=8447a3bf0e1b88bdd07fa584d83c81c64ad192cf;hp=80352025d169218f726e3a0e3ad1ecd226a37596;p=p5sagit%2FTry-Tiny.git use a lexical variable to prevent external fondling --- diff --git a/lib/Try/Tiny.pm b/lib/Try/Tiny.pm index 90963b0..cf8f1f7 100644 --- a/lib/Try/Tiny.pm +++ b/lib/Try/Tiny.pm @@ -29,7 +29,7 @@ BEGIN { *_HAS_SUBNAME = ($su || $sn) ? sub(){1} : sub(){0}; } -our @_finally_guards; +my @_finally_guards; # Need to prototype as @ not $$ because of the way Perl evaluates the prototype. # Keeping it at $$ means you only ever get 1 sub because we need to eval in a list @@ -75,11 +75,13 @@ sub try (&;@) { if _HAS_SUBNAME; # set up scope guards to invoke the finally blocks at the end. - # this should really be a lexical variable instead of our/local but that - # causes issues with perls < 5.20 due to perl rt#119311 - local @_finally_guards = + # this should really be a function scope lexical variable instead of + # file scope + local but that causes issues with perls < 5.20 due to + # perl rt#119311 + local $_finally_guards[0] = [ map { Try::Tiny::ScopeGuard->_new($_) } - @finally; + @finally + ]; # save the value of $@ so we can set $@ back to it in the beginning of the eval # and restore $@ after the eval finishes @@ -112,7 +114,7 @@ sub try (&;@) { # destructor overwrote $@ as the eval was unwinding. if ( $failed ) { # pass $error to the finally blocks - push @$_, $error for @_finally_guards; + push @$_, $error for @{$_finally_guards[0]}; # if we got an error, invoke the catch block. if ( $catch ) {