Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

comment on

( #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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others exploiting the Monastery: (13)
    As of 2021-04-16 12:28 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found

      Notices?