How can I replace all 'die's with 'confess' in a Perl application?

Viewed 3699

I'm working in a large Perl application and would like to get stack traces every time 'die' is called. I'm aware of the Carp module, but I would prefer not to search/replace every instance of 'die' with 'confess'. In addition, I would like full stack traces for errors in Perl modules or the Perl interpreter itself, and obviously I can't change those to use Carp.

So, is there a way for me to modify the 'die' function at runtime so that it behaves like 'confess'? Or, is there a Perl interpreter setting that will throw full stack traces from 'die'?

4 Answers

I usually only want to replace the dies in a bit of code, so I localize the __DIE__ handler:

 {
 use Carp;
 local $SIG{__DIE__} = \&Carp::confess;

 ....
 }

As a development tool this can work, but some modules play tricks with this to get their features to work. Those features may break in odd ways when you override the handler they were expecting. It's not a good practice, but it happens sometimes.

Related