tags:

views:

319

answers:

2

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__ } = sub { Carp::confess( @_ ) };
+6  A: 

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.

Mark Johnson
I don't know why I didn't think of it. It works.
daotoad
I must admit that I learned that trick from a co-worker.
Mark Johnson
+6  A: 

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

castaway