9.5. Confession Is Good for the Soul

Sometimes your program dies not in your code but in some module you call—suicide by association if you will. A module that wants to die is supposed to call Carp::croak instead, because croak reports the location of death as the place the module function was called, not the line of code in the module function containing the croak call.

Some modules don't do this. Either they call die, or they call another module that croaks; either way, you're left with an error message that doesn't tell you which line of your program caused its untimely demise.

If this happens, here's a quick way to force it to 'fess up. Near the beginning of your program, insert the following:

use Carp qw(cluck verbose);
$SIG{__DIE__}  = sub { confess @_ };
$SIG{__WARN__} = sub { cluck @_ };

Now whenever your program calls die/croak or carp/warn it ends up calling confess or cluck[9] instead, which have exactly the same effect except they print a stack trace of how they were called that goes all the way back to your main program.

[9] Perl is nothing if not whimsical. Presumably in this case it helps to dispel the air of morbidity surrounding the whole topic of program death.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset