0

Why doesn't use Carp qw(verbose); make die produce a stack trace? I mean that just

ERROR at ./test.pl line 8.

is printed, but I want also a stack trace.

#!/usr/bin/perl

use strict;
use warnings;

use Carp qw(verbose);

sub c { die "ERROR"; }

sub b {
  c;
}

sub a {
  b;
}

a;

3 Answers 3

5

Try using Devel::Confess. Usage:

perl -d:Confess myscript.pl

It makes die print a stack track.

4
  • 1
    And so does Carp::Always
    – ikegami
    Jun 6, 2014 at 11:56
  • Yep. The advantage of Devel::Confess is that it even adds stack traces to blessed exception objects. Carp::Always doesn't; it should be called Carp::NearlyAlways. :-)
    – tobyink
    Jun 6, 2014 at 16:17
  • ow, so you're saying Devel::Confess breaks code that throws objects?
    – ikegami
    Jun 6, 2014 at 16:32
  • 1
    Nope - it's cleverer than that. It subclasses whatever class the object was blessed into, adding a stack trace, reblesses the exception into the new subclass, and rethrows it.
    – tobyink
    Jun 6, 2014 at 21:11
3

Of use Carp qw( verbose );, the documentation says:

As a debugging aid, you can force Carp to treat a croak as a confess and a carp as a cluck across all modules.

You don't use croak or carp, so use Carp qw( verbose ); is useless.

You can achieve what you want by overriding die or by creating a $SIG{__DIE__} handler. Carp::Always is a pre-made solution that does this for you.

3

Then you want confess

use strict;
use warnings;

use Carp qw(confess);

sub c { confess "ERROR"; }
sub b { c; }
sub a { b; }
a();

Outputs:

ERROR at confess.pl line 6.
        main::c() called at confess.pl line 7
        main::b() called at confess.pl line 8
        main::a() called at confess.pl line 9

And if you can't change your other code, you can use a $SIG{__DIE__}

use Carp qw(confess);

$SIG{__DIE__} = \&confess;

sub c { die "ERROR"; }
sub b { c; }
sub a { b; }
a();
3
  • 1
    No, it does not address my issue. I want die in a third-party module to print a stack trace WITHOUT modifying that module (that is it is not acceptable to replace die with croak or confess or whatever in that module, I can't modify the module)
    – porton
    Jun 6, 2014 at 8:48
  • It is not a fact of acceptable or not. Carp will not do what you want. Carp works as described in the documentation. So the answer is correct. Your code does not work because this is not how Carp was designed.
    – Matteo
    Jun 6, 2014 at 8:49
  • 1
    use Carp qw(croak) does not normally print a stack trace. It will normally only mention one line where the error occurred. use Carp qw(confess) can be used to get a stack trace, and croak can be made to act like confess using use Carp qw(croak verbose). The reason you get a stack trace from croak in your example is that croak normally uses package boundaries to determine which line number to report the error at; however in your example the entire stack is in the same package (main), so it cannot determine which line number to report the error at, so prints the entire stack.
    – tobyink
    Jun 6, 2014 at 8:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.