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

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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


In reply to Re^5: Automatic stack traces for warns and dies by broquaint
in thread Automatic stack traces for warns and dies by fergal

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2024-04-16 06:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found