In Perl, is there a way to force all fatal errors to display a stack backtrace like Carp::confess
produces?
I know you can do use warnings FATAL => 'all';
to make warnings fatal over the current lexical scope.
Further it is possible to use $SIG{__WARN__} = sub { CORE::die(@_) };
to make all warnings fatal everywhere (that hasn't localized the SIGWARN handler).
Is there a clean way to do this, or do I need to tweak SIGDIE? And if I do write a SIGDIE handler, what is the best way to get the trace?
An ideal solution would work with the standard testing libraries, Test::More
and friends.
Update: Mark Johnson suggests using a SIGDIE handler to call Carp::confess
. It works nicely. Here's the code:
use Carp;
$SIG{ __DIE__ } = \&Carp::confess;
Install a SIGDIE handler that calls Carp::confess? Or just set up Carp::confess as the handler for DIE?
Beware of the standard gotchas related to eval. There is an even weirder gotcha with regard to BEGIN blocks. Also note the ominous warning in perlvar.
See this question for more information on generating stack traces.
See also the module "Carp::Always", which turns all dies and warns in your code to stacktraces.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With