Devel::StackTrace - An object representing a stack trace
version 1.31
use Devel::StackTrace; my $trace = Devel::StackTrace->new(); print $trace->as_string(); # like carp # from top (most recent) of stack to bottom. while ( my $frame = $trace->next_frame() ) { print "Has args\n" if $frame->hasargs(); } # from bottom (least recent) of stack to top. while ( my $frame = $trace->prev_frame() ) { print "Sub: ", $frame->subroutine(), "\n"; }
The Devel::StackTrace module contains two classes, C,Devel::StackTrace> and Devel::StackTrace::Frame. These objects encapsulate the information that can retrieved via Perl's caller() function, as well as providing a simple interface to this data.
Devel::StackTrace
caller()
The Devel::StackTrace object contains a set of Devel::StackTrace::Frame objects, one for each level of the stack. The frames contain all the data available from caller().
Devel::StackTrace::Frame
This code was created to support my Exception::Class::Base class (part of Exception::Class) but may be useful in other contexts.
When describing the methods of the trace object, I use the words 'top' and 'bottom'. In this context, the 'top' frame on the stack is the most recent frame and the 'bottom' is the least recent.
Here's an example:
foo(); # bottom frame is here sub foo { bar(); } sub bar { Devel::StackTrace->new(); # top frame is here. }
This class provide the following methods:
Returns a new Devel::StackTrace object.
Takes the following parameters:
frame_filter => $sub
By default, Devel::StackTrace will include all stack frames before the call to its constructor.
However, you may want to filter out some frames with more granularity than 'ignore_package' or 'ignore_class' allow.
You can provide a subroutine which is called with the raw frame data for each frame. This is a hash reference with two keys, "caller", and "args", both of which are array references. The "caller" key is the raw data as returned by Perl's caller() function, and the "args" key are the subroutine arguments found in @DB::args.
@DB::args
The filter should return true if the frame should be included, or false if it should be skipped.
ignore_package => $package_name OR \@package_names
Any frames where the package is one of these packages will not be on the stack.
ignore_class => $package_name OR \@package_names
Any frames where the package is a subclass of one of these packages (or is the same package) will not be on the stack.
Devel::StackTrace internally adds itself to the 'ignore_package' parameter, meaning that the Devel::StackTrace package is ALWAYS ignored. However, if you create a subclass of Devel::StackTrace it will not be ignored.
no_refs => $boolean
If this parameter is true, then Devel::StackTrace will not store references internally when generating stacktrace frames. This lets your objects go out of scope.
Devel::StackTrace replaces any references with their stringified representation.
no_args => $boolean
If this parameter is true, then Devel::StackTrace will not store caller arguments in stack trace frames at all.
respect_overload => $boolean
By default, Devel::StackTrace will call overload::AddrRef() to get the underlying string representation of an object, instead of respecting the object's stringification overloading. If you would prefer to see the overloaded representation of objects in stack traces, then set this parameter to true.
overload::AddrRef()
max_arg_length => $integer
By default, Devel::StackTrace will display the entire argument for each subroutine call. Setting this parameter causes truncates each subroutine argument's string representation if it is longer than this number of characters.
message => $string
By default, Devel::StackTrace will use 'Trace begun' as the message for the first stack frame when you call as_string. You can supply an alternative message using this option.
as_string
indent => $boolean
If this parameter is true, each stack frame after the first will start with a tab character, just like Carp::confess().
Carp::confess()
Returns the next Devel::StackTrace::Frame object on the stack, going down. If this method hasn't been called before it returns the first frame. It returns undef when it reaches the bottom of the stack and then resets its pointer so the next call to $trace->next_frame() or $trace->prev_frame() will work properly.
undef
$trace->next_frame()
$trace->prev_frame()
Returns the next Devel::StackTrace::Frame object on the stack, going up. If this method hasn't been called before it returns the last frame. It returns undef when it reaches the top of the stack and then resets its pointer so the next call to $trace->next_frame() or $trace->prev_frame() will work properly.
Resets the pointer so that the next call to $trace->next_frame() or $trace->prev_frame() will start at the top or bottom of the stack, as appropriate.
When this method is called with no arguments, it returns a list of Devel::StackTrace::Frame objects. They are returned in order from top (most recent) to bottom.
This method can also be used to set the object's frames if you pass it a list of Devel::StackTrace::Frame objects.
This is useful if you want to filter the list of frames in ways that are more complex than can be handled by the $trace->filter_frames() method:
$trace->filter_frames()
$stacktrace->frames( my_filter( $stacktrace->frames() ) );
Given an index, this method returns the relevant frame, or undef if there is no frame at that index. The index is exactly like a Perl array. The first frame is 0 and negative indexes are allowed.
Returns the number of frames in the trace object.
Calls $frame->as_string() on each frame from top to bottom, producing output quite similar to the Carp module's cluck/confess methods.
$frame->as_string()
The optional \%p parameter only has one option. The max_arg_length parameter truncates each subroutine argument's string representation if it is longer than this number of characters.
\%p
max_arg_length
Please submit bugs to the CPAN RT system at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Devel%3A%3AStackTrace or via email at bug-devel-stacktrace@rt.cpan.org.
Dave Rolsky <autarch@urth.org>
This software is Copyright (c) 2014 by Dave Rolsky.
This is free software, licensed under:
The Artistic License 2.0 (GPL Compatible)
To install Devel::StackTrace, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Devel::StackTrace
CPAN shell
perl -MCPAN -e shell install Devel::StackTrace
For more information on module installation, please visit the detailed CPAN module installation guide.