/usr/share/perl5/Devel/Backtrace.pm is in libdevel-backtrace-perl 0.12-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 | package Devel::Backtrace;
use strict;
use warnings;
use Devel::Backtrace::Point;
use Carp;
use overload '""' => \&to_string;
=head1 NAME
Devel::Backtrace - Object-oriented backtrace
=head1 VERSION
This is version 0.12.
=cut
our $VERSION = '0.12';
=head1 SYNOPSIS
my $backtrace = Devel::Backtrace->new;
print $backtrace; # use automatic stringification
# See EXAMPLES to see what the output might look like
print $backtrace->point(0)->line;
=head1 METHODS
=head2 Devel::Backtrace->new()
Optional parameters: -start => $start, -format => $format
If only one parameter is given, it will be used as $start.
Constructs a new C<Devel::Backtrace> which is filled with all the information
C<caller($i)> provides, where C<$i> starts from C<$start>. If no argument is
given, C<$start> defaults to 0.
If C<$start> is 1 (or higher), the backtrace won't contain the information that
(and where) Devel::Backtrace::new() was called.
=cut
sub new {
my $class = shift;
my (@opts) = @_;
my $start;
my %pointopts;
if (1 == @opts) {
$start = shift @opts;
}
while (my $opt = shift @opts) {
if ('-format' eq $opt) {
$pointopts{$opt} = shift @opts;
} elsif ('-start' eq $opt) {
$start = shift @opts;
} else {
croak "Unknown option $opt";
}
}
if (defined $start) {
$pointopts{'-skip'} = $start;
} else {
$start = 0;
}
my @backtrace;
for (my $deep = $start; my @caller = caller($deep); ++$deep) {
push @backtrace, Devel::Backtrace::Point->new(
\@caller,
-level => $deep,
%pointopts,
);
}
return bless \@backtrace, $class;
}
=head2 $backtrace->point($i)
Returns the i'th tracepoint as a L<Devel::Backtrace::Point> object (see its documentation
for how to access every bit of information).
Note that the following code snippet will print the information of
C<caller($start+$i)>:
print Devel::Backtrace->new($start)->point($i)
=cut
sub point {
my $this = shift;
my ($i) = @_;
return $this->[$i];
}
=head2 $backtrace->points()
Returns a list of all tracepoints. In scalar context, the number of
tracepoints is returned.
=cut
sub points {
my $this = shift;
return @$this;
}
=head2 $backtrace->skipme([$package])
This method deletes all leading tracepoints that contain information about calls
within C<$package>. Afterwards the C<$backtrace> will look as though it had
been created with a higher value of C<$start>.
If the optional parameter C<$package> is not given, it defaults to the calling
package.
The effect is similar to what the L<Carp> module does.
This module ships with an example "skipme.pl" that demonstrates how to use this
method. See also L</EXAMPLES>.
=cut
sub skipme {
my $this = shift;
my $package = @_ ? $_[0] : caller;
my $skip = 0;
my $skipped;
while (@$this and $package eq $this->point(0)->package) {
$skipped = shift @$this;
$skip++;
}
$this->_adjustskip($skip);
return $skipped;
}
sub _adjustskip {
my ($this, $newskip) = @_;
$_->_skip($newskip + ($_->_skip || 0)) for $this->points;
}
=head2 $backtrace->skipmysubs([$package])
This method is like C<skipme> except that it deletes calls I<to> the package
rather than calls I<from> the package.
Before discarding those calls, C<skipme> is called. This is because usually
the topmost call in the stack is to Devel::Backtrace->new, which would not be
catched by C<skipmysubs> otherwise.
This means that skipmysubs usually deletes more lines than skipme would.
C<skipmysubs> was added in Devel::Backtrace version 0.06.
See also L</EXAMPLES> and the example "skipme.pl".
=cut
sub skipmysubs {
my $this = shift;
my $package = @_ ? $_[0] : caller;
my $skipped = $this->skipme($package);
my $skip = 0;
while (@$this and $package eq $this->point(0)->called_package) {
$skipped = shift @$this;
$skip++;
}
$this->_adjustskip($skip);
return $skipped;
}
=head2 $backtrace->to_string()
Returns a string that contains one line for each tracepoint. It will contain
the information from C<Devel::Backtrace::Point>'s to_string() method. To get
more information, use the to_long_string() method.
Note that you don't have to call to_string() if you print a C<Devel::Backtrace>
object or otherwise treat it as a string, as the stringification operator is
overloaded.
See L</EXAMPLES>.
=cut
sub to_string {
my $this = shift;
return join '', map "$_\n", $this->points;
}
=head2 $backtrace->to_long_string()
Returns a very long string that contains several lines for each trace point.
The result will contain every available bit of information. See
L<Devel::Backtrace::Point/to_long_string> for an example of what the result
looks like.
=cut
sub to_long_string {
my $this = shift;
return join "\n", map $_->to_long_string, $this->points;
}
1
__END__
=head1 EXAMPLES
A sample stringification might look like this:
Devel::Backtrace::new called from MyPackage (foo.pl:30)
MyPackage::test2 called from MyPackage (foo.pl:28)
MyPackage::test1 called from main (foo.pl:18)
main::bar called from main (foo.pl:6)
main::foo called from main (foo.pl:13)
If MyPackage called skipme, the first two lines would be removed. If it called
skipmysubs, the first three lines would be removed.
If you don't like the format, you can change it:
my $backtrace = Devel::Backtrace->new(-format => '%I. %s');
This would produce a stringification of the following form:
0. Devel::Backtrace::new
1. MyPackage::test2
2. MyPackage::test1
3. main::bar
4. main::foo
=head1 SEE ALSO
L<Devel::StackTrace> does mostly the same as this module. I'm afraid I hadn't
noticed it until I uploaded this module.
L<Carp::Trace> is a simpler module which gives you a backtrace in string form.
L<Devel::DollarAt> comes with this distribution and is a nice application of
this module. You can use it for debugging to get a backtrace out of $@.
=head1 AUTHOR
Christoph Bussenius <pepe@cpan.org>
If you use this module, I'll be glad if you drop me a note.
You should mention this module's name in the subject of your mails, in order to
make sure they won't get lost in all the spam.
=head1 LICENSE
This module is in the public domain.
If your country's law does not allow this module being in the public
domain or does not include the concept of public domain, you may use the
module under the same terms as perl itself.
=cut
|