blib/lib/Try/Catch.pm

filter: Statementstmt 100% 66 / 66 Branchbran 100% 34 / 34 Conditioncond 66.6% 4 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 8 / 8 Podpod 100% 3 / 3 totaltotal 96.6% 117 / 121 CC 21 SCAR 22.4
CC
21
Cov
98%
try
15.0
catch
5.0
finally
3.0
CRAP 9.4
↑ Summary
1 package Try::Catch;
2 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 13 $Carp::Internal{+__PACKAGE__}++;
6 13 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 13 our @EXPORT = our @EXPORT_OK = qw(try catch finally);
8 13 our $VERSION = '1.1.0';
9
10 4 ▶sub _default_catch {
Subroutine _default_catch: called CC=1 SCAR=0.0
11 4 croak $_[0];
12 }
13
14 67 ▶sub try(&;@) {
Subroutine try: called CC=15 SCAR=27.1
Pod documented
15 67 my $wantarray = wantarray;
16 67 my $try = shift;
17 67 my $caller = pop;
18 67 my $finally = pop;
19 67 my $catch = pop;
20
21 67 ▶ if (!$caller || $caller ne __PACKAGE__){
Branch: 1 branch
BranchTrueFalseTotal
if (not $caller or $caller ne __PACKAGE__) 1 66 67
Condition66%2/3
not $caller or $caller ne __PACKAGE__
l!l&&r!l&&!r
1066
MC/DC50%1/2
not $caller or $caller ne __PACKAGE__
not $caller $caller ne __PACKAGE__
Truth table66%2/3
not(A) or B
A= not $callerB= $caller ne __PACKAGE__
ABresult
1X1
011
000
22 1 croak "syntax error after try block \n" .
23 "usage : \n" .
24 "try { ... } catch { ... }; \n" .
25 "try { ... } finally { ... }; \n" .
26 "try { ... } catch { ... } finally { ... }; ";
27 }
28
29 #sane behaviour is to throw an error
30 #if there is no catch block
31 66 ▶ if (!$catch){
Branch: 1 branch
BranchTrueFalseTotal
unless ($catch) 12 54 66
32 12 $catch = \&_default_catch;
33 }
34
35 66 my @ret;
36 66 my $prev_error = $@;
37 66 my $fail = not eval {
38 66 $@ = $prev_error;
39 66 ▶ if (!defined $wantarray) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $wantarray) { } 55 11 66
elsif (not $wantarray) { } 7 4 11
40 55 $try->();
41 } elsif (!$wantarray) {
42 7 $ret[0] = $try->();
43 } else {
44 4 @ret = $try->();
45 }
46 23 return 1;
47 };
48
49 62 my $error = $@;
50
51 62 ▶ if ($fail) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fail) 39 23 62
52 39 my $ret = not eval {
53 39 $@ = $prev_error;
54 39 for ($error) {
55 39 ▶ if (!defined $wantarray) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $wantarray) { } 33 6 39
elsif (not $wantarray) { } 4 2 6
56 33 $catch->($error);
57 } elsif (!$wantarray) {
58 4 $ret[0] = $catch->($error);
59 } else {
60 2 @ret = $catch->($error);
61 }
62 27 last; ## seems to boost speed by 7%
63 }
64 28 return 1;
65 };
66
67 39 ▶ if ($ret){
Branch: 1 branch
BranchTrueFalseTotal
if ($ret) 11 28 39
68 11 my $catch_error = $@;
69 11 ▶ if ($finally) {
Branch: 1 branch
BranchTrueFalseTotal
if ($finally) 9 2 11
70 9 $@ = $prev_error;
71 9 $finally->($error);
72 }
73 11 croak $catch_error;
74 }
75 }
76
77 51 ▶ if ($finally) {
Branch: 1 branch
BranchTrueFalseTotal
if ($finally) 20 31 51
78 20 $@ = $prev_error;
79 20 ▶ $finally->( $fail ? $error : () );
Branch: 1 branch
BranchTrueFalseTotal
$fail ? : 9 11 20
80 }
81
82 50 $@ = $prev_error;
83 50 ▶ return $wantarray ? @ret : $ret[0];
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 4 46 50
84 }
85
86 60 ▶sub catch(&;@) {
Subroutine catch: called CC=5 SCAR=16.1
Pod documented
87 60 ▶ croak 'Useless bare catch()' unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 3 57 60
88 57 ▶ if (@_ > 1){
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) { } 20 37 57
89 20 ▶ croak "syntax error after catch block - maybe a missing semicolon"
Branch: 1 branch
BranchTrueFalseTotal
if not $_[2] or $_[2] ne __PACKAGE__ 1 19 20
Condition66%2/3
not $_[2] or $_[2] ne __PACKAGE__
l!l&&r!l&&!r
1019
MC/DC50%1/2
not $_[2] or $_[2] ne __PACKAGE__
not $_[2] $_[2] ne __PACKAGE__
Truth table66%2/3
not(A) or B
A= not $_[2]B= $_[2] ne __PACKAGE__
ABresult
1X1
011
000
90 if !$_[2] || $_[2] ne __PACKAGE__;
91 } else {
92 37 return ( shift, undef, __PACKAGE__);
93 }
94 19 return (@_);
95 }
96
97 36 ▶sub finally(&;@) {
Subroutine finally: called CC=3 SCAR=11.0
Pod documented
98 36 ▶ croak 'Useless bare finally()' unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 3 33 36
99 33 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 1 32 33
100 1 croak "syntax error after finally block - maybe a missing semicolon";
101 }
102 32 return ( shift, __PACKAGE__ );
103 }
104
105 13 1;
106
107 __END__
↑ Summary