in reply to logging, to include unhandled exceptions

use Monitor;
plus
package Monitor; BEGIN { ... } END { ... } $SIG{__DIE__} = sub { die $@ if $^S; ... }; $SIG{__WARN__} = sub { ... }; 1;

References:

Replies are listed 'Best First'.
Re^2: logging, to include unhandled exceptions
by klassa (Acolyte) on Sep 02, 2008 at 19:20 UTC

    Thanks... I'd considered __DIE__, but was a little put off by the warning in 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 also not quite sure I understand the use of $^S, in your example. According to the man page, your code would propagate the die -- but only if executing an eval. Why not always do it?

    Anyway, in light of the warning in perlvar, what "difficult to track down" errors am I apt to run into, here? I mean, is there a way to categorize them in some way?

    Thanks!

      According to the man page, your code would propagate the die -- but only if executing an eval. Why not always do it?

      Not quite. I propagated the die without further action if executing an eval.

      I left up to you what to do when outside of any eval, likely something followed by die or exit.

      I don't really see the difference between using $SIG{__DIE__} and overriding CORE::GLOBAL::die, so you might as well use the latter.

      BEGIN { *CORE::GLOBAL::die = sub { CORE::die(@_) if $^S; ... }; } BEGIN { *CORE::GLOBAL::warn = sub { ... }; }

      We still need to check $^S, so I don't understand the warning perlvar gives.

      Anyway, in light of the warning in perlvar, what "difficult to track down" errors am I apt to run into, here?

      I don't know.

        Ahhhhh. I understand your reasoning for using $^S now. Thanks for the clarification...

        I guess the only remain point of confusion I have is about die itself. perlfunc isn't clear on this, as I read it. When something "blows up" in a script, is die what perl invokes, under the covers? It seems not to be, except when it is (so to speak). Consider this code:

        # $SIG{__DIE__} = sub { print "I died.\n"; exit 0 }; BEGIN { *CORE::GLOBAL::die = sub { print "Died in the new die.\n"; exit 0; }; } print "Requiring something bogus.\n"; require "bogus"; print "Done requiring something bogus.\n";

        When I leave the $SIG line commented out, I do not see "Died in the new die". When I uncomment the $SIG line, however, I do see "I died". That's counter-intuitive, since you wouldn't think that $SIG{__DIE__} would fire in the absence of an actual call to die.

        Anyway, as a result of that, I'm inclined to go with the $SIG approach after all, since it seems to catch this kind of a failure -- which is what I'm trying to accomplish. Thanks again!