Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

perl

carp

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'?

like image 721
Mike Ottum Avatar asked Dec 09 '09 23:12

Mike Ottum


2 Answers

Use Devel::SimpleTrace or Carp::Always and they'll do what you're asking for without any hard work on your part. They have global effect, which means they can easily be added for just one run on the commandline using e.g. -MDevel::SimpleTrace.

like image 60
hobbs Avatar answered Oct 08 '22 17:10

hobbs


What about setting a __DIE__ signal handler? Something like

$SIG{__DIE__} = sub { Carp::confess @_ };

at the top of your script? See perlvar %SIG for more information.

like image 39
mob Avatar answered Oct 08 '22 17:10

mob