Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Update 2:The latest bleaperl has a patch that allows
perl -Mdiagnostics=t-,-w badly_behaved_script
-w adds stack traces to warnigns and -t turns off the explanations. No need for a new module.

Update: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, except with much more info when it dies.

Most languages will give you a stack trace when they die or at least you can set a flag so that this happens, Perl lacks this feature and I couldn't find a module that provides it. So for quite a while now I have been using my own module that works like this

use Devel::Verbose; a_sub(); sub a_sub { will_warn(); print STDERR "-------------\n"; will_die(); } sub will_warn { warn("something funny"); } sub will_die { die("something awful"); }
this will give output something like
Trace begun at t.pl line 11
main::will_warn at t.pl line 5
main::a_sub at t.pl line 3
something funny at t.pl line 11.
-------------
Trace begun at t.pl line 14
main::will_die at t.pl line 7
main::a_sub at t.pl line 3
something awful at t.pl line 14.
It's very careful not to interefere with the original dieing and warning behaviour (it's not perfect in this respect but it's close).

Have I reinvented a wheel? Just checking before I post to CPAN. It's based on Devel::StackTrace and Exception::Class.

Carp does something similar but you have to explicitly use it in your code, whereas I can do perl -MDevel::Verbose some_script.pl and I will get full stack traces from all warn()s and die()s, without changing a line of code in some_script.pl.


In reply to 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 browsing the Monastery: (4)
As of 2024-04-19 13:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found