Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Automatic stack traces for warns and dies

by Aristotle (Chancellor)
on Jul 31, 2004 at 19:39 UTC ( #378967=note: print w/replies, xml ) Need Help??


in reply to Automatic stack traces for warns and dies

use Carp qw(confess cluck); BEGIN { *CORE::GLOBAL::warn = \&cluck; *CORE::GLOBAL::die = \&confess; }

(Note the compile time override; this is important.)

Makeshifts last the longest.

Replies are listed 'Best First'.
Re^2: Automatic stack traces for warns and dies
by fergal (Chaplain) on Jul 31, 2004 at 20:32 UTC
    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.

      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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://378967]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2020-11-27 12:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?