Beefy Boxes and Bandwidth Generously Provided by pair Networks Russ
good chemistry is complicated,
and a little bit messy-LW
 
PerlMonks

Forcing stack trace?

by Anonymous Monk
 | Log in | Create a new user | The Monastery Gates | Super Search | 
 | Seekers of Perl Wisdom | Meditations | PerlMonks Discussion | 
 | Obfuscation | Reviews | Cool Uses For Perl | Perl News | Q&A | Tutorials | 
 | Poetry | Recent Threads | Newest Nodes | Donate | What's New | 

on Sep 21, 2007 at 09:43 UTC ( #640319=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Can I tell perl to produce a stack trace, even when the program doesn't use carp, cluck, et al? This seems to be the default behaviour in other languages like Ruby and Python.

Actually I was trying to install RT and was getting the fatal error: "Can't locate object method "new" via package "RT::Handle" at ... line 218." It'd be nice if I can trace where this error came from.

Comment on Forcing stack trace?
Re: Forcing stack trace?
by Anonymous Monk on Sep 21, 2007 at 09:50 UTC
    Sorry, forgot to mention that I did manage to install RT, but just still wondering abuot the stack trace thing. :-)
Re: Forcing stack trace?
by fenLisesi (Curate) on Sep 21, 2007 at 10:12 UTC
    For your own modules, it easy to write your own stack trace dumping utility using caller. Also see Exception::Class. I don't know what you can do to get dumps from other people's modules, except to hack their source or use the debugger. Cheers.

      You can always roll you own for everything, but it would be much better if the language facilitates it by default. If I don't want it, i can easily catch it.

Re: Forcing stack trace?
by andreas1234567 (Vicar) on Sep 21, 2007 at 10:25 UTC
    I frequently use something like this:
    use strict; use warnings; use Log::Log4perl qw(:easy); Log::Log4perl->easy_init($WARN); my $log = Log::Log4perl->get_logger('stack'); sub show_call_stack { my ( $path, $line, $subr ); my $max_depth = 30; my $i = 1; if ($log->is_warn()) { $log->warn("--- Begin stack trace ---"); while ( (my @call_details = (caller($i++))) && ($i<$max_depth) ) { $log->warn("$call_details[1] line $call_details[2] in function $ +call_details[3]"); } $log->warn("--- End stack trace ---"); } } sub tze { show_call_stack(); } sub bar { return tze();} sub foo { return bar(); } foo(); __END__ $ perl -w 640319.pl 2007/09/21 12:23:10 --- Begin stack trace --- 2007/09/21 12:23:10 640319.pl line 22 in function main::tze 2007/09/21 12:23:10 640319.pl line 23 in function main::bar 2007/09/21 12:23:10 640319.pl line 25 in function main::foo 2007/09/21 12:23:10 --- End stack trace ---
    --
    Andreas
Re: Forcing stack trace?
by DrHyde (Parson) on Sep 21, 2007 at 10:45 UTC
    print Devel::StackTrace->new()->as_string();
Re: Forcing stack trace?
by tinita (Vicar) on Sep 21, 2007 at 11:29 UTC
    Can I tell perl to produce a stack trace, even when the program doesn't use carp, cluck, et al?
    i'm not sure what do you mean by that.
    use Carp qw(longmess); print longmess();
    works for me.
      From what I understand, stack trace can be printed if we use the Carp module (e.g. confess). But suppose other people's code just use die() and not use Carp at all?
        You can override the global __DIE__ handler something like (untested):
        use Carp; $SIG{ __DIE__ } = sub { Carp::confess( @_ ) };

        See perlvar for more info on %SIG.

Re: Forcing stack trace?
by educated_foo (Priest) on Sep 22, 2007 at 06:11 UTC
    Wow... not many helpful responses. I assume your program is exiting from a call to die? You can get a stack trace by overriding it, like so:
    *CORE::GLOBAL::die = sub { require Carp; Carp::confess };

Login:
Password
remember me
What's my password?
Create A New User

Node Status?
node history
Node Type: perlquestion [id://640319]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (14)
GrandFather
atcroft
mr_mischief
CardinalNumber
herveus
jaldhar
MidLifeXis
muba
ssandv
djp
AndyZaft
Neighbour
im2
BourneID
As of 2010-09-06 03:22 GMT
Sections?
Seekers of Perl Wisdom
Cool Uses for Perl
Meditations
PerlMonks Discussion
Categorized Q&A
Tutorials
Obfuscated Code
Perl Poetry
Perl News
See About the sections of PerlMonks
Information?
PerlMonks FAQ
Guide to the Monastery
What's New at PerlMonks
Voting/Experience System
Tutorials
Reviews
Library
Perl FAQs
Other Info Sources
Find Nodes?
Nodes You Wrote
Super Search
List Nodes By Users
Newest Nodes
Recently Active Threads
Selected Best Nodes
Best Nodes
Worst Nodes
Saints in our Book
Leftovers?
The St. Larry Wall Shrine
Offering Plate
Awards
Craft
Snippets Section
Code Catacombs
Quests
Editor Requests
Buy PerlMonks Gear
PerlMonks Merchandise
Planet Perl
Perlsphere
Use Perl
Perl.com
Perl 5 Wiki
Perl Jobs
Perl Mongers
Perl Directory
Perl documentation
CPAN
Random Node
Voting Booth?

My favourite poll on PerlMonks is ...

Your first Perl Book - the first one ever
Average number of caffeinated beverages per work day - the poll with the highest participation
My Thoughts on the New Voting/Experience System - the poll with the fewest votes cast
When I grow up, I want to be: - one of the polls with the fewest options
Perl 6 will primarily be: - the first one on Perl6
When I see a poll - one of the many polls about polls
this poll ;-)
yet to come
none - I hate polls. Bah.
some other

Results (95 votes), past polls