perlquestion
clinton
<p>According to the [doc://perlvar] docs for <c>$SIG{__DIE__}</c>:</p>
<blockquote><i>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. <b>Any other use is deprecated.</b></i></blockquote>
<p>However, I find this particular behaviour useful. For instance, in my mod_perl application, pretty much all the code is wrapped in an <c>eval</c> 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:</p>
<code>
#===================================
sub uncaught_error {
#===================================
die @_
if ref $_[0] # already an exception object
|| ! defined $^S; # code is being compiled
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;
</code>
<p>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?</p>
<p>thanks</p>
<p>Clint</p>
<p><b>UPDATE:</b> Added some comments to the code</p>