1Devel::StackTrace(3) User Contributed Perl Documentation Devel::StackTrace(3)
2
3
4
6 Devel::StackTrace - An object representing a stack trace
7
9 version 1.30
10
12 use Devel::StackTrace;
13
14 my $trace = Devel::StackTrace->new;
15
16 print $trace->as_string; # like carp
17
18 # from top (most recent) of stack to bottom.
19 while (my $frame = $trace->next_frame) {
20 print "Has args\n" if $frame->hasargs;
21 }
22
23 # from bottom (least recent) of stack to top.
24 while (my $frame = $trace->prev_frame) {
25 print "Sub: ", $frame->subroutine, "\n";
26 }
27
29 The Devel::StackTrace module contains two classes, Devel::StackTrace
30 and Devel::StackTrace::Frame. The goal of this object is to
31 encapsulate the information that can found through using the caller()
32 function, as well as providing a simple interface to this data.
33
34 The Devel::StackTrace object contains a set of Devel::StackTrace::Frame
35 objects, one for each level of the stack. The frames contain all the
36 data available from "caller()".
37
38 This code was created to support my Exception::Class::Base class (part
39 of Exception::Class) but may be useful in other contexts.
40
42 When describing the methods of the trace object, I use the words 'top'
43 and 'bottom'. In this context, the 'top' frame on the stack is the
44 most recent frame and the 'bottom' is the least recent.
45
46 Here's an example:
47
48 foo(); # bottom frame is here
49
50 sub foo {
51 bar();
52 }
53
54 sub bar {
55 Devel::StackTrace->new; # top frame is here.
56 }
57
59 · Devel::StackTrace->new(%named_params)
60
61 Returns a new Devel::StackTrace object.
62
63 Takes the following parameters:
64
65 · frame_filter => $sub
66
67 By default, Devel::StackTrace will include all stack frames
68 before the call to its its constructor.
69
70 However, you may want to filter out some frames with more
71 granularity than 'ignore_package' or 'ignore_class' allow.
72
73 You can provide a subroutine which is called with the raw
74 frame data for each frame. This is a hash reference with
75 two keys, "caller", and "args", both of which are array
76 references. The "caller" key is the raw data as returned by
77 Perl's "caller()" function, and the "args" key are the
78 subroutine arguments found in @DB::args.
79
80 The filter should return true if the frame should be
81 included, or false if it should be skipped.
82
83 · ignore_package => $package_name OR \@package_names
84
85 Any frames where the package is one of these packages will
86 not be on the stack.
87
88 · ignore_class => $package_name OR \@package_names
89
90 Any frames where the package is a subclass of one of these
91 packages (or is the same package) will not be on the stack.
92
93 Devel::StackTrace internally adds itself to the
94 'ignore_package' parameter, meaning that the
95 Devel::StackTrace package is ALWAYS ignored. However, if
96 you create a subclass of Devel::StackTrace it will not be
97 ignored.
98
99 · no_refs => $boolean
100
101 If this parameter is true, then Devel::StackTrace will not
102 store references internally when generating stacktrace
103 frames. This lets your objects go out of scope.
104
105 Devel::StackTrace replaces any references with their
106 stringified representation.
107
108 · no_args => $boolean
109
110 If this parameter is true, then Devel::StackTrace will not
111 store caller arguments in stack trace frames at all.
112
113 · respect_overload => $boolean
114
115 By default, Devel::StackTrace will call
116 "overload::AddrRef()" to get the underlying string
117 representation of an object, instead of respecting the
118 object's stringification overloading. If you would prefer
119 to see the overloaded representation of objects in stack
120 traces, then set this parameter to true.
121
122 · max_arg_length => $integer
123
124 By default, Devel::StackTrace will display the entire
125 argument for each subroutine call. Setting this parameter
126 causes truncates each subroutine argument's string
127 representation if it is longer than this number of
128 characters.
129
130 · message => $string
131
132 By default, Devel::StackTrace will use 'Trace begun' as the
133 message for the first stack frame when you call
134 "as_string". You can supply an alternative message using
135 this option.
136
137 · indent => $boolean
138
139 If this parameter is true, each stack frame after the first
140 will start with a tab character, just like
141 "Carp::confess()".
142
143 · $trace->next_frame
144
145 Returns the next Devel::StackTrace::Frame object down on the stack.
146 If it hasn't been called before it returns the first frame. It
147 returns undef when it reaches the bottom of the stack and then
148 resets its pointer so the next call to "next_frame" or "prev_frame"
149 will work properly.
150
151 · $trace->prev_frame
152
153 Returns the next Devel::StackTrace::Frame object up on the stack.
154 If it hasn't been called before it returns the last frame. It
155 returns undef when it reaches the top of the stack and then resets
156 its pointer so pointer so the next call to "next_frame" or
157 "prev_frame" will work properly.
158
159 · $trace->reset_pointer
160
161 Resets the pointer so that the next call "next_frame" or
162 "prev_frame" will start at the top or bottom of the stack, as
163 appropriate.
164
165 · $trace->frames
166
167 When this method is called with no arguments, it returns a list of
168 Devel::StackTrace::Frame objects. They are returned in order from
169 top (most recent) to bottom.
170
171 This method can also be used to set the object's frames if you pass
172 it a list of Devel::StackTrace::Frame objects objects.
173
174 This is useful if you want to filter the list of frames in ways
175 that are more complex than can be handled by "filter_frames":
176
177 $stacktrace->frames( my_filter( $stacktrace->frames() ) );
178
179 · $trace->frame ($index)
180
181 Given an index, returns the relevant frame or undef if there is not
182 frame at that index. The index is exactly like a Perl array. The
183 first frame is 0 and negative indexes are allowed.
184
185 · $trace->frame_count
186
187 Returns the number of frames in the trace object.
188
189 · $trace->as_string(\%p)
190
191 Calls as_string on each frame from top to bottom, producing output
192 quite similar to the Carp module's cluck/confess methods.
193
194 The optional "\%p" parameter only has one useful option. The
195 "max_arg_length" parameter truncates each subroutine argument's
196 string representation if it is longer than this number of
197 characters.
198
200 Please submit bugs to the CPAN RT system at
201 http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Devel%3A%3AStackTrace or
202 via email at bug-devel-stacktrace@rt.cpan.org.
203
205 Dave Rolsky <autarch@urth.org>
206
208 This software is Copyright (c) 2012 by Dave Rolsky.
209
210 This is free software, licensed under:
211
212 The Artistic License 2.0 (GPL Compatible)
213
214
215
216perl v5.16.3 2012-11-20 Devel::StackTrace(3)