in reply to Custom SIG DIE handler that isn't executed in evals

I suggest reverting the sig die handler before the eval, then restore it, after.

Maybe something like this:

# untested my $defaultSigDie = $SIG{__DIE__}; $SIG{__DIE__} = \&SigDieHandler; sub doEval { my $savedHandler = $SIG{__DIE__}; $SIG{__DIE__} = $defaultSigDie; my $result = eval $_[0]; $SIG{__DIE__} = $savedHandler; return $result; }

Untested. YMMV

Update: Forgot about local (Thanks, anony monk)

# untested my $defaultSigDie = $SIG{__DIE__}; $SIG{__DIE__} = \&SigDieHandler; sub doEval { local $SIG{__DIE__} = $defaultSigDie; my $result = eval $_[0]; return $result; }

Replies are listed 'Best First'.
Re^2: Custom SIG DIE handler that isn't executed in evals
by Anonymous Monk on Sep 14, 2016 at 02:02 UTC

    Hi, thats what local does for globals :) it saves the value until the end of the scope/block

    { local $SIG{__DIE__} = ...; } ## no sig handler here doEval(); sub doEval { local $SIG{__DIE__} = ...; ... } ## the end
      But what to do when the eval is deep within a bunch of system modules? I'm receiving e-mails right now from my script because of an eval located in /usr/local/lib64/perl5/XSLoader.pm

        But what to do when the eval is deep within a bunch of system modules?

        see perlvar#$^S

        Expanding on what the anony monk said, your die handler can test if the die happened in an eval.

        So, maybe the following:

        # not tested sub SigDieHandler { unless ($^S) # skip if die happened in an eval { ...; # create and send email } }

        Not tested, YMMV.