PPI/Exception.pm

filter: Statementstmt 81.4% 22 / 27 Branchbran 40.0% 4 / 10 Conditioncond 25.0% 1 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 87.5% 7 / 8 Podpod 100% 4 / 4 totaltotal 69.0% 38 / 55 CC 8 SCAR 14.0
CC
8
Cov
66%
callers
6.0
throw
5.6
new
5.2
CRAP 4.1
« PPI/Normal.pm ↑ Summary PPI/Token/Structure.pm »
1 package PPI::Exception;
2  
3 =head1 NAME
4  
5 PPI::Exception - The PPI exception base class
6  
7 =head1 SYNOPSIS
8  
9 use PPI::Exception;
10
11 my $e = PPI::Exception->new( 'something happened' );
12 $e->throw;
13  
14 PPI::Exception->new( message => 'something happened' )->throw;
15 PPI::Exception->throw( message => 'something happened' );
16  
17 =head1 DESCRIPTION
18  
19 All exceptions thrown from within PPI will be instances or derivations
20 of this class.
21  
22 =cut
23  
24 62 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 62 ▶use Params::Util qw{_INSTANCE};
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 62 ▶use vars qw{$VERSION};
Subroutine BEGIN: called CC=1 SCAR=0.0
28 BEGIN {
29 62 ▶ $VERSION = '1.236';
Subroutine BEGIN: called CC=1 SCAR=0.0
30 }
31  
32  
33 =head1 METHODS
34  
35 =head2 new $message | message => $message, ...
36  
37 Constructs and returns a new C<PPI::Exception> object.
38  
39 A message for the exception can be passed, either as a string
40 or as C<< message => $message >>. The message is available via the
41 C<message> method.
42  
43 =cut
44  
45 sub new {
46 3 ▶ my $class = shift;
Subroutine new: called CC=3 SCAR=16.5
Pod documented
47 3 ▶ return bless { @_ }, $class if @_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 3 0 3
48 0 ▶ return bless { message => $_[0] }, $class if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
49 0 return bless { message => 'Unknown Exception' }, $class;
50 }
51  
52  
53 =head2 throw
54  
55 If called on a C<PPI::Exception> object, throws the object.
56 If called on the class name, uses the arguments to construct a
57 C<PPI::Exception> and then throw it.
58  
59 Each time the object is thrown, information from the Perl <caller(0)>
60 call is saved and made available via the C<callers> method.
61  
62 This method never returns.
63  
64 =cut
65  
66 sub throw {
67 4 ▶ my $it = shift;
Subroutine throw: called CC=5 SCAR=17.3
Pod documented
68 4 ▶ if ( _INSTANCE($it, 'PPI::Exception') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&_INSTANCE($it, 'PPI::Exception')) { } 1 3 4
69 1 ▶ if ( $it->{callers} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($it->{'callers'}) { } 1 0 1
70 1 push @{ $it->{callers} }, [ caller(0) ];
71 } else {
72 0 ▶ $it->{callers} ||= [];
Condition0%0/2
$it->{"callers"} ||= []
l!l
00
MC/DC0%0/1
$it->{"callers"} ||= []
$it->{"callers"}
Truth table0%0/2
A
A= $it->{"callers"}
Aresult
11
00
73 }
74 } else {
75 3 ▶ my $message = $_[0] || 'Unknown Exception';
Condition50%1/2
$_[0] || 'Unknown Exception'
l!l
30
MC/DC0%0/1
$_[0] || 'Unknown Exception'
$_[0]
Truth table50%1/2
A
A= $_[0]
Aresult
11
00
76 3 $it = $it->new(
77 message => $message,
78 callers => [
79 [ caller(0) ],
80 ],
81 );
82 }
83 4 die $it;
84 }
85  
86  
87 =head2 message
88  
89 Returns the exception message passed to the object's constructor,
90 or a default message.
91  
92 =cut
93  
94 sub message {
95 4 ▶ $_[0]->{message};
Subroutine message: called CC=1 SCAR=0.0
Pod documented
96 }
97  
98  
99 =head2 callers
100  
101 Returns a listref, each element of which is a listref of C<caller(0)>
102 information. The returned listref can be empty.
103  
104 =cut
105  
106 sub callers {
107 0 ▶ @{ $_[0]->{callers} || [] };
Subroutine callers: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"callers"} or [] 0 0 0
108 }
109  
110  
111 62 1;
« PPI/Normal.pm ↑ Summary PPI/Token/Structure.pm »