blib/lib/Exception/Lite.pm

filter: Statementstmt 68.6% 307 / 447 Branchbran 44.1% 91 / 206 Conditioncond 33.3% 10 / 30 MC/DCmcdc 11.1% 2 / 18 Subroutinesub 51.8% 57 / 110 Podpod 1.2% 1 / 82 totaltotal 52.4% 468 / 893 CC 115 SCAR 25.8
CC
115
Cov
60%
_dumpMessage
57.4
isException
56.0
declareExceptionClass
29.4
CRAP 13.2
↑ Summary
1 # Copyright (c) 2010 Elizabeth Grace Frank-Backman.
2 # All rights reserved.
3 # Liscenced under the "Artistic Liscence"
4 # (see http://dev.perl.org/licenses/artistic.html)
5  
6 1 ▶use 5.8.8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use overload;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 package Exception::Lite;
12 1 our @ISA = qw(Exporter);
13 1 our @EXPORT_OK=qw(declareExceptionClass isException isChainable
14 onDie onWarn);
15 1 our %EXPORT_TAGS
16 =( common => [qw(declareExceptionClass isException isChainable)]
17 , all => [@EXPORT_OK]
18 );
19 1 my $CLASS='Exception::Lite';
20  
21 #------------------------------------------------------------------
22  
23 1 our $STRINGIFY=3;
24 1 our $FILTER=1;
25 1 our $UNDEF='<undef>';
26 1 our $TAB=3;
27 1 our $LINE_LENGTH=120;
28  
29 # provide command line control over amount and layout of debugging
30 # information, e.g. perl -mException::Lite=STRINGIFY=4
31  
32 1 ▶sub import {
Subroutine import: called CC=6 SCAR=32.2
33 Exception::Lite->export_to_level(1, grep {
34 1 ▶ if (/^(\w+)=(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^(\w+)=(.*)$/) { } 0 2 2
35 0 my $k = $1;
36 0 my $v = $2;
37 0 ▶ if ($k eq 'STRINGIFY') { $STRINGIFY=$v;
Branch: 4 branches
BranchTrueFalseTotal
if ($k eq 'STRINGIFY') { } 0 0 0
elsif ($k eq 'FILTER') { } 0 0 0
elsif ($k eq 'LINE_LENGTH') { } 0 0 0
elsif ($k eq 'TAB') { } 0 0 0
38 0 } elsif ($k eq 'FILTER') { $FILTER=$v;
39 0 } elsif ($k eq 'LINE_LENGTH') { $LINE_LENGTH=$v;
40 0 } elsif ($k eq 'TAB') { $TAB=$v;
41 }
42 0 0;
43 } else {
44 2 1;
45 }
46 } @_);
47 }
48  
49 #------------------------------------------------------------------
50 # Note to source code divers: DO NOT USE THIS. This is intended for
51 # internal use but must be declared with "our" because we need to
52 # localize it. This is an implementation detail and cannot be relied
53 # on for future releases.
54  
55 1 our $STACK_OFFSET=0;
56  
57 #------------------------------------------------------------------
58  
59 1 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
60 1 ▶use constant EVAL => '(eval)';
Subroutine BEGIN: called CC=1 SCAR=0.0
61  
62 #==================================================================
63 # EXPORTABLE FUNCTIONS
64 #==================================================================
65  
66 7 ▶sub declareExceptionClass {
Subroutine declareExceptionClass: called CC=14 SCAR=33.8
Pod documented
67 7 my ($sClass, $sSuperClass, $xFormatRule, $bCustomizeSubclass) = @_;
68 7 my $sPath = $sClass; $sPath =~ s/::/\//g; $sPath .= '.pm';
69 7 ▶ if ($INC{$sPath}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($INC{$sPath}) 1 6 7
70 # we want to start with the caller's frame, not ours
71 1 local $STACK_OFFSET = $STACK_OFFSET + 1;
72 1 die 'Exception::Lite::Any'->new("declareExceptionClass failed: "
73 . "$sClass is already defined!");
74 0 return undef;
75 }
76  
77 6 my $sRef=ref($sSuperClass);
78 6 ▶ if ($sRef) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sRef) { } 1 5 6
79 1 $bCustomizeSubclass = $xFormatRule;
80 1 $xFormatRule = $sSuperClass;
81 1 $sSuperClass=undef;
82 } else {
83 5 $sRef = ref($xFormatRule);
84 5 ▶ if (!$sRef && defined($xFormatRule)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $sRef and defined $xFormatRule) 0 5 5
Condition66%2/3
not $sRef and defined $xFormatRule
!ll&&!rl&&r
230
MC/DC0%0/2
not $sRef and defined $xFormatRule
not $sRef defined $xFormatRule
Truth table66%2/3
not(A) and B
A= not $sRefB= defined $xFormatRule
ABresult
0X0
100
111
85 0 $bCustomizeSubclass = $xFormatRule;
86 0 $xFormatRule = undef;
87 }
88 }
89  
90 # set up things dependent on whether or not the class has a
91 # format string or expects a message for each instance
92  
93 6 my ($sLeadingParams, $sAddOrOmit, $sRethrowMsg, $sMakeMsg);
94 6 my $sReplaceMsg='';
95  
96 6 ▶ if ($sRef) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sRef) { } 3 3 6
97 3 $sLeadingParams='my $e; $e=shift if ref($_[0]);';
98 3 $sAddOrOmit='added an unnecessary message or format';
99 3 $sRethrowMsg='';
100  
101 #generate format rule
102 3 ▶ $xFormatRule=$xFormatRule->($sClass) if ($sRef eq 'CODE');
Branch: 1 branch
BranchTrueFalseTotal
if $sRef eq "CODE" 0 3 3
103  
104 3 my $sFormat= 'q{' . $xFormatRule->[0] . '}';
105 3 ▶ if (scalar($xFormatRule) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar $xFormatRule == 1) { } 0 3 3
106 0 $sMakeMsg='my $msg='.$sFormat;
107 } else {
108 3 my $sSprintf = 'Exception::Lite::_sprintf(' . $sFormat
109 . ', map {defined($_)?$_:\''. $UNDEF .'\'} @$h{qw('
110 . join(' ', @$xFormatRule[1..$#$xFormatRule]) . ')});';
111 3 $sMakeMsg='my $msg='.$sSprintf;
112 3 $sReplaceMsg='$_[0]->[0]='.$sSprintf;
113 }
114  
115 } else {
116 3 $sLeadingParams = 'my $e=shift; my $msg;'.
117 'if(ref($e)) { $msg=shift; $msg=$e->[0] if !defined($msg);}'.
118 'else { $msg=$e;$e=undef; }';
119 3 $sAddOrOmit='omitted a required message';
120 3 $sRethrowMsg='my $msg=shift; $_[0]->[0]=$msg if defined($msg);';
121 3 $sMakeMsg='';
122 }
123  
124 # put this in an eval so that it doesn't cause parse errors at
125 # compile time in no-threads versions of Perl
126  
127 6 my $sTid = eval q{defined(&threads::tid)?'threads->tid':'undef'};
128  
129 6 my $sDeclare = "package $sClass;".
130 'sub new { my $cl=shift;'. $sLeadingParams .
131 'my $st=Exception::Lite::_cacheStackTrace($e);'.
132 'my $h= Exception::Lite::_shiftProperties($cl' .
133 ',$st,"'.$sAddOrOmit.'",@_);' . $sMakeMsg .
134 'my $self=bless([$msg,$h,$st,$$,'.$sTid.',$e,[]],$cl);';
135  
136 # the remainder depends on the type of subclassing
137  
138 6 ▶ if ($bCustomizeSubclass) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bCustomizeSubclass) { } 1 5 6
139 1 $sDeclare .= '$self->[7]={}; $self->_new(); return $self; }'
140 . 'sub _p_getSubclassData { $_[0]->[7]; }';
141 } else {
142 5 $sDeclare .= 'return $self;}'.
143 'sub replaceProperties {'.
144 'my $h={%{$_[0]->[1]},%{$_[1]}}; $_[0]->[1]=$h;'.$sReplaceMsg.
145 '}'.
146 'sub rethrow {' .
147 'my $self=shift;' . $sRethrowMsg .
148 'Exception::Lite::_rethrow($self,"'.$sAddOrOmit.'",@_)' .
149 '}';
150  
151 5 ▶ unless (isExceptionClass($sSuperClass)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (isExceptionClass($sSuperClass)) 4 1 5
152 4 $sDeclare .=
153 'sub _getInterface { \'Exception::Lite\' }' .
154 'sub getMessage { $_[0]->[0] };' .
155 'sub getProperty { $_[0]->[1]->{$_[1]} }' .
156 'sub isProperty { exists($_[0]->[1]->{$_[1]})?1:0 }' .
157 'sub getStackTrace { $_[0]->[2] }' .
158 'sub getFrameCount { scalar(@{$_[0]->[2]}); }' .
159 'sub getFile { $_[0]->[2]->[ $_[1]?$_[1]:0 ]->[0] };' .
160 'sub getLine { $_[0]->[2]->[ $_[1]?$_[1]:0 ]->[1] };' .
161 'sub getSubroutine { $_[0]->[2]->[ $_[1]?$_[1]:0 ]->[2] };' .
162 'sub getArgs { $_[0]->[2]->[ $_[1]?$_[1]:0 ]->[3] };' .
163 'sub getPackage {$_[0]->[2]->[-1]->[2] =~ /(\w+)>$/;$1}'.
164 'sub getPid { $_[0]->[3] }' .
165 'sub getTid { $_[0]->[4] }' .
166 'sub getChained { $_[0]->[5] }' .
167 'sub getPropagation { $_[0]->[6]; }' .
168 'use overload '.
169 'q{""} => \&Exception::Lite::_dumpMessage ' .
170 ', q{0+} => \&Exception::Lite::_refaddr, fallback=>1;' .
171 'sub PROPAGATE { push @{$_[0]->[6]},[$_[1],$_[2]]; $_[0]}';
172 }
173 }
174 6 $sDeclare .= 'return 1;';
175  
176 0 ▶ local $SIG{__WARN__} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
177 0 my ($p,$f,$l) = caller(2);
178 0 my $s=$_[0]; $s =~ s/at \(eval \d+\)\s+line\s+\d+\.//m;
179 0 print STDERR "$s in declareExceptionClass($sClass,...) "
180 ."in file $f, line $l\n";
181 6 };
182  
183 0 ▶ eval $sDeclare or do {
Subroutine BEGIN: called CC=1 SCAR=1.0
Subroutine BEGIN: called CC=1 SCAR=1.0
Subroutine BEGIN: called CC=1 SCAR=1.0
Subroutine BEGIN: called CC=1 SCAR=1.0
Subroutine PROPAGATE: not called CC=1 SCAR=1.0
Subroutine PROPAGATE: called CC=1 SCAR=1.0
Subroutine PROPAGATE: not called CC=1 SCAR=1.0
Subroutine PROPAGATE: not called CC=1 SCAR=1.0
Subroutine _getInterface: not called CC=1 SCAR=1.0
Subroutine _getInterface: called CC=1 SCAR=1.0
Subroutine _getInterface: not called CC=1 SCAR=1.0
Subroutine _getInterface: not called CC=1 SCAR=1.0
Subroutine _p_getSubclassData: called CC=1 SCAR=1.0
Subroutine getArgs: not called CC=2 SCAR=8.8
Subroutine getArgs: not called CC=2 SCAR=8.8
Subroutine getArgs: not called CC=2 SCAR=8.8
Subroutine getArgs: not called CC=2 SCAR=8.8
Subroutine getChained: called CC=1 SCAR=1.0
Subroutine getChained: called CC=1 SCAR=1.0
Subroutine getChained: called CC=1 SCAR=1.0
Subroutine getChained: not called CC=1 SCAR=1.0
Subroutine getFile: not called CC=2 SCAR=8.8
Subroutine getFile: called CC=2 SCAR=8.8
Subroutine getFile: not called CC=2 SCAR=8.8
Subroutine getFile: not called CC=2 SCAR=8.8
Subroutine getFrameCount: not called CC=1 SCAR=1.0
Subroutine getFrameCount: not called CC=1 SCAR=1.0
Subroutine getFrameCount: not called CC=1 SCAR=1.0
Subroutine getFrameCount: not called CC=1 SCAR=1.0
Subroutine getLine: not called CC=2 SCAR=8.8
Subroutine getLine: called CC=2 SCAR=8.8
Subroutine getLine: not called CC=2 SCAR=8.8
Subroutine getLine: not called CC=2 SCAR=8.8
Subroutine getMessage: called CC=1 SCAR=1.0
Subroutine getMessage: called CC=1 SCAR=1.0
Subroutine getMessage: called CC=1 SCAR=1.0
Subroutine getMessage: not called CC=1 SCAR=1.0
Subroutine getPackage: not called CC=1 SCAR=1.0
Subroutine getPackage: called CC=1 SCAR=1.0
Subroutine getPackage: not called CC=1 SCAR=1.0
Subroutine getPackage: not called CC=1 SCAR=1.0
Subroutine getPid: called CC=1 SCAR=1.0
Subroutine getPid: called CC=1 SCAR=1.0
Subroutine getPid: called CC=1 SCAR=1.0
Subroutine getPid: not called CC=1 SCAR=1.0
Subroutine getPropagation: called CC=1 SCAR=1.0
Subroutine getPropagation: called CC=1 SCAR=1.0
Subroutine getPropagation: called CC=1 SCAR=1.0
Subroutine getPropagation: not called CC=1 SCAR=1.0
Subroutine getProperty: not called CC=1 SCAR=1.0
Subroutine getProperty: called CC=1 SCAR=1.0
Subroutine getProperty: not called CC=1 SCAR=1.0
Subroutine getProperty: not called CC=1 SCAR=1.0
Subroutine getStackTrace: called CC=1 SCAR=1.0
Subroutine getStackTrace: called CC=1 SCAR=1.0
Subroutine getStackTrace: called CC=1 SCAR=1.0
Subroutine getStackTrace: not called CC=1 SCAR=1.0
Subroutine getSubroutine: not called CC=2 SCAR=8.8
Subroutine getSubroutine: called CC=2 SCAR=8.8
Subroutine getSubroutine: not called CC=2 SCAR=8.8
Subroutine getSubroutine: not called CC=2 SCAR=8.8
Subroutine getTid: called CC=1 SCAR=1.0
Subroutine getTid: called CC=1 SCAR=1.0
Subroutine getTid: called CC=1 SCAR=1.0
Subroutine getTid: not called CC=1 SCAR=1.0
Subroutine isProperty: not called CC=2 SCAR=8.8
Subroutine isProperty: not called CC=2 SCAR=8.8
Subroutine isProperty: not called CC=2 SCAR=8.8
Subroutine isProperty: not called CC=2 SCAR=8.8
Subroutine new: called CC=3 SCAR=13.7
Subroutine new: called CC=3 SCAR=13.7
Subroutine new: called CC=3 SCAR=13.7
Subroutine new: called CC=3 SCAR=13.7
Subroutine new: called CC=3 SCAR=13.7
Subroutine new: called CC=3 SCAR=13.7
Subroutine replaceProperties: not called CC=1 SCAR=1.0
Subroutine replaceProperties: called CC=1 SCAR=1.0
Subroutine replaceProperties: called CC=1 SCAR=1.0
Subroutine replaceProperties: not called CC=2 SCAR=8.8
Subroutine replaceProperties: not called CC=2 SCAR=8.8
Subroutine rethrow: not called CC=2 SCAR=8.8
Subroutine rethrow: called CC=2 SCAR=8.8
Subroutine rethrow: called CC=2 SCAR=8.8
Subroutine rethrow: not called CC=1 SCAR=1.0
Subroutine rethrow: not called CC=1 SCAR=1.0
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Branch: 38 branches
BranchTrueFalseTotal
eval $sDeclare or do { () = caller 1; print STDERR "Can't create class $sClass at file $f, line $l\n"; if ($sClass =~ /\w:\w/) { print STDERR "Bad class name: At least one ':' is not doubled\n"; } elsif (not $sClass =~ /^\w+(?:::\w+)*$/) { print STDERR "Bad class name: $sClass\n"; } else { $sDeclare =~ s/(sub |use )/\n$1/g; print STDERR "$sDeclare\n"; } } 6 0 6
$_[1] ? : 0 0 0
$_[1] ? : 0 0 0
$_[1] ? : 0 0 0
$_[1] ? : 0 0 0
$_[1] ? : 0 0 0
$_[1] ? : 0 6 6
$_[1] ? : 0 0 0
$_[1] ? : 0 0 0
$_[1] ? : 0 0 0
$_[1] ? : 0 6 6
$_[1] ? : 0 0 0
$_[1] ? : 0 0 0
$_[1] ? : 0 0 0
$_[1] ? : 0 6 6
$_[1] ? : 0 0 0
$_[1] ? : 0 0 0
exists $_[0][1]{$_[1]} ? : 0 0 0
exists $_[0][1]{$_[1]} ? : 0 0 0
exists $_[0][1]{$_[1]} ? : 0 0 0
exists $_[0][1]{$_[1]} ? : 0 0 0
if (ref $e) { } 0 1 1
unless defined $msg 0 0 0
if (ref $e) { } 2 5 7
unless defined $msg 1 1 2
if (ref $e) { } 2 5 7
unless defined $msg 1 1 2
if ref $_[0] 1 3 4
defined $_ ? : 6 2 8
if ref $_[0] 0 1 1
defined $_ ? : 2 0 2
if ref $_[0] 0 2 2
defined $_ ? : 1 1 2
defined $_ ? : 0 0 0
defined $_ ? : 0 0 0
if defined $msg 0 0 0
if defined $msg 0 2 2
if defined $msg 0 2 2
184 0 my ($p,$f,$l) = caller(1);
185 0 print STDERR "Can't create class $sClass at file $f, line $l\n";
186 0 ▶ if ($sClass =~ /\w:\w/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($sClass =~ /\w:\w/) { } 0 0 0
elsif (not $sClass =~ /^\w+(?:::\w+)*$/) { } 0 0 0
187 0 print STDERR "Bad class name: "
188 ."At least one ':' is not doubled\n";
189 } elsif ($sClass !~ /^\w+(?:::\w+)*$/) {
190 0 print STDERR "Bad class name: $sClass\n";
191 } else {
192 0 $sDeclare=~s/(sub |use )/\n$1/g; print STDERR "$sDeclare\n";
193 }
194 };
195  
196 # this needs to be separate from the eval, otherwise it never
197 # ends up in @INC or @ISA, at least in Perl 5.8.8
198 6 $INC{$sPath} = __FILE__;
199 6 ▶ eval "\@${sClass}::ISA=qw($sSuperClass);" if $sSuperClass;
Branch: 1 branch
BranchTrueFalseTotal
if $sSuperClass 2 4 6
200  
201 6 return $sClass;
202 }
203  
204 #------------------------------------------------------------------
205  
206 0 ▶sub isChainable { return ref($_[0])?1:0; }
Subroutine isChainable: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
207  
208 #------------------------------------------------------------------
209  
210 0 ▶sub isException {
Subroutine isException: not called CC=7 SCAR=40.3
Pod undocumented
211 0 my ($e, $sClass) = @_;
212 0 my $sRef=ref($e);
213 0 ▶ return !defined($sClass)
Branch: 6 branches
BranchTrueFalseTotal
!defined($sClass) ? : 0 0 0
$sRef ? : 0 0 0
$sClass eq '' ? : 0 0 0
$sRef eq '' ? : 0 0 0
$sRef eq '' ? : 0 0 0
$sRef->isa($sClass) ? : 0 0 0
214 ? ($sRef ? isExceptionClass($sRef) : 0)
215 : $sClass eq ''
216 ? ($sRef eq '' ? 1 : 0)
217 : ($sRef eq '')
218 ? 0
219 : $sRef->isa($sClass)
220 ?1:0;
221 }
222  
223 #------------------------------------------------------------------
224  
225 5 ▶sub isExceptionClass {
Subroutine isExceptionClass: called CC=4 SCAR=14.3
Pod undocumented
226 5 ▶ return defined($_[0]) && $_[0]->can('_getInterface')
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] && $_[0]->can('_getInterface') && $_[0]->_getInterface eq __PACKAGE__ ? : 1 4 5
Condition66%4/6
defined $_[0] and $_[0]->can("_getInterface") and $_[0]->_getInterface eq __PACKAGE__
!ll&&!rl&&r
401
defined $_[0] and $_[0]->can("_getInterface")
!ll&&!rl&&r
401
MC/DC33%1/3
defined $_[0] and $_[0]->can("_getInterface") and $_[0]->_getInterface eq __PACKAGE__
defined $_[0] $_[0]->can("_getInterface") $_[0]->_getInterface eq __PACKAGE__
Truth table50%2/4
A and B and C
A= defined $_[0]B= $_[0]->can("_getInterface")C= $_[0]->_getInterface eq __PACKAGE__
ABCresult
0XX0
10X0
1100
1111
227 && ($_[0]->_getInterface() eq __PACKAGE__) ? 1 : 0;
228 }
229  
230 #------------------------------------------------------------------
231  
232 0 ▶sub onDie {
Subroutine onDie: not called CC=1 SCAR=6.9
Pod undocumented
233 0 my $iStringify = $_[0];
234 0 ▶ $SIG{__DIE__} = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
235 0 $Exception::Lite::STRINGIFY=$iStringify;
236 0 ▶ warn 'Exception::Lite::Any'->new('Unexpected death:'.$_[0])
Branch: 1 branch
BranchTrueFalseTotal
unless $^S or isException($_[0]) 0 0 0
Condition0%0/3
$^S or isException($_[0])
l!l&&r!l&&!r
000
MC/DC0%0/2
$^S or isException($_[0])
$^S isException($_[0])
Truth table0%0/3
A or B
A= $^SB= isException($_[0])
ABresult
1X1
011
000
237 unless $^S || isException($_[0]);
238 0 };
239 }
240  
241 #------------------------------------------------------------------
242  
243 0 ▶sub onWarn {
Subroutine onWarn: not called CC=1 SCAR=6.9
Pod undocumented
244 0 my $iStringify = $_[0];
245 0 ▶ $SIG{__WARN__} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
246 0 $Exception::Lite::STRINGIFY=$iStringify;
247 0 print STDERR 'Exception::Lite::Any'->new("Warning: $_[0]");
248 0 };
249 }
250  
251 #==================================================================
252 # PRIVATE SUBROUTINES
253 #==================================================================
254  
255 #------------------------------------------------------------------
256  
257 83 ▶sub _cacheCall {
Subroutine _cacheCall: called CC=6 SCAR=17.9
258 83 my $iFrame = $_[0];
259  
260 83 my @aCaller;
261 my $aArgs;
262  
263 # caller populates @DB::args if called within DB package
264 83 eval {
265 # this 2 line wierdness is needed to prevent Module::Build from finding
266 # this and adding it to the provides list.
267 package
268 DB;
269  
270 #get rid of eval and call to _cacheCall
271 83 @aCaller = caller($iFrame+2);
272  
273 # mark leading undefined elements as maybe shifted away
274 83 my $iDefined;
275 83 ▶ if ($#aCaller < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#aCaller < 0) 22 61 83
276 22 @DB::args=@ARGV;
277 }
278 $aArgs = [ map {
279 83 defined($_)
280 170 ▶ ? do {$iDefined=1;
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 163 7 170
281 163 ▶ "'$_'" . (overload::Method($_,'""')
Branch: 2 branches
BranchTrueFalseTotal
overload::Method($_, '""') ? : 71 92 163
defined $iDefined ? : 7 0 7
282 ? ' ('.overload::StrVal($_).')':'')}
283 : 'undef' . (defined($iDefined)
284 ? '':' (maybe shifted away?)')
285 } @DB::args];
286 };
287  
288 83 ▶ return $#aCaller < 0 ? \$aArgs : [ @aCaller[0..3], $aArgs ];
Branch: 1 branch
BranchTrueFalseTotal
$#aCaller < 0 ? : 22 61 83
289 }
290  
291 #------------------------------------------------------------------
292  
293 22 ▶sub _cacheStackTrace {
Subroutine _cacheStackTrace: called CC=13 SCAR=29.4
294 22 my $e=$_[0]; my $st=[];
295  
296 # set up initial frame
297 22 my $iFrame= $STACK_OFFSET + 1; # call to new
298 22 my $aCall = _cacheCall($iFrame++);
299 22 my ($sPackage, $iFile, $iLine, $sSub, $sArgs) = @$aCall;
300 22 my $iLineFrame=$iFrame;
301  
302 22 $aCall = _cacheCall($iFrame++); #context of call to new
303 22 ▶ while (ref($aCall) ne 'REF') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $aCall ne "REF") 39 22 61
304 39 $sSub = $aCall->[3]; # subroutine containing file,line
305 39 $sArgs = $aCall->[4]; # args used to call $sSub
306  
307 #print STDERR "debug-2: package=$sPackage file=$iFile line=$iLine"
308 # ." sub=$sSub, args=@$sArgs\n";
309  
310 # in evals we want the line number within the eval, but the
311 # name of the sub in which the eval was located. To get this
312 # we wait to push on the stack until we get an actual sub name
313 # and we avoid overwriting the location information, hence 'ne'
314  
315 39 ▶ if (!$FILTER || ($sSub ne EVAL)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $FILTER or $sSub ne "(eval)") 22 17 39
Condition66%2/3
not $FILTER or $sSub ne "(eval)"
l!l&&r!l&&!r
02217
MC/DC50%1/2
not $FILTER or $sSub ne "(eval)"
not $FILTER $sSub ne "(eval)"
Truth table66%2/3
not(A) or B
A= not $FILTERB= $sSub ne "(eval)"
ABresult
1X1
011
000
316 22 my $aFrame=[ $iFile, $iLine, $sSub, $sArgs ];
317 22 ($sPackage, $iFile, $iLine) = @$aCall;
318 22 $iLineFrame=$iFrame;
319  
320 22 my $sRef=ref($FILTER);
321 22 ▶ if ($sRef eq 'CODE') {
Branch: 3 branches
BranchTrueFalseTotal
if ($sRef eq 'CODE') { } 0 22 22
elsif ($sRef eq 'ARRAY' and not _isIgnored($sSub, $FILTER)) { } 0 22 22
elsif ($sRef eq 'Regexp' and not _isIgnored($sSub, [$FILTER])) { } 0 44 44
322 0 my $x = $FILTER->(@$aFrame, $iFrame, $iLineFrame);
323 0 ▶ if (ref($x) eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $x eq 'ARRAY') { } 0 0 0
elsif (not $x) { } 0 0 0
324 0 $aFrame=$x;
325 } elsif (!$x) {
326 0 $aFrame=undef;
327 }
328 ▶ } elsif (($sRef eq 'ARRAY') && ! _isIgnored($sSub, $FILTER)) {
Condition33%1/3
$sRef eq "ARRAY" and not _isIgnored($sSub, $FILTER)
!ll&&!rl&&r
2200
MC/DC0%0/2
$sRef eq "ARRAY" and not _isIgnored($sSub, $FILTER)
$sRef eq "ARRAY" not _isIgnored($sSub, $FILTER)
Truth table33%1/3
A and not(B)
A= $sRef eq "ARRAY"B= not _isIgnored($sSub, $FILTER)
ABresult
0X0
100
111
329 0 $aFrame=undef;
330 ▶ } elsif (($sRef eq 'Regexp') && !_isIgnored($sSub, [$FILTER])) {
Condition33%1/3
$sRef eq "Regexp" and not _isIgnored($sSub, [$FILTER])
!ll&&!rl&&r
2200
MC/DC0%0/2
$sRef eq "Regexp" and not _isIgnored($sSub, [$FILTER])
$sRef eq "Regexp" not _isIgnored($sSub, [$FILTER])
Truth table33%1/3
A and not(B)
A= $sRef eq "Regexp"B= not _isIgnored($sSub, [$FILTER])
ABresult
0X0
100
111
331 0 $aFrame=undef;
332 }
333 22 ▶ push(@$st, $aFrame) if $aFrame;
Branch: 1 branch
BranchTrueFalseTotal
if $aFrame 22 0 22
334 }
335  
336 39 $aCall = _cacheCall($iFrame++);
337 }
338  
339 22 push @$st, [ $iFile, $iLine, "<package: $sPackage>", $$aCall ];
340 22 ▶ if ($e) { my $n=$#{$e->[2]}-$#$st;$e->[2]=[@{$e->[2]}[0..$n]]};
Branch: 1 branch
BranchTrueFalseTotal
if ($e) 5 17 22
341 22 return $st;
342 }
343  
344 #-----------------------------
345  
346 0 ▶sub _isIgnored {
Subroutine _isIgnored: not called CC=3 SCAR=24.8
347 0 my ($sSub, $aIgnore) = @_;
348 0 ▶ foreach my $re (@$aIgnore) { return 1 if $sSub =~ $re; }
Branch: 1 branch
BranchTrueFalseTotal
if $sSub =~ /$re/ 0 0 0
349 0 return 0;
350 }
351  
352 #------------------------------------------------------------------
353  
354 60 ▶sub _dumpMessage {
Subroutine _dumpMessage: called CC=23 SCAR=40.5
355 60 my ($e, $iDepth) = @_;
356  
357 60 my $sMsg = $e->getMessage();
358 60 ▶ return $sMsg unless $STRINGIFY;
Branch: 1 branch
BranchTrueFalseTotal
unless $STRINGIFY 20 40 60
359 40 ▶ if (ref($STRINGIFY) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $STRINGIFY eq "CODE") 10 30 40
360 10 return $STRINGIFY->($sMsg);
361 }
362  
363 30 ▶ $iDepth = 0 unless defined($iDepth);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $iDepth 30 0 30
364 30 my $sIndent = ' ' x ($TAB*$iDepth);
365 30 $sMsg = "\n${sIndent}Exception! $sMsg";
366 30 ▶ return $sMsg if $STRINGIFY == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $STRINGIFY == 0 0 30 30
367  
368 30 my ($sThrow, $sReach);
369 30 my $sTab = ' ' x $TAB;
370  
371 30 $sIndent.= $sTab;
372 30 ▶ if ($STRINGIFY > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($STRINGIFY > 2) { } 16 14 30
373 16 my $aPropagation = $e->getPropagation();
374 16 ▶ for (my $i=$#$aPropagation; $i >= 0; $i--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 0 16 16
375 0 my ($f,$l) = @{$aPropagation->[$i]};
376 0 $sMsg .= "\n${sIndent}rethrown at file $f, line $l";
377 }
378 16 $sMsg .= "\n";
379 16 $sThrow='thrown ';
380 16 $sReach='reached ';
381 } else {
382 14 $sThrow='';
383 14 $sReach='';
384 }
385  
386 30 my $st=$e->getStackTrace();
387 30 my $iTop = scalar @$st;
388  
389 30 ▶ for (my $iFrame=0; $iFrame<$iTop; $iFrame++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($iFrame < $iTop) 30 16 46
390 30 my ($f,$l,$s,$aArgs) = @{$st->[$iFrame]};
391  
392 30 ▶ if ($iFrame) {
Branch: 1 branch
BranchTrueFalseTotal
if ($iFrame) { } 4 26 30
393 #2nd and following stack frame
394 4 my $sVia="${sIndent}${sReach}via file $f, line $l";
395 4 my $sLine="$sVia in $s";
396 4 ▶ $sMsg .= (length($sLine)>$LINE_LENGTH
Branch: 1 branch
BranchTrueFalseTotal
length $sLine > $LINE_LENGTH ? : 0 4 4
397 ? "\n$sVia\n$sIndent${sTab}in $s" : "\n$sLine");
398 } else {
399 # first stack frame
400 26 my $tid=$e->getTid();
401 26 my $sAt="${sIndent}${sThrow}at file $f, line $l";
402 26 my $sLine="$sAt in $s";
403 26 ▶ $sMsg .= (length($sLine)>$LINE_LENGTH
Branch: 2 branches
BranchTrueFalseTotal
length $sLine > $LINE_LENGTH ? : 0 26 26
defined $tid ? : 0 26 26
404 ? "\n$sAt\n$sIndent${sTab}in $s" : "\n$sLine")
405 . ", pid=" . $e->getPid() . (defined($tid)?", tid=$tid":'');
406  
407 26 ▶ return "$sMsg\n" if $STRINGIFY == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $STRINGIFY == 1 14 12 26
408 }
409  
410 16 ▶ if ($STRINGIFY > 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($STRINGIFY > 3) 0 16 16
411 0 my $bTop = ($iFrame+1) == $iTop;
412 0 ▶ my $sVar= ($bTop && !$iDepth) ? '@ARGV' : '@_';
Branch: 1 branch
BranchTrueFalseTotal
$bTop && !$iDepth ? : 0 0 0
Condition0%0/3
$bTop and not $iDepth
!ll&&!rl&&r
000
MC/DC0%0/2
$bTop and not $iDepth
$bTop not $iDepth
Truth table0%0/3
A and not(B)
A= $bTopB= not $iDepth
ABresult
0X0
100
111
413 my $bMaybeEatenByGetOpt = $bTop && !scalar(@$aArgs)
414 0 ▶ && exists($INC{'Getopt/Long.pm'});
Condition0%0/6
$bTop && !scalar(@$aArgs) && exists $INC{'Getopt/Long.pm'}
!ll&&!rl&&r
000
$bTop and not scalar @$aArgs
!ll&&!rl&&r
000
MC/DC0%0/3
$bTop && !scalar(@$aArgs) && exists $INC{'Getopt/Long.pm'}
$bTop not scalar @$aArgs exists $INC{'Getopt/Long.pm'}
Truth table0%0/4
A and not(B) && C
A= $bTopB= not scalar @$aArgsC= exists $INC{'Getopt/Long.pm'}
ABCresult
0XX0
10X0
1100
1111
415  
416 0 my $sVarIndent = "\n${sIndent}" . (' ' x $TAB);
417 0 my $sArgPrefix = "${sVarIndent}".(' ' x length($sVar)).' ';
418 0 ▶ if ($bMaybeEatenByGetOpt) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bMaybeEatenByGetOpt) { } 0 0 0
419 0 $sMsg .= $sArgPrefix . $sVar
420 . '() # maybe eaten by Getopt::Long?';
421 } else {
422 0 my $sArgs = join($sArgPrefix.',', @$aArgs);
423 0 $sMsg .= "${sVarIndent}$sVar=($sArgs";
424 0 ▶ $sMsg .= $sArgs ? "$sArgPrefix)" : ')';
Branch: 1 branch
BranchTrueFalseTotal
$sArgs ? : 0 0 0
425 }
426 }
427 }
428 16 $sMsg.="\n";
429 16 ▶ return $sMsg if $STRINGIFY == 2;
Branch: 1 branch
BranchTrueFalseTotal
if $STRINGIFY == 2 0 16 16
430  
431 16 my $eChained = $e->getChained();
432 16 ▶ if (defined($eChained)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $eChained) 0 16 16
433 0 ▶ my $sTrigger = isException($eChained)
Branch: 1 branch
BranchTrueFalseTotal
isException($eChained) ? : 0 0 0
434 ? _dumpMessage($eChained, $iDepth+1)
435 : "\n${sIndent}$eChained\n";
436 0 $sMsg .= "\n${sIndent}Triggered by...$sTrigger";
437 }
438 16 return $sMsg;
439 }
440  
441 #------------------------------------------------------------------
442  
443 # refaddr has a prototype($) so we can't use it directly as an
444 # overload operator: it complains about being passed 3 parameters
445 # instead of 1.
446 15 ▶sub _refaddr { Scalar::Util::refaddr($_[0]) };
Subroutine _refaddr: called CC=1 SCAR=0.0
447  
448 #------------------------------------------------------------------
449  
450 4 ▶sub _rethrow {
Subroutine _rethrow: called CC=3 SCAR=11.8
451 4 my $self = shift; my $sAddOrOmit = shift;
452 4 my ($p,$f,$l)=caller(1);
453 4 $self->PROPAGATE($f,$l);
454  
455 4 ▶ if (@_%2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ % 2) 0 4 4
456 0 warn sprintf('bad parameter list to %s->rethrow(...)'
457 .'at file %d, line %d: odd number of elements in property-value '
458 .'list, property value has no property name and will be '
459 ."discarded (common causes: you have %s string)\n"
460 ,$f, $l, $sAddOrOmit);
461 0 shift @_;
462 }
463 4 ▶ $self->replaceProperties({@_}) if (@_);
Branch: 1 branch
BranchTrueFalseTotal
if @_ 4 0 4
464 4 return $self;
465 }
466  
467 #------------------------------------------------------------------
468 # Traps warnings and reworks them so that they tell the user how
469 # to fix the problem rather than obscurely complain about an
470 # invisible sprintf with uninitialized values that seem to come from
471 # no where (and make Exception::Lite look like it is broken)
472  
473 7 ▶sub _sprintf {
Subroutine _sprintf: called CC=3 SCAR=11.0
474 7 my $sMsg;
475 my $sWarn;
476  
477 {
478 7 ▶ local $SIG{__WARN__} = sub { $sWarn=$_[0] if !defined($sWarn) };
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if not defined $sWarn 2 2 4
479  
480 # sprintf has prototype ($@)
481 7 my $sFormat = shift;
482 7 $sMsg = sprintf($sFormat, @_);
483 }
484  
485 7 ▶ if (defined($sWarn)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sWarn) 2 5 7
486 2 my $sReason='';
487 2 my ($f, $l, $s) = (caller(1))[1,2,3];
488 2 $s =~ s/::(\w+)\z/->$1/;
489 2 $sWarn =~ s/sprintf/$s/;
490 2 $sWarn =~ s/\s+at\s+[\w\/\.]+\s+line\s+\d+\.\s+\z//;
491 2 ▶ if ($sWarn
Branch: 1 branch
BranchTrueFalseTotal
if ($sWarn =~ /^Use of uninitialized value in|^Missing argument/) 2 0 2
492 =~ m{^Use of uninitialized value in|^Missing argument}) {
493 2 my $p=$s; $p =~ s/->\w+\z//;
494 2 $sReason ="\n Most likely cause: "
495 . "Either you are missing property-value pairs needed to"
496 . "build the message or your exception class's format"
497 . "definition mistakenly has too many placeholders "
498 . "(e.g. %s,%d,etc)\n";
499 }
500 2 warn "$sWarn called at file $f, line $l$sReason\n";
501 }
502 7 return $sMsg;
503 }
504  
505 #------------------------------------------------------------------
506  
507 22 ▶sub _shiftProperties {
Subroutine _shiftProperties: called CC=2 SCAR=6.9
508 22 my $cl= shift; my $st=shift; my $sAddOrOmit = shift;
509 22 ▶ if (@_%2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ % 2) 1 21 22
510 1 $"='|';
511 1 warn sprintf('bad parameter list to %s->new(...) at '
512 .'file %s, line %d: odd number of elements in property-value '
513 .'list, property value has no property name and will be '
514 .'discarded (common causes: you have %s string -or- you are '
515 ."using a string as a chained exception)\n"
516 ,$cl,$st->[0]->[0],$st->[0]->[1], $sAddOrOmit);
517 1 shift @_;
518 }
519 22 return {@_};
520 }
521  
522 #==================================================================
523 # MODULE INITIALIZATION
524 #==================================================================
525  
526 1 declareExceptionClass(__PACKAGE__ .'::Any');
527 1 1;
↑ Summary