in reply to goto in CORE::GLOBAL::exit - is it safe?

YES!!! My intuition was right again. :) Here it is, that bug with goto:
perl -e ' BEGIN { *CORE::GLOBAL::exit = sub { goto FASTCGI_NEXT_REQUEST; }; } while (1) { eval { that_cgi_script() }; FASTCGI_NEXT_REQUEST: last; } sub that_cgi_script { local $SIG{__DIE__} = sub { print "<p>error: $_[0]"; exit; print "XX +X\n" }; print "before buggy code\n"; eval { buggy_code() }; print "after buggy code\n"; } sub buggy_code { die "error!"; print "after die\n"; } '
This example output:
before buggy code
<p>error: error! at -e line 20.
after buggy code
i.e. goto doesn't work as expected, and execution continue after eval. Actually exit in $SIG{__DIE__} handler work like return - because "print "XXX"" doesn't executed.

I've found a workaround for this bug. In BEGIN block must be added no-op handler for CORE::GLOBAL::die (which just emulate how perl's handler work):

*CORE::GLOBAL::die = sub { if ($SIG{__DIE__}) { my $s = $_[0]; $s .= sprintf " at %s line %d.\n", (caller)[1,2] if $s !~ /\n\z/ +; $SIG{__DIE__}->($s); } };
and now this example work correctly and output:
before buggy code
<p>error: error! at -e line 27.
Sadly, but I'm not ready to explain, why adding that handler force goto to work correctly. :( Probably it's because my handler isn't work exactly like perl's handler. But I run a lot of different tests, and my handler work exactly like perl default handler for all tests except example shown above.

Replies are listed 'Best First'.
Re^2: goto in CORE::GLOBAL::exit - is it safe?
by sgt (Deacon) on Aug 31, 2007 at 16:15 UTC

    hi again, my gut feeling is any use of goto EXPR (not goto sub) which is not "lexical" (meaning you goto to a label in the same scope) is almost guaranteed to wreak havoc. I have read 'perldoc -f goto' a few times and somehow it feels scary ;)

    ok, so you want to treat exit() like an exceptional condition and let it bubble up till the surface, err the top of your event loop. Well make it happen. i.e use '*CORE::GLOBAL::exit = sub { die X::EXIT->new; }' and somehow use a die-handler which re'die's if $@->isa("X::EXIT"). Some kind of automatic "rethrow"; sorry I don't have time to test it.

    cheers --stephan update I left an ampersand after not goto...