Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

forcing eval'd die to ignore global $SIG{__DIE__} handler

by cLive ;-) (Prior)
on Sep 22, 2007 at 20:04 UTC ( #640544=perlquestion: print w/replies, xml ) Need Help??
cLive ;-) has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to track down a rogue die in a large system that contains a lot of eval'd code. I was going to use a $SIG{__DIE__} handler to collect system state info at die, something like this:
#!/usr/bin/perl use strict; use warnings; # create a die handler - this will have stack trace # and global var dump in RL local $SIG{__DIE__} = sub { print "REAL DIE CAPTURED @_\n"; exit(0); } +; # localize the die handler (works as expected) print "eval die with local __DIE__ sig handler:\n"; eval { local $SIG{__DIE__}; die "OMG - someone killed kenny"; }; if ($@) { print "Eval die captured: $@\n"; } # no local die triggers handler above - expected, but unwanted. print "eval die without local __DIE__ sig handler:\n"; eval { die "OMG - someone killed kenny"; }; if ($@) { print "Eval die captured: $@\n"; } # real die, works as expected when previous case commented out print "REAL die:\n"; die("This is a real error"); print "\nDie failed...\n";

But, as you can see, I would have to find every eval and add in a localized $SIG{__DIE__} to override the newly defined global.

Is there any way I can force all evals to ignore the global handler and carry on as normal? Or am I going about this the wrong way? Perhaps I need to dive back into Carp to see what I'm missing. Hmmmm...

Thoughts anyone?

Replies are listed 'Best First'.
Re: forcing eval'd die to ignore global $SIG{__DIE__} handler
by ikegami (Pope) on Sep 22, 2007 at 20:39 UTC
    In your handler, don't print and exit if $^S is true.
      quote latest "perldoc perlvar":

      Having to even think about the $^S variable in your exception handlers is simply wrong. $SIG{__DIE__} as currently implemented invites grievous and difficult to track down errors. Avoid it and use an "END{}" or CORE::GLOBAL::die override instead.

      I'm not saying that I support that, quite the contrary, but at least it is worth to know about this sudden perversion of the nature.

      Thanks! Damn I knew there was something simple I was missing. Heh.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://640544]
Approved by shmem
Front-paged by Old_Gray_Bear
[davido]: jdporter : Is there a specific issue, or are we all just getting up there?

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (8)
As of 2018-06-21 16:38 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (118 votes). Check out past polls.