blib/lib/Error.pm

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