Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I force a stack backtrace for all fatal errors in Perl?

Tags:

perl

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;
like image 282
daotoad Avatar asked Apr 10 '09 19:04

daotoad


2 Answers

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.

like image 157
Mark Johnson Avatar answered Oct 15 '22 05:10

Mark Johnson


See also the module "Carp::Always", which turns all dies and warns in your code to stacktraces.

like image 31
castaway Avatar answered Oct 15 '22 07:10

castaway