http://qs321.pair.com?node_id=378978


in reply to Re: Automatic stack traces for warns and dies
in thread Automatic stack traces for warns and dies

I think that will also mess up anything which is doing an eval and then checking $@ afterwards.

I should have been more clear in my question, I'm not asking "how do I do this?", I already know how to do it. I'm asking "is this readily available already?" because it's pretty amazing that after all these years, there is no standard, easy way to force Perl to give a stacktrace when it dies.

In particular I want to be able to say to someone who's using my code and having problems, "run it again with perl -MDevel::Verbose and send me the output" and they'll be able to get it from CPAN and the code should work just as before.

  • Comment on Re^2: Automatic stack traces for warns and dies

Replies are listed 'Best First'.
Re^3: Automatic stack traces for warns and dies
by broquaint (Abbot) on Aug 01, 2004 at 11:16 UTC
    I think that will also mess up anything which is doing an eval and then checking $@ afterwards.
    Does it?
    use Carp qw(confess cluck); BEGIN { *CORE::GLOBAL::warn = \&cluck; *CORE::GLOBAL::die = \&confess; } sub foo { bar() } sub bar { print "in bar\n"; die "badness!" } eval { foo }; print "ack - $@" if $@; __output__ in bar ack - badness! at - line 8 main::bar() called at - line 7 main::foo() called at - line 10 eval {...} called at - line 10
    Same goes for throwing objects
    use Carp qw(confess cluck); BEGIN { *CORE::GLOBAL::warn = \&cluck; *CORE::GLOBAL::die = \&confess; } sub foo { bar() } sub bar { print "in bar\n";die( bless{akey=>"a string"} ) } use DDS; eval { foo }; print "ack: ", Dump($@) if ref $@; __output__ in bar ack: $main1 = bless( { akey => 'a string' }, 'main' );
    So Aristotle's solution should fit the bill.
    HTH

    _________
    broquaint

      The first example is probably OK as long as your check against $@ is both strict enough and liberal enough to not be confused by the stack trace. The second example has a bigger problem, eval {foo} works but foo() on it's own doesn't give a stack trace.

      The solution is not a one-liner, it needs to pay attention to $^S which indicates whether we're being eval()ed or not. It should also play well with other die/warn handlers. diagnostics does this. I'll submitting a small patch to add stack traces to warns and to allow a mode that turns off the verbose explanations, then I'll be happy.

        The second example has a bigger problem, eval {foo} works but foo() on it's own doesn't give a stack trace.
        It doesn't?
        use Carp qw(confess cluck); BEGIN { *CORE::GLOBAL::warn = \&cluck; *CORE::GLOBAL::die = \&confess; } sub foo { bar() } sub bar { print "in bar\n"; die "badness!" } foo(); __output__ in bar badness! at - line 8 main::bar() called at - line 7 main::foo() called at - line 10
        The solution is not a one-liner, it needs to pay attention to $^S which indicates whether we're being eval()ed or not.
        But this is dealt with by perl, so even with die and warn overridden the value of $^S will be unaffected.
        It should also play well with other die/warn handlers.
        And it does
        use Carp qw(confess cluck); BEGIN { *CORE::GLOBAL::warn = \&cluck; *CORE::GLOBAL::die = \&confess; } $SIG{__DIE__} = sub { print "I died: @_\n" }; $SIG{__WARN__} = sub { print "watch out: @_\n" }; sub foo { bar() } sub bar { print "in &bar\n"; warn "it's behind you!" } sub baz { qux() } sub qux { print "in &qux\n"; die "ack!" } eval { foo; baz; }; print "The End.\n"; __output__ in &bar watch out: it's behind you! at - line 12 main::bar() called at - line 11 main::foo() called at - line 17 eval {...} called at - line 17 in &qux I died: ack! at - line 15 main::qux() called at - line 14 main::baz() called at - line 17 eval {...} called at - line 17 The End.
        My point is, is that overriding die and warn with the corresponding calls from Carp should work fine as they inevitably call die and warn anyhow. The only problems you should run into with these overrides is in existing code where exception handling could very well get muddled, but this shouldn't be a problem if you use subs to localise this behaviour to a given package e.g
        { package Expressive; use Carp qw(confess cluck); use subs qw( die warn ); *die = \&confess; *warn = \&cluck; sub test { warn "warning in ".__PACKAGE__."::test\n"; die "dieing in ".__PACKAGE__."::test\n"; } } print "using Expressive::\n"; eval { Expressive::test }; print $@; print "\nusing main::\n"; eval { warn "warning in ".__PACKAGE__; die "dieing in ".__PACKAGE__; }; print $@; print "\ndone.\n"; __output__ using Expressive:: warning in Expressive::test Expressive::test() called at pmsopw_379099.pl line 18 eval {...} called at pmsopw_379099.pl line 18 dieing in Expressive::test Expressive::test() called at pmsopw_379099.pl line 18 eval {...} called at pmsopw_379099.pl line 18 using main:: warning in main at pmsopw_379099.pl line 23. dieing in main at pmsopw_379099.pl line 24. done.
        HTH

        _________
        broquaint