Backtrace.pm

filter: Statementstmt 98.4% 62 / 63 Branchbran 95.0% 19 / 20 Conditioncond 75.0% 6 / 8 MC/DCmcdc 60.0% 3 / 5 Subroutinesub 100% 13 / 13 Podpod 100% 7 / 7 totaltotal 94.8% 110 / 116 CC 15 SCAR 13.0
CC
15
Cov
96%
new
7.0
skipme
4.0
skipmysubs
4.0
CRAP 3.7
« Backtrace/Point.pm ↑ Summary
1 package Devel::Backtrace;
2 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use Devel::Backtrace::Point;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 6 ▶use overload '""' => \&to_string;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 Devel::Backtrace - Object-oriented backtrace
12  
13 =head1 VERSION
14  
15 This is version 0.12.
16  
17 =cut
18  
19 6 our $VERSION = '0.12';
20  
21 =head1 SYNOPSIS
22  
23 my $backtrace = Devel::Backtrace->new;
24  
25 print $backtrace; # use automatic stringification
26 # See EXAMPLES to see what the output might look like
27  
28 print $backtrace->point(0)->line;
29  
30 =head1 METHODS
31  
32 =head2 Devel::Backtrace->new()
33  
34 Optional parameters: -start => $start, -format => $format
35  
36 If only one parameter is given, it will be used as $start.
37  
38 Constructs a new C<Devel::Backtrace> which is filled with all the information
39 C<caller($i)> provides, where C<$i> starts from C<$start>. If no argument is
40 given, C<$start> defaults to 0.
41  
42 If C<$start> is 1 (or higher), the backtrace won't contain the information that
43 (and where) Devel::Backtrace::new() was called.
44  
45 =cut
46  
47 11 ▶sub new {
Subroutine new: called CC=7 SCAR=19.5
Pod documented
48 11 my $class = shift;
49 11 my (@opts) = @_;
50  
51 11 my $start;
52 my %pointopts;
53  
54 11 ▶ if (1 == @opts) {
Branch: 1 branch
BranchTrueFalseTotal
if (1 == @opts) 4 7 11
55 4 $start = shift @opts;
56 }
57 11 ▶ while (my $opt = shift @opts) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $opt = shift @opts) 3 11 14
58 3 ▶ if ('-format' eq $opt) {
Branch: 2 branches
BranchTrueFalseTotal
if ('-format' eq $opt) { } 1 2 3
elsif ('-start' eq $opt) { } 2 0 2
59 1 $pointopts{$opt} = shift @opts;
60 } elsif ('-start' eq $opt) {
61 2 $start = shift @opts;
62 } else {
63 0 croak "Unknown option $opt";
64 }
65 }
66  
67 11 ▶ if (defined $start) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $start) { } 6 5 11
68 6 $pointopts{'-skip'} = $start;
69 } else {
70 5 $start = 0;
71 }
72  
73 11 my @backtrace;
74 11 ▶ for (my $deep = $start; my @caller = caller($deep); ++$deep) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@caller) = caller $deep) 45 11 56
75 45 push @backtrace, Devel::Backtrace::Point->new(
76 \@caller,
77 -level => $deep,
78 %pointopts,
79 );
80 }
81  
82 11 return bless \@backtrace, $class;
83 }
84  
85 =head2 $backtrace->point($i)
86  
87 Returns the i'th tracepoint as a L<Devel::Backtrace::Point> object (see its documentation
88 for how to access every bit of information).
89  
90 Note that the following code snippet will print the information of
91 C<caller($start+$i)>:
92  
93 print Devel::Backtrace->new($start)->point($i)
94  
95 =cut
96  
97 36 ▶sub point {
Subroutine point: called CC=1 SCAR=0.0
Pod documented
98 36 my $this = shift;
99 36 my ($i) = @_;
100 36 return $this->[$i];
101 }
102  
103 =head2 $backtrace->points()
104  
105 Returns a list of all tracepoints. In scalar context, the number of
106 tracepoints is returned.
107  
108 =cut
109  
110 16 ▶sub points {
Subroutine points: called CC=1 SCAR=0.0
Pod documented
111 16 my $this = shift;
112 16 return @$this;
113 }
114  
115 =head2 $backtrace->skipme([$package])
116  
117 This method deletes all leading tracepoints that contain information about calls
118 within C<$package>. Afterwards the C<$backtrace> will look as though it had
119 been created with a higher value of C<$start>.
120  
121 If the optional parameter C<$package> is not given, it defaults to the calling
122 package.
123  
124 The effect is similar to what the L<Carp> module does.
125  
126 This module ships with an example "skipme.pl" that demonstrates how to use this
127 method. See also L</EXAMPLES>.
128  
129 =cut
130  
131 5 ▶sub skipme {
Subroutine skipme: called CC=4 SCAR=13.9
Pod documented
132 5 my $this = shift;
133 5 ▶ my $package = @_ ? $_[0] : caller;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 4 1 5
134  
135 5 my $skip = 0;
136 5 my $skipped;
137 5 ▶ while (@$this and $package eq $this->point(0)->package) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$this and $package eq $this->point(0)->package) 10 5 15
Condition66%2/3
@$this and $package eq $this->point(0)->package
!ll&&!rl&&r
0510
MC/DC50%1/2
@$this and $package eq $this->point(0)->package
@$this $package eq $this->point(0)->package
Truth table66%2/3
A and B
A= @$thisB= $package eq $this->point(0)->package
ABresult
0X0
100
111
138 10 $skipped = shift @$this;
139 10 $skip++;
140 }
141 5 $this->_adjustskip($skip);
142 5 return $skipped;
143 }
144  
145 9 ▶sub _adjustskip {
Subroutine _adjustskip: called CC=3 SCAR=11.0
146 9 my ($this, $newskip) = @_;
147  
148 9 ▶ $_->_skip($newskip + ($_->_skip || 0)) for $this->points;
Condition100%2/2
$_->_skip || 0
l!l
2510
MC/DC100%1/1
$_->_skip || 0
$_->_skip
Truth table100%2/2
A
A= $_->_skip
Aresult
11
00
149 }
150  
151 =head2 $backtrace->skipmysubs([$package])
152  
153 This method is like C<skipme> except that it deletes calls I<to> the package
154 rather than calls I<from> the package.
155  
156 Before discarding those calls, C<skipme> is called. This is because usually
157 the topmost call in the stack is to Devel::Backtrace->new, which would not be
158 catched by C<skipmysubs> otherwise.
159  
160 This means that skipmysubs usually deletes more lines than skipme would.
161  
162 C<skipmysubs> was added in Devel::Backtrace version 0.06.
163  
164 See also L</EXAMPLES> and the example "skipme.pl".
165  
166 =cut
167  
168 4 ▶sub skipmysubs {
Subroutine skipmysubs: called CC=4 SCAR=13.9
Pod documented
169 4 my $this = shift;
170 4 ▶ my $package = @_ ? $_[0] : caller;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 2 2 4
171  
172 4 my $skipped = $this->skipme($package);
173 4 my $skip = 0;
174 4 ▶ while (@$this and $package eq $this->point(0)->called_package) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$this and $package eq $this->point(0)->called_package) 4 4 8
Condition66%2/3
@$this and $package eq $this->point(0)->called_package
!ll&&!rl&&r
044
MC/DC50%1/2
@$this and $package eq $this->point(0)->called_package
@$this $package eq $this->point(0)->called_package
Truth table66%2/3
A and B
A= @$thisB= $package eq $this->point(0)->called_package
ABresult
0X0
100
111
175 4 $skipped = shift @$this;
176 4 $skip++;
177 }
178 4 $this->_adjustskip($skip);
179 4 return $skipped;
180 }
181  
182 =head2 $backtrace->to_string()
183  
184 Returns a string that contains one line for each tracepoint. It will contain
185 the information from C<Devel::Backtrace::Point>'s to_string() method. To get
186 more information, use the to_long_string() method.
187  
188 Note that you don't have to call to_string() if you print a C<Devel::Backtrace>
189 object or otherwise treat it as a string, as the stringification operator is
190 overloaded.
191  
192 See L</EXAMPLES>.
193  
194 =cut
195  
196 4 ▶sub to_string {
Subroutine to_string: called CC=1 SCAR=0.0
Pod documented
197 4 my $this = shift;
198 4 return join '', map "$_\n", $this->points;
199 }
200  
201  
202 =head2 $backtrace->to_long_string()
203  
204 Returns a very long string that contains several lines for each trace point.
205 The result will contain every available bit of information. See
206 L<Devel::Backtrace::Point/to_long_string> for an example of what the result
207 looks like.
208  
209 =cut
210  
211 1 ▶sub to_long_string {
Subroutine to_long_string: called CC=1 SCAR=0.0
Pod documented
212 1 my $this = shift;
213 1 return join "\n", map $_->to_long_string, $this->points;
214 }
215  
216  
217 6 1
218 __END__
« Backtrace/Point.pm ↑ Summary