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

4

18

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

Jehiel answered 9/12, 2009 at 23:19 Comment(0)
I
32

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.

Indiscrimination answered 9/12, 2009 at 23:24 Comment(0)
B
14

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.

Bullring answered 9/12, 2009 at 23:24 Comment(2)
The modules I linked work by setting $SIG{__DIE__}, so yes, in a pinch you can do the same thing yourself. The modules just have a little extra code to make nicer output :)Indiscrimination
This is solution for home made error processing. You can do anything you need, like saving errors in file or post them by email.Desmoid
G
3

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.

Gasometer answered 10/12, 2009 at 18:0 Comment(0)
B
0

The Error module will convert all dies to Error::Simple objects, which contain a full stacktrace (the constructor parses the "at file... line.." text and creates a stack trace). You can use an arbitrary object (generally subclassed from Error::Simple) to handle errors with the $Error::ObjectifyCallback preference.

This is especially handy if you commonly throw around exceptions of other types to signal other events, as then you just add a handler for Error::Simple (or whatever other class you are using for errors) and have it dump its stacktrace or perform specialized logging depending on the type of error.

Beabeach answered 9/12, 2009 at 23:55 Comment(2)
Sometimes Error breaks someone else's exception-handling code and leads to a small debugging nightmare :)Indiscrimination
Yes, sadly it does. I've also had to recently abandon it as it likes to export with, which is incompatible with Moose. I've mostly concluded now that exceptions are evil. :)Beabeach

© 2022 - 2024 — McMap. All rights reserved.