Error.pm

filter: Statementstmt 82.2% 236 / 287 Branchbran 58.8% 73 / 124 Conditioncond 59.4% 22 / 37 MC/DCmcdc 26.0% 6 / 23 Subroutinesub 87.1% 34 / 39 Podpod 100% 17 / 17 totaltotal 73.6% 388 / 527 CC 77 SCAR 24.8
CC
77
Cov
74%
prior
52.3
run_clauses
20.8
flush
20.0
CRAP 11.9
↑ Summary Error/Simple.pm »
1 # Error.pm
2 #
3 # Copyright (c) 1997-8 Graham Barr <gbarr@ti.com>. All rights reserved.
4 # This program is free software; you can redistribute it and/or
5 # modify it under the same terms as Perl itself.
6 #
7 # Based on my original Error.pm, and Exceptions.pm by Peter Seibel
8 # <peter@weblogic.com> and adapted by Jesse Glick <jglick@sig.bsh.com>.
9 #
10 # but modified ***significantly***
11  
12 package Error;
13 26 $Error::VERSION = '0.17031';
14 26 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 26 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 26 ▶use overload (
Subroutine BEGIN: called CC=1 SCAR=0.0
18 '""' => 'stringify',
19 '0+' => 'value',
20 18 ▶ 'bool' => sub { return 1; },
Subroutine __ANON__: called CC=1 SCAR=0.0
21 26 'fallback' => 1
22 26 );
23  
24 26 $Error::Depth = 0; # Depth to pass to caller()
25 26 $Error::Debug = 0; # Generate verbose stack traces
26 26 @Error::STACK = (); # Clause stack for try
27 26 $Error::THROWN = undef; # last error thrown, a workaround until die $ref works
28  
29 26 my $LAST; # Last error created
30 my %ERROR; # Last error associated with package
31  
32 sub _throw_Error_Simple
33 12 ▶{
Subroutine _throw_Error_Simple: called CC=1 SCAR=0.0
34 12 my $args = shift;
35 12 return Error::Simple->new( $args->{'text'} );
36 }
37  
38 26 $Error::ObjectifyCallback = \&_throw_Error_Simple;
39  
40 # Exported subs are defined in Error::subs
41  
42 26 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 sub import
45 29 ▶{
Subroutine import: called CC=2 SCAR=6.9
46 29 shift;
47 29 my @tags = @_;
48 29 local $Exporter::ExportLevel = $Exporter::ExportLevel + 1;
49  
50 @tags = grep {
51 29 ▶ if ( $_ eq ':warndie' )
Branch: 1 branch
BranchTrueFalseTotal
if ($_ eq ':warndie') { } 14 12 26
52 {
53 14 Error::WarnDie->import();
54 14 0;
55 }
56 else
57 {
58 12 1;
59 }
60 } @tags;
61  
62 29 Error::subs->import(@tags);
63 }
64  
65 # I really want to use last for the name of this method, but it is a keyword
66 # which prevent the syntax last Error
67  
68 sub prior
69 1 ▶{
Subroutine prior: called CC=8 SCAR=39.6
Pod documented
70 1 shift; # ignore
71  
72 1 ▶ return $LAST unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 1 0 1
73  
74 0 my $pkg = shift;
75 0 ▶ return exists $ERROR{$pkg} ? $ERROR{$pkg} : undef
Branch: 2 branches
BranchTrueFalseTotal
unless ref $pkg 0 0 0
exists $ERROR{$pkg} ? : 0 0 0
76 unless ref($pkg);
77  
78 0 my $obj = $pkg;
79 0 my $err = undef;
80 0 ▶ if ( $obj->isa('HASH') )
Branch: 2 branches
BranchTrueFalseTotal
if ($obj->isa('HASH')) { } 0 0 0
elsif ($obj->isa('GLOB')) { } 0 0 0
81 {
82 $err = $obj->{'__Error__'}
83 0 ▶ if exists $obj->{'__Error__'};
Branch: 1 branch
BranchTrueFalseTotal
if exists $obj->{"__Error__"} 0 0 0
84 }
85 elsif ( $obj->isa('GLOB') )
86 {
87 0 $err = ${*$obj}{'__Error__'}
88 0 ▶ if exists ${*$obj}{'__Error__'};
Branch: 1 branch
BranchTrueFalseTotal
if exists do { *$obj }->{"__Error__"} 0 0 0
89 }
90  
91 0 $err;
92 }
93  
94 sub flush
95 0 ▶{
Subroutine flush: not called CC=4 SCAR=30.0
Pod documented
96 0 shift; #ignore
97  
98 0 ▶ unless (@_)
Branch: 1 branch
BranchTrueFalseTotal
unless (@_) 0 0 0
99 {
100 0 $LAST = undef;
101 0 return;
102 }
103  
104 0 my $pkg = shift;
105 0 ▶ return unless ref($pkg);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $pkg 0 0 0
106  
107 0 ▶ undef $ERROR{$pkg} if defined $ERROR{$pkg};
Branch: 1 branch
BranchTrueFalseTotal
if defined $ERROR{$pkg} 0 0 0
108 }
109  
110 # Return as much information as possible about where the error
111 # happened. The -stacktrace element only exists if $Error::DEBUG
112 # was set when the error was created
113  
114 sub stacktrace
115 1 ▶{
Subroutine stacktrace: called CC=4 SCAR=23.2
Pod documented
116 1 my $self = shift;
117  
118 return $self->{'-stacktrace'}
119 1 ▶ if exists $self->{'-stacktrace'};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"-stacktrace"} 1 0 1
120  
121 0 ▶ my $text = exists $self->{'-text'} ? $self->{'-text'} : "Died";
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'-text'} ? : 0 0 0
122  
123 0 ▶ $text .= sprintf( " at %s line %d.\n", $self->file, $self->line )
Branch: 1 branch
BranchTrueFalseTotal
unless $text =~ /\n$/s 0 0 0
124 unless ( $text =~ /\n$/s );
125  
126 0 $text;
127 }
128  
129 sub associate
130 0 ▶{
Subroutine associate: not called CC=4 SCAR=30.0
Pod documented
131 0 my $err = shift;
132 0 my $obj = shift;
133  
134 0 ▶ return unless ref($obj);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $obj 0 0 0
135  
136 0 ▶ if ( $obj->isa('HASH') )
Branch: 2 branches
BranchTrueFalseTotal
if ($obj->isa('HASH')) { } 0 0 0
elsif ($obj->isa('GLOB')) { } 0 0 0
137 {
138 0 $obj->{'__Error__'} = $err;
139 }
140 elsif ( $obj->isa('GLOB') )
141 {
142 0 ${*$obj}{'__Error__'} = $err;
143 }
144 0 $obj = ref($obj);
145 0 $ERROR{ ref($obj) } = $err;
146  
147 0 return;
148 }
149  
150 sub new
151 25 ▶{
Subroutine new: called CC=4 SCAR=13.9
Pod documented
152 25 my $self = shift;
153 25 my ( $pkg, $file, $line ) = caller($Error::Depth);
154  
155 25 my $err = bless {
156 '-package' => $pkg,
157 '-file' => $file,
158 '-line' => $line,
159 @_
160 }, $self;
161  
162 $err->associate( $err->{'-object'} )
163 25 ▶ if ( exists $err->{'-object'} );
Branch: 1 branch
BranchTrueFalseTotal
if exists $err->{"-object"} 0 25 25
164  
165 # To always create a stacktrace would be very inefficient, so
166 # we only do it if $Error::Debug is set
167  
168 25 ▶ if ($Error::Debug)
Branch: 1 branch
BranchTrueFalseTotal
if ($Error::Debug) 1 24 25
169 {
170 1 require Carp;
171 1 local $Carp::CarpLevel = $Error::Depth;
172 1 ▶ my $text = defined( $err->{'-text'} ) ? $err->{'-text'} : "Error";
Branch: 1 branch
BranchTrueFalseTotal
defined $err->{'-text'} ? : 1 0 1
173 1 my $trace = Carp::longmess($text);
174  
175 # Remove try calls from the trace
176 1 $trace =~
177 s/(\n\s+\S+__ANON__[^\n]+)?\n\s+eval[^\n]+\n\s+Error::subs::try[^\n]+(?=\n)//sog;
178 1 $trace =~
179 s/(\n\s+\S+__ANON__[^\n]+)?\n\s+eval[^\n]+\n\s+Error::subs::run_clauses[^\n]+\n\s+Error::subs::try[^\n]+(?=\n)//sog;
180 1 $err->{'-stacktrace'} = $trace;
181 }
182  
183 25 $@ = $LAST = $ERROR{$pkg} = $err;
184 }
185  
186 # Throw an error. this contains some very gory code.
187  
188 sub throw
189 23 ▶{
Subroutine throw: called CC=2 SCAR=6.9
Pod documented
190 23 my $self = shift;
191 23 local $Error::Depth = $Error::Depth + 1;
192  
193 # if we are not rethrow-ing then create the object to throw
194 23 ▶ $self = $self->new(@_) unless ref($self);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 12 11 23
195  
196 23 die $Error::THROWN = $self;
197 }
198  
199 # syntactic sugar for
200 #
201 # die with Error( ... );
202  
203 sub with
204 0 ▶{
Subroutine with: not called CC=1 SCAR=6.9
Pod documented
205 0 my $self = shift;
206 0 local $Error::Depth = $Error::Depth + 1;
207  
208 0 $self->new(@_);
209 }
210  
211 # syntactic sugar for
212 #
213 # record Error( ... ) and return;
214  
215 sub record
216 0 ▶{
Subroutine record: not called CC=1 SCAR=6.9
Pod documented
217 0 my $self = shift;
218 0 local $Error::Depth = $Error::Depth + 1;
219  
220 0 $self->new(@_);
221 }
222  
223 # catch clause for
224 #
225 # try { ... } catch CLASS with { ... }
226  
227 sub catch
228 16 ▶{
Subroutine catch: called CC=3 SCAR=11.0
Pod documented
229 16 my $pkg = shift;
230 16 my $code = shift;
231 16 ▶ my $clauses = shift || {};
Condition100%2/2
shift() || {}
l!l
610
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
232 16 ▶ my $catch = $clauses->{'catch'} ||= [];
Condition100%2/2
$clauses->{"catch"} ||= []
l!l
313
MC/DC100%1/1
$clauses->{"catch"} ||= []
$clauses->{"catch"}
Truth table100%2/2
A
A= $clauses->{"catch"}
Aresult
11
00
233  
234 16 unshift @$catch, $pkg, $code;
235  
236 16 $clauses;
237 }
238  
239 # Object query methods
240  
241 sub object
242 0 ▶{
Subroutine object: not called CC=2 SCAR=17.9
Pod documented
243 0 my $self = shift;
244 0 ▶ exists $self->{'-object'} ? $self->{'-object'} : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'-object'} ? : 0 0 0
245 }
246  
247 sub file
248 11 ▶{
Subroutine file: called CC=2 SCAR=7.2
Pod documented
249 11 my $self = shift;
250 11 ▶ exists $self->{'-file'} ? $self->{'-file'} : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'-file'} ? : 11 0 11
251 }
252  
253 sub line
254 11 ▶{
Subroutine line: called CC=2 SCAR=7.2
Pod documented
255 11 my $self = shift;
256 11 ▶ exists $self->{'-line'} ? $self->{'-line'} : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'-line'} ? : 11 0 11
257 }
258  
259 sub text
260 1 ▶{
Subroutine text: called CC=2 SCAR=7.2
Pod documented
261 1 my $self = shift;
262 1 ▶ exists $self->{'-text'} ? $self->{'-text'} : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'-text'} ? : 1 0 1
263 }
264  
265 # overload methods
266  
267 sub stringify
268 12 ▶{
Subroutine stringify: called CC=2 SCAR=7.2
Pod documented
269 12 my $self = shift;
270 12 ▶ defined $self->{'-text'} ? $self->{'-text'} : "Died";
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'-text'} ? : 12 0 12
271 }
272  
273 sub value
274 3 ▶{
Subroutine value: called CC=2 SCAR=7.2
Pod documented
275 3 my $self = shift;
276 3 ▶ exists $self->{'-value'} ? $self->{'-value'} : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'-value'} ? : 3 0 3
277 }
278  
279 package Error::Simple;
280 26 $Error::Simple::VERSION = '0.17031';
281 26 @Error::Simple::ISA = qw(Error);
282  
283 sub new
284 22 ▶{
Subroutine new: called CC=3 SCAR=11.0
Pod documented
285 22 my $self = shift;
286 22 my $text = "" . shift;
287 22 my $value = shift;
288 22 my (@args) = ();
289  
290 22 local $Error::Depth = $Error::Depth + 1;
291  
292 22 ▶ @args = ( -file => $1, -line => $2 )
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ s/\s+at\s+(\S+)\s+line\s+(\d+)(?:,\s*<[^>]*>\s+line\s+\d+)?\.?\n?$//s 12 10 22
293 if ( $text =~
294 s/\s+at\s+(\S+)\s+line\s+(\d+)(?:,\s*<[^>]*>\s+line\s+\d+)?\.?\n?$//s );
295 22 ▶ push( @args, '-value', 0 + $value )
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 1 21 22
296 if defined($value);
297  
298 22 $self->SUPER::new( -text => $text, @args );
299 }
300  
301 sub stringify
302 12 ▶{
Subroutine stringify: called CC=2 SCAR=6.9
Pod documented
303 12 my $self = shift;
304 12 my $text = $self->SUPER::stringify;
305 12 ▶ $text .= sprintf( " at %s line %d.\n", $self->file, $self->line )
Branch: 1 branch
BranchTrueFalseTotal
unless $text =~ /\n$/s 10 2 12
306 unless ( $text =~ /\n$/s );
307 12 $text;
308 }
309  
310 ##########################################################################
311 ##########################################################################
312  
313 # Inspired by code from Jesse Glick <jglick@sig.bsh.com> and
314 # Peter Seibel <peter@weblogic.com>
315  
316 package Error::subs;
317 26 $Error::subs::VERSION = '0.17031';
318 26 ▶use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
319 26 ▶use vars qw(@EXPORT_OK @ISA %EXPORT_TAGS);
Subroutine BEGIN: called CC=1 SCAR=0.0
320  
321 26 @EXPORT_OK = qw(try with finally except otherwise);
322 26 %EXPORT_TAGS = ( try => \@EXPORT_OK );
323  
324 26 @ISA = qw(Exporter);
325  
326 sub run_clauses ($$$\@)
327 23 ▶{
Subroutine run_clauses: called CC=19 SCAR=30.4
328 23 my ( $clauses, $err, $wantarray, $result ) = @_;
329 23 my $code = undef;
330  
331 23 ▶ $err = $Error::ObjectifyCallback->( { 'text' => $err } ) unless ref($err);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $err 9 14 23
332  
333 CATCH:
334 {
335  
336 # catch
337 23 my $catch;
338 23 ▶ if ( defined( $catch = $clauses->{'catch'} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined($catch = $clauses->{"catch"})) 14 9 23
339 {
340 14 my $i = 0;
341  
342 CATCHLOOP:
343 14 ▶ for ( ; $i < @$catch ; $i += 2 )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$catch) 16 4 20
344 {
345 16 my $pkg = $catch->[$i];
346 16 ▶ unless ( defined $pkg )
Branch: 2 branches
BranchTrueFalseTotal
if (defined $pkg) { } 15 1 16
if (do { builtin::blessed($err) and $err->isa($pkg) }) 11 5 16
347 {
348 #except
349 1 splice( @$catch, $i, 2, $catch->[ $i + 1 ]->($err) );
350 1 $i -= 2;
351 1 next CATCHLOOP;
352 }
353 0 ▶ elsif ( Scalar::Util::blessed($err) && $err->isa($pkg) )
Condition100%3/3
builtin::blessed($err) && $err->isa($pkg)
!ll&&!rl&&r
1311
MC/DC100%2/2
builtin::blessed($err) && $err->isa($pkg)
builtin::blessed($err) $err->isa($pkg)
Truth table100%3/3
A && B
A= builtin::blessed($err)B= $err->isa($pkg)
ABresult
0X0
100
111
354 {
355 11 $code = $catch->[ $i + 1 ];
356 11 while (1)
357 {
358 11 my $more = 0;
359 11 local ( $Error::THROWN, $@ );
360 11 my $ok = eval {
361 11 $@ = $err;
362 11 ▶ if ($wantarray)
Branch: 2 branches
BranchTrueFalseTotal
if ($wantarray) { } 0 11 11
elsif (defined $wantarray) { } 3 8 11
363 {
364 0 @{$result} = $code->( $err, \$more );
365 }
366 elsif ( defined($wantarray) )
367 {
368 3 @{$result} = ();
369 3 $result->[0] = $code->( $err, \$more );
370 }
371 else
372 {
373 8 $code->( $err, \$more );
374 }
375 6 1;
376 };
377 11 ▶ if ($ok)
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) { } 6 5 11
378 {
379 6 ▶ next CATCHLOOP if $more;
Branch: 1 branch
BranchTrueFalseTotal
if $more 1 5 6
380 5 undef $err;
381 }
382 else
383 {
384 5 ▶ $err = $@ || $Error::THROWN;
Condition33%1/3
$@ || $Error::THROWN
l!l&&r!l&&!r
500
MC/DC0%0/2
$@ || $Error::THROWN
$@ $Error::THROWN
Truth table33%1/3
A || B
A= $@B= $Error::THROWN
ABresult
1X1
011
000
385 5 ▶ $err = $Error::ObjectifyCallback->(
Branch: 1 branch
BranchTrueFalseTotal
unless ref $err 2 3 5
386 { 'text' => $err } )
387 unless ref($err);
388 }
389 10 last CATCH;
390 }
391 }
392 }
393 }
394  
395 # otherwise
396 13 my $owise;
397 13 ▶ if ( defined( $owise = $clauses->{'otherwise'} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined($owise = $clauses->{"otherwise"})) 8 5 13
398 {
399 8 my $code = $clauses->{'otherwise'};
400 8 my $more = 0;
401 8 local ( $Error::THROWN, $@ );
402 8 my $ok = eval {
403 8 $@ = $err;
404 8 ▶ if ($wantarray)
Branch: 2 branches
BranchTrueFalseTotal
if ($wantarray) { } 0 8 8
elsif (defined $wantarray) { } 0 8 8
405 {
406 0 @{$result} = $code->( $err, \$more );
407 }
408 elsif ( defined($wantarray) )
409 {
410 0 @{$result} = ();
411 0 $result->[0] = $code->( $err, \$more );
412 }
413 else
414 {
415 8 $code->( $err, \$more );
416 }
417 6 1;
418 };
419 8 ▶ if ($ok)
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) { } 6 2 8
420 {
421 6 undef $err;
422 }
423 else
424 {
425 2 ▶ $err = $@ || $Error::THROWN;
Condition33%1/3
$@ || $Error::THROWN
l!l&&r!l&&!r
200
MC/DC0%0/2
$@ || $Error::THROWN
$@ $Error::THROWN
Truth table33%1/3
A || B
A= $@B= $Error::THROWN
ABresult
1X1
011
000
426  
427 2 ▶ $err = $Error::ObjectifyCallback->( { 'text' => $err } )
Branch: 1 branch
BranchTrueFalseTotal
unless ref $err 2 0 2
428 unless ref($err);
429 }
430 }
431 }
432 23 $err;
433 }
434  
435 sub try (&;$)
436 25 ▶{
Subroutine try: called CC=12 SCAR=25.1
437 25 my $try = shift;
438 25 ▶ my $clauses = @_ ? shift : {};
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 24 1 25
439 25 my $ok = 0;
440 25 my $err = undef;
441 25 my @result = ();
442  
443 25 unshift @Error::STACK, $clauses;
444  
445 25 my $wantarray = wantarray();
446  
447 do
448 25 {
449 25 local $Error::THROWN = undef;
450 25 local $@ = undef;
451  
452 25 $ok = eval {
453 25 ▶ if ($wantarray)
Branch: 2 branches
BranchTrueFalseTotal
if ($wantarray) { } 0 25 25
elsif (defined $wantarray) { } 3 22 25
454 {
455 0 @result = $try->();
456 }
457 elsif ( defined $wantarray )
458 {
459 3 $result[0] = $try->();
460 }
461 else
462 {
463 22 $try->();
464 }
465 2 1;
466 };
467  
468 25 ▶ $err = $@ || $Error::THROWN
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 23 2 25
Condition33%1/3
$@ || $Error::THROWN
l!l&&r!l&&!r
2300
MC/DC0%0/2
$@ || $Error::THROWN
$@ $Error::THROWN
Truth table33%1/3
A || B
A= $@B= $Error::THROWN
ABresult
1X1
011
000
469 unless $ok;
470 };
471  
472 25 shift @Error::STACK;
473  
474 25 ▶ $err = run_clauses( $clauses, $err, wantarray, @result )
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 23 2 25
475 unless ($ok);
476  
477 $clauses->{'finally'}->()
478 25 ▶ if ( defined( $clauses->{'finally'} ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $clauses->{"finally"} 3 22 25
479  
480 25 ▶ if ( defined($err) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $err) 12 13 25
481 {
482 12 ▶ if ( Scalar::Util::blessed($err) && $err->can('throw') )
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($err) and $err->can('throw')) { } 11 1 12
Condition66%2/3
builtin::blessed($err) and $err->can("throw")
!ll&&!rl&&r
1011
MC/DC50%1/2
builtin::blessed($err) and $err->can("throw")
builtin::blessed($err) $err->can("throw")
Truth table66%2/3
A and B
A= builtin::blessed($err)B= $err->can("throw")
ABresult
0X0
100
111
483 {
484 11 throw $err;
485 }
486 else
487 {
488 1 die $err;
489 }
490 }
491  
492 13 ▶ wantarray ? @result : $result[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 13 13
493 }
494  
495 # Each clause adds a sub to the list of clauses. The finally clause is
496 # always the last, and the otherwise clause is always added just before
497 # the finally clause.
498 #
499 # All clauses, except the finally clause, add a sub which takes one argument
500 # this argument will be the error being thrown. The sub will return a code ref
501 # if that clause can handle that error, otherwise undef is returned.
502 #
503 # The otherwise clause adds a sub which unconditionally returns the users
504 # code reference, this is why it is forced to be last.
505 #
506 # The catch clause is defined in Error.pm, as the syntax causes it to
507 # be called as a method
508  
509 sub with (&;$)
510 16 ▶{
Subroutine with: called CC=1 SCAR=0.0
511 16 @_;
512 }
513  
514 sub finally (&)
515 3 ▶{
Subroutine finally: called CC=1 SCAR=0.0
516 3 my $code = shift;
517 3 my $clauses = { 'finally' => $code };
518 3 $clauses;
519 }
520  
521 # The except clause is a block which returns a hashref or a list of
522 # key-value pairs, where the keys are the classes and the values are subs.
523  
524 sub except (&;$)
525 1 ▶{
Subroutine except: called CC=3 SCAR=11.6
526 1 my $code = shift;
527 1 ▶ my $clauses = shift || {};
Condition50%1/2
shift() || {}
l!l
01
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
528 1 ▶ my $catch = $clauses->{'catch'} ||= [];
Condition50%1/2
$clauses->{"catch"} ||= []
l!l
01
MC/DC0%0/1
$clauses->{"catch"} ||= []
$clauses->{"catch"}
Truth table50%1/2
A
A= $clauses->{"catch"}
Aresult
11
00
529  
530 1 ▶ my $sub = sub {
Subroutine __ANON__: called CC=4 SCAR=15.5
531 1 my $ref;
532 1 my (@array) = $code->( $_[0] );
533 1 ▶ if ( @array == 1 && ref( $array[0] ) )
Branch: 1 branch
BranchTrueFalseTotal
if (@array == 1 and ref $array[0]) { } 1 0 1
Condition33%1/3
@array == 1 and ref $array[0]
!ll&&!rl&&r
001
MC/DC0%0/2
@array == 1 and ref $array[0]
@array == 1 ref $array[0]
Truth table33%1/3
A and B
A= @array == 1B= ref $array[0]
ABresult
0X0
100
111
534 {
535 1 $ref = $array[0];
536 1 ▶ $ref = [%$ref]
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($ref, "HASH") 1 0 1
537 if ( UNIVERSAL::isa( $ref, 'HASH' ) );
538 }
539 else
540 {
541 0 $ref = \@array;
542 }
543 1 @$ref;
544 1 };
545  
546 1 unshift @{$catch}, undef, $sub;
547  
548 1 $clauses;
549 }
550  
551 sub otherwise (&;$)
552 10 ▶{
Subroutine otherwise: called CC=3 SCAR=12.3
553 10 my $code = shift;
554 10 ▶ my $clauses = shift || {};
Condition50%1/2
shift() || {}
l!l
010
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
555  
556 10 ▶ if ( exists $clauses->{'otherwise'} )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $clauses->{"otherwise"}) 0 10 10
557 {
558 0 require Carp;
559 0 Carp::croak("Multiple otherwise clauses");
560 }
561  
562 10 $clauses->{'otherwise'} = $code;
563  
564 10 $clauses;
565 }
566  
567 26 1;
568  
569 package Error::WarnDie;
570 26 $Error::WarnDie::VERSION = '0.17031';
571 sub gen_callstack($)
572 6 ▶{
Subroutine gen_callstack: called CC=1 SCAR=0.0
573 6 my ($start) = @_;
574  
575 6 require Carp;
576 6 local $Carp::CarpLevel = $start;
577 6 my $trace = Carp::longmess("");
578  
579 # Remove try calls from the trace
580 6 $trace =~
581 s/(\n\s+\S+__ANON__[^\n]+)?\n\s+eval[^\n]+\n\s+Error::subs::try[^\n]+(?=\n)//sog;
582 6 $trace =~
583 s/(\n\s+\S+__ANON__[^\n]+)?\n\s+eval[^\n]+\n\s+Error::subs::run_clauses[^\n]+\n\s+Error::subs::try[^\n]+(?=\n)//sog;
584 6 my @callstack = split( m/\n/, $trace );
585 6 return @callstack;
586 }
587  
588 26 my $old_DIE;
589 my $old_WARN;
590  
591 sub DEATH
592 4 ▶{
Subroutine DEATH: called CC=8 SCAR=21.0
593 4 my ($e) = @_;
594  
595 4 ▶ my $die = $old_DIE || sub { die @_ };
Subroutine __ANON__: called CC=1 SCAR=0.1
Condition66%2/3
$old_DIE || sub { die @_; }
l!l&&r!l&&!r
130
MC/DC0%0/2
$old_DIE || sub { die @_; }
$old_DIE sub { die @_; }
Truth table66%2/3
A || B
A= $old_DIEB= sub { die @_; }
ABresult
1X1
011
000
596  
597 4 ▶ $die->(@_) if $^S;
Branch: 1 branch
BranchTrueFalseTotal
if $^S 0 4 4
598  
599 4 my ( $etype, $message, $location, @callstack );
600 4 ▶ if ( ref($e) && $e->isa("Error") )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $e and $e->isa('Error')) { } 1 3 4
Condition66%2/3
ref $e and $e->isa("Error")
!ll&&!rl&&r
301
MC/DC50%1/2
ref $e and $e->isa("Error")
ref $e $e->isa("Error")
Truth table66%2/3
A and B
A= ref $eB= $e->isa("Error")
ABresult
0X0
100
111
601 {
602 1 $etype = "exception of type " . ref($e);
603 1 $message = $e->text;
604 1 $location = $e->file . ":" . $e->line;
605 1 @callstack = split( m/\n/, $e->stacktrace );
606 }
607 else
608 {
609 # Don't apply subsequent layer of message formatting
610 3 ▶ $die->($e) if ( $e =~ m/^\nUnhandled perl error caught at toplevel:\n\n/ );
Branch: 1 branch
BranchTrueFalseTotal
if $e =~ /^\nUnhandled perl error caught at toplevel:\n\n/ 0 3 3
611 3 $etype = "perl error";
612 3 my $stackdepth = 0;
613 3 ▶ while ( caller($stackdepth) =~ m/^Error(?:$|::)/ )
Branch: 1 branch
BranchTrueFalseTotal
if (caller($stackdepth) =~ /^Error(?:$|::)/) 0 3 3
614 {
615 0 ++$stackdepth;
616 }
617  
618 3 @callstack = gen_callstack( $stackdepth + 1 );
619  
620 3 $message = "$e";
621 3 chomp $message;
622  
623 3 ▶ if ( $message =~ s/ at (.*?) line (\d+)\.$// )
Branch: 1 branch
BranchTrueFalseTotal
if ($message =~ s/ at (.*?) line (\d+)\.$//) { } 2 1 3
624 {
625 2 $location = $1 . ":" . $2;
626 }
627 else
628 {
629 1 my @caller = caller($stackdepth);
630 1 $location = $caller[1] . ":" . $caller[2];
631 }
632 }
633  
634 4 shift @callstack;
635  
636 # Do it this way in case there are no elements; we don't print a spurious \n
637 4 my $callstack = join( "", map { "$_\n" } @callstack );
638  
639 4 $die->("\nUnhandled $etype caught at toplevel:\n\n $message\n\nThrown from: $location\n\nFull stack trace:\n\n$callstack\n");
640 }
641  
642 sub TAXES
643 3 ▶{
Subroutine TAXES: called CC=2 SCAR=6.9
644 3 my ($message) = @_;
645  
646 3 ▶ my $warn = $old_WARN || sub { warn @_ };
Subroutine __ANON__: called CC=1 SCAR=0.1
Condition66%2/3
$old_WARN || sub { warn @_; }
l!l&&r!l&&!r
120
MC/DC0%0/2
$old_WARN || sub { warn @_; }
$old_WARN sub { warn @_; }
Truth table66%2/3
A || B
A= $old_WARNB= sub { warn @_; }
ABresult
1X1
011
000
647  
648 3 $message =~ s/ at .*? line \d+\.$//;
649 3 chomp $message;
650  
651 3 my @callstack = gen_callstack(1);
652 3 my $location = shift @callstack;
653  
654 # $location already starts in a leading space
655 3 $message .= $location;
656  
657 # Do it this way in case there are no elements; we don't print a spurious \n
658 3 my $callstack = join( "", map { "$_\n" } @callstack );
659  
660 3 $warn->("$message:\n$callstack");
661 }
662  
663 sub import
664 14 ▶{
Subroutine import: called CC=1 SCAR=0.0
665 14 $old_DIE = $SIG{__DIE__};
666 14 $old_WARN = $SIG{__WARN__};
667  
668 14 $SIG{__DIE__} = \&DEATH;
669 14 $SIG{__WARN__} = \&TAXES;
670 }
671  
672 26 1;
673  
674 __END__
↑ Summary Error/Simple.pm »