clinton has asked for the wisdom of the Perl Monks concerning the following question:
According to the perlvar docs for $SIG{__DIE__}:
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.
However, I find this particular behaviour useful. For instance, in my mod_perl application, pretty much all the code is wrapped in an eval which lets me return a nice error page to the user. Because of this glitch, I am able to inflate an uncaught exception into an error object, with attached stacktrace:
#=================================== sub uncaught_error { #=================================== die @_ if ref $_[0] # already an excep +tion object || ! defined $^S; # code is being co +mpiled my ($class) = caller(0); # throw() does the + work of inflating @_ = ( $class, 'Uncaught', join( '', @_ ) ); # an error message + into an exception object goto $class->can('throw') || \&throw; # of the relevant +class } $SIG{__DIE__} = \&uncaught_error;
Once this 'glitch' is fixed, the above code will no longer work (as in it will no longer give me a stack trace from the place where the error occurred) . Is there a better (not-about-to-be-deprecated) way to do this?
thanks
Clint
UPDATE: Added some comments to the code
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Deprecated use of $SIG{__DIE__} is useful (haste)
by tye (Sage) on Feb 28, 2009 at 19:01 UTC | |
Re: Deprecated use of $SIG{__DIE__} is useful
by ikegami (Patriarch) on Feb 28, 2009 at 19:03 UTC | |
by clinton (Priest) on Feb 28, 2009 at 19:09 UTC | |
by ikegami (Patriarch) on Feb 28, 2009 at 19:13 UTC | |
by perrin (Chancellor) on Feb 28, 2009 at 23:02 UTC | |
by ikegami (Patriarch) on Feb 28, 2009 at 23:07 UTC | |
by perrin (Chancellor) on Feb 28, 2009 at 23:38 UTC | |
Re: Deprecated use of $SIG{__DIE__} is useful
by perrin (Chancellor) on Feb 28, 2009 at 23:04 UTC | |
by tye (Sage) on Mar 01, 2009 at 02:44 UTC |
Back to
Seekers of Perl Wisdom