in reply to Re: SIG, Modules, and Eval
in thread SIG, Modules, and Eval

> Look at the code of CGI::Carp - it's implemented there. They check if the exception was thrown from an eval by examining $^S.

Either I didn't make my self clear or you misunderstood or I misunderstand your comment. :) The issue is that the eval's are in modules not under my control; specifically, they're from CPAN if it matters. Those modules are essentially getting in my way and triggering my __DIE__ signal handler when I don't want that. They don't die, but they do trigger my handler uneccesarily.

Kevin

Replies are listed 'Best First'.
Re^3: SIG, Modules, and Eval
by ikegami (Patriarch) on Aug 16, 2014 at 04:38 UTC
    $SIG{__DIE__} = sub { return if $^S; ... print(STDERR $_[0]); exit($! || ($? >> 8) || 255); };
      I did something similar on one of my first tries to find out what was going on and to fix this. I looked at the call stack and if there wasn't a ".cgi" file in the list then assumed I was in the "compile" stage and just returned. Not as elegent as using $^S but it worked. If I had to do something like this, I like the $^S way better now that I know about it.

      In the end, I'd really like to know why I had to do anything like this. If we have code like:
      package X; BEGIN { whatever; } $main::SIG{__DIE__} = \&some_function; # when does this get run? sub new { ... }
      In what phase is that SIG handler assigned? I would have thought it was in an implied INIT, but the code acts like it's in an implied BEGIN. I ended up solving the entire issue by putting that line in an explicit INIT block, but why did I have to do that?
        $main::SIG{__DIE__} = \&some_function; # when does this get run?

        It gets run at the same time as any other code in the same place would. Give it a try:

        #!/usr/bin/perl use strict; use warnings; use feature qw/say/; sub function { say "Howdy!"; } #die "horribly"; $main::SIG{__DIE__} = \&function; #die "horribly"; #BEGIN { die "horribly"; }

        Try uncommenting each of the various dies; only the one that dies after the signal handler has been assigned will produce a "Howdy!". No implicit blocks of any kind are involved.

        Alternatively, check with -MO=Deparse.