http://www.perlmonks.org?node_id=1089378


in reply to Bizarre Proc::Daemon error

> a) Why is the script dying when executing an expression in an eval? Isn't that the whole point of eval?

Your die handler is called from within the block eval and dies like you told him to do. ( update: or maybe not?)

You might(?)¹ be able to check the caller to avoid dieing in block-eval.

== Updates

=== From perlvar

Due to an implementation glitch, the $SIG{__DIE__} hook is called even inside an eval(). Do not use this to rewrite a pending exception in $@, or as a bizarre substitute for overriding "CORE::GLOBAL::die()". This strange action at a distance may be fixed in a future release so that $SIG{__DIE__} is only called if your program is about to exit, as was the original intent. Any other use is deprecated.

=== Possible workaround

¹) Indeed! As you can see from this test, does caller(1) tell you if the die appeared within an eval .

So better avoid to die in that case! :)

(But you should check the whole caller-chain till top-level for the string (eval) )

use Carp 'cluck'; $SIG{__DIE__}=sub {cluck "called in '",(caller(1))[3],"'\n\n" }; warn "\n---EVALED FATAL\n"; eval { $x=0/0 } or print ">>> $@"; warn "\n---NORMAL FATAL\n"; $x=0/0;

-->

---EVALED FATAL called in '(eval)' at /tmp/tst.pl line 2 main::__ANON__('Illegal division by zero at /tmp/tst.pl line 6.\x{ +a}') called at /tmp/tst.pl line 6 eval {...} called at /tmp/tst.pl line 6 >>> Illegal division by zero at /tmp/tst.pl line 6. ---NORMAL FATAL called in '' at /tmp/tst.pl line 2 main::__ANON__('Illegal division by zero at /tmp/tst.pl line 10.\x +{a}') called at /tmp/tst.pl line 10 Illegal division by zero at /tmp/tst.pl line 10.

=== "may be fixed in a future release"

Well, I wonder why this was never fixed, if a simple check of caller is sufficient.

Cheers Rolf

(addicted to the Perl Programming Language)

Replies are listed 'Best First'.
Re^2: Bizarre Proc::Daemon error # die-handler
by hv (Prior) on Jun 12, 2014 at 08:41 UTC

    I'm not sure I understand what you're saying here: eval will trap death, even when invoked from a __DIE__ handler:

    % perl -wl local $SIG{__DIE__} = sub { print "in handler (@_)"; die "from handler"; }; eval { die "foo" }; print "eval returned (but knows the error was <$@>)"; __END__ in handler (foo at - line 5. ) eval returned (but knows the error was <from handler at - line 3. >) %

    Hugo

      Strange ...

      ... your snippet runs identically on my machine and I can't reproduce anymore what I saw (!?!)

      Most likely I was wrong about why the OP's program aborts.

      Cheers Rolf

      (addicted to the Perl Programming Language)