pileofrogs has asked for the wisdom of the Perl Monks concerning the following question:

Every now and again I run into a problem where something I do causes an external module to die in a really unhelpful way.

Is there a way to make a script print a helpful stack trace a-la confess or croak regardless of how the procress died? Would...

$SIG{__DIE__} = sub ( croak(shift) );
... make my computer blow up?

Thanks!
--Pileofrogs

Replies are listed 'Best First'.
Re: croakify non-croakers?
by moritz (Cardinal) on Nov 08, 2007 at 07:36 UTC
    I'd use this:
    use Carp qw(confess); $SIG{__DIE__} = \&confess;

    The choice whether to use croak or confess is yours, but croak hides information from you, confess doesn't. If the module might be buggy, confess is the better choice.

    Update: The Carp documentation mentions perl -MCarp=verbose script.pl to force stack traces, or setting $Carp::Verbose in the script to "upgrade" croak to confess, so the difference isn't all that big.

Re: croakify non-croakers?
by shmem (Chancellor) on Nov 08, 2007 at 08:26 UTC
    ... make my computer blow up?

    No, but it would make perl blow up, since a sub needs curlies:

    $SIG{__DIE__} = sub { croak(shift) };

    But you don't know whether @_ contains anything useful, if set at all, at the moment the die occurs. So you'll probably better off using Carp::confess, as moritz already pointed out.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

      I belive $SIG{__DIE__} is called by perl with the reformatted error message passed as an argument, so @_ does usually contain something useful.