blib/lib/Try/Tiny/SmartCatch.pm

filter: Statementstmt 24.0% 25 / 104 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 35.2% 6 / 17 Podpod 100% 6 / 6 totaltotal 18.3% 37 / 202 CC 37 SCAR 58.1
CC
37
Cov
15%
try
650.0
for_error
132.0
new
12.0
CRAP 335.1
↑ Summary
1 package Try::Tiny::SmartCatch;
2  
3 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use vars qw/@EXPORT @EXPORT_OK %EXPORT_TAGS $VERSION @ISA/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 require Exporter;
11 2 @ISA = qw/Exporter/;
12 }
13  
14 2 @EXPORT = qw/try catch_when catch_default then finally/;
15 2 @EXPORT_OK = (@EXPORT, qw/throw/);
16 2 %EXPORT_TAGS = (
17 all => [@EXPORT_OK],
18 );
19  
20 2 ++$Carp::Internal{+__PACKAGE__};
21  
22 2 $VERSION = '0.5';
23  
24 0 ▶sub try($;@) {
Subroutine try: not called CC=25 SCAR=64.8
Pod documented
25 0 my ($try, @code_refs) = @_;
26  
27 0 my ($catch_default, @catch_when, $code_ref, @finally, $ref_type, $then, $wantarray);
28  
29 0 $wantarray = wantarray ();
30  
31 0 foreach $code_ref (@code_refs) {
32 0 ▶ next if (!$code_ref);
Branch: 1 branch
BranchTrueFalseTotal
unless $code_ref 0 0 0
33  
34 0 $ref_type = ref($code_ref);
35  
36 ## zero or more 'catch_when' blocks
37 0 ▶ if ($ref_type eq 'Try::Tiny::SmartCatch::Catch::When') {
Branch: 4 branches
BranchTrueFalseTotal
if ($ref_type eq 'Try::Tiny::SmartCatch::Catch::When') { } 0 0 0
elsif ($ref_type eq 'Try::Tiny::SmartCatch::Catch::Default') { } 0 0 0
elsif ($ref_type eq 'Try::Tiny::SmartCatch::Finally') { } 0 0 0
elsif ($ref_type eq 'Try::Tiny::SmartCatch::Then') { } 0 0 0
38 ## we need to save same handler for many different exception types
39 0 push(@catch_when, $code_ref);
40 }
41 ## zero or one 'catch_default' blocks
42 elsif ($ref_type eq 'Try::Tiny::SmartCatch::Catch::Default') {
43 $catch_default = $$code_ref{code}
44 0 ▶ if (!defined($catch_default));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $catch_default 0 0 0
45 }
46 ## zero or more 'finally' blocks
47 elsif ($ref_type eq 'Try::Tiny::SmartCatch::Finally') {
48 0 push(@finally, $$code_ref);
49 }
50 ## zero or one 'then' blocks
51 elsif ($ref_type eq 'Try::Tiny::SmartCatch::Then') {
52 0 ▶ $then = $$code_ref
Branch: 1 branch
BranchTrueFalseTotal
unless defined $then 0 0 0
53 if (!defined($then));
54 }
55 ## unknown block type
56 else {
57 0 require Carp;
58 0 Carp::confess("Unknown code ref type given '$ref_type'. Check your usage & try again");
59 }
60 }
61  
62 0 my ($error, $failed, $prev_error, @ret);
63  
64 ## save the value of $@ so we can set $@ back to it in the beginning of the eval
65 0 $prev_error = $@;
66  
67 {
68 ## localize $@ to prevent clobbering of previous value by a successful eval.
69 0 local $@;
70  
71 ## failed will be true if the eval dies, because 1 will not be returned from the eval body
72 0 $failed = not eval {
73 0 $@ = $prev_error;
74  
75 ## call try block in list context if try subroutine is called in list context, or we have 'then' block
76 ## result of 'try' block is passed as arguments to then block, so we need do that in that way
77 0 ▶ if ($wantarray || $then) {
Branch: 2 branches
BranchTrueFalseTotal
if ($wantarray or $then) { } 0 0 0
elsif (defined $wantarray) { } 0 0 0
Condition0%0/3
$wantarray or $then
l!l&&r!l&&!r
000
MC/DC0%0/2
$wantarray or $then
$wantarray $then
Truth table0%0/3
A or B
A= $wantarrayB= $then
ABresult
1X1
011
000
78 0 @ret = &$try();
79 }
80 elsif (defined($wantarray)) {
81 0 $ret[0] = &$try();
82 }
83 else {
84 0 &$try();
85 }
86  
87 ## properly set $fail to false
88 0 return 1;
89 };
90  
91 ## copy $@ to $error; when we leave this scope, local $@ will revert $@
92 ## back to its previous value
93 0 $error = $@;
94 }
95  
96 ## set up a scope guard to invoke the finally block at the end
97 my @guards = (
98 map {
99 0 ▶ Try::Tiny::SmartCatch::ScopeGuard->_new($_, $failed ? $error : ())
Branch: 1 branch
BranchTrueFalseTotal
$failed ? : 0 0 0
100 } @finally
101 );
102  
103 ## at this point $failed contains a true value if the eval died, even if some
104 ## destructor overwrote $@ as the eval was unwinding.
105 0 ▶ if ($failed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($failed) { } 0 0 0
106 ## if we got an error, invoke the catch block.
107 0 ▶ if (scalar(@catch_when) || $catch_default) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @catch_when or $catch_default) 0 0 0
Condition0%0/3
scalar @catch_when or $catch_default
l!l&&r!l&&!r
000
MC/DC0%0/2
scalar @catch_when or $catch_default
scalar @catch_when $catch_default
Truth table0%0/3
A or B
A= scalar @catch_whenB= $catch_default
ABresult
1X1
011
000
108  
109 ## This works like given($error), but is backwards compatible and
110 ## sets $_ in the dynamic scope for the body of $catch
111 0 for ($error) {
112 0 my ($catch_data);
113 0 foreach $catch_data (@catch_when) {
114 0 ▶ return &{$$catch_data{code}}($error)
Branch: 1 branch
BranchTrueFalseTotal
if $catch_data->for_error($error) 0 0 0
115 if ($catch_data->for_error($error));
116 }
117  
118 0 ▶ return &$catch_default($error)
Branch: 1 branch
BranchTrueFalseTotal
if $catch_default 0 0 0
119 if ($catch_default);
120  
121 0 die($error);
122 }
123 }
124  
125 0 return;
126 }
127  
128 ## no failure, $@ is back to what it was, everything is fine
129 else {
130 ## do we have then block? if we does, execute it in correct context
131 0 ▶ if ($then) {
Branch: 1 branch
BranchTrueFalseTotal
if ($then) 0 0 0
132 0 ▶ if ($wantarray) {
Branch: 2 branches
BranchTrueFalseTotal
if ($wantarray) { } 0 0 0
elsif (defined $wantarray) { } 0 0 0
133 0 @ret = &$then(@ret);
134 }
135 elsif (defined($wantarray)) {
136 0 $ret[0] = &$then(@ret);
137 }
138 else {
139 0 &$then(@ret);
140 }
141 }
142  
143 0 ▶ return if (!defined($wantarray));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $wantarray 0 0 0
144 0 ▶ return $wantarray ? @ret : $ret[0];
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 0 0 0
145 }
146 }
147  
148 0 ▶sub catch_when ($$;@) {
Subroutine catch_when: not called CC=1 SCAR=6.9
Pod documented
149 0 my ($types, $block) = (shift(@_), shift(@_));
150  
151 0 my $catch = Try::Tiny::SmartCatch::Catch::When->new($block, $types);
152  
153 0 return ($catch, @_);
154 }
155  
156 0 ▶sub catch_default ($;@) {
Subroutine catch_default: not called CC=1 SCAR=6.9
Pod documented
157 0 my $block = shift(@_);
158  
159 0 my $catch = Try::Tiny::SmartCatch::Catch::Default->new($block);
160  
161 0 return ($catch, @_);
162 }
163  
164 0 ▶sub then ($;@) {
Subroutine then: not called CC=1 SCAR=6.9
Pod documented
165 0 my $block = shift(@_);
166  
167 0 my $then = bless(\$block, 'Try::Tiny::SmartCatch::Then');
168  
169 0 return ($then, @_);
170 }
171  
172 0 ▶sub finally ($;@) {
Subroutine finally: not called CC=1 SCAR=6.9
Pod documented
173 0 my $block = shift(@_);
174  
175 0 my $finally = bless(\$block, 'Try::Tiny::SmartCatch::Finally');
176  
177 0 return ($finally, @_);
178 }
179  
180 0 ▶sub throw {
Subroutine throw: not called CC=1 SCAR=6.9
Pod documented
181 0 return die (@_);
182 }
183  
184 package # hide from PAUSE
185 Try::Tiny::SmartCatch::ScopeGuard;
186 {
187  
188 0 ▶ sub _new {
Subroutine _new: not called CC=1 SCAR=6.9
189 0 shift(@_);
190 0 return bless([ @_ ]);
191 }
192  
193 0 ▶ sub DESTROY {
Subroutine DESTROY: not called CC=1 SCAR=6.9
194 0 my ($guts) = @_;
195  
196 0 my $code = shift(@$guts);
197 0 return &$code(@$guts);
198 }
199 }
200  
201 2 package Try::Tiny::SmartCatch::Catch::Default;
202 {
203 0 ▶ sub new {
Subroutine new: not called CC=1 SCAR=6.9
204 0 my ($class, $code) = @_;
205  
206 0 my $self = { code => $code };
207 0 $self = bless($self, $class);
208  
209 0 return $self;
210 }
211 }
212  
213 2 package Try::Tiny::SmartCatch::Catch::When;
214 {
215 2 ▶ use Scalar::Util qw/blessed/;
Subroutine BEGIN: called CC=1 SCAR=0.0
216  
217 0 ▶ sub new {
Subroutine new: not called CC=3 SCAR=24.8
218 0 my ($class, $code, $types) = @_;
219  
220 0 ▶ my $self = {
Branch: 2 branches
BranchTrueFalseTotal
ref $types eq 'ARRAY' ? : 0 0 0
defined $types ? : 0 0 0
221 code => $code,
222 types => (
223 ref($types) eq 'ARRAY' ? $types :
224 defined($types) ? [$types] :
225 []
226 ),
227 };
228  
229 0 return bless($self, $class);
230 }
231  
232 0 ▶ sub for_error {
Subroutine for_error: not called CC=11 SCAR=48.8
233 0 my ($self, $error, $types) = @_;
234  
235 $types = $$self{types}
236 0 ▶ if (!defined($types));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $types 0 0 0
237 0 ▶ $types = [$types]
Branch: 1 branch
BranchTrueFalseTotal
if ref $types ne "ARRAY" 0 0 0
238 if (ref($types) ne 'ARRAY');
239  
240 0 ▶ if (blessed($error)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($error)) { } 0 0 0
241 0 foreach (@$types) {
242 0 ▶ return 1 if ($error->isa($_));
Branch: 1 branch
BranchTrueFalseTotal
if $error->isa($_) 0 0 0
243 }
244 }
245 else {
246 0 my $type;
247 0 foreach $type (@$types) {
248 0 ▶ return 1 if (
Branch: 1 branch
BranchTrueFalseTotal
if ref $type eq "Regexp" and $error =~ /$type/ or not ref $type and index($error, $type) != -1 0 0 0
Condition0%0/9
ref $type eq "Regexp" and $error =~ /$type/ or not ref $type and index($error, $type) != -1
l!l&&r!l&&!r
000
ref $type eq "Regexp" and $error =~ /$type/
!ll&&!rl&&r
000
not ref $type and index($error, $type) != -1
!ll&&!rl&&r
000
MC/DC0%0/4
ref $type eq "Regexp" and $error =~ /$type/ or not ref $type and index($error, $type) != -1
ref $type eq "Regexp" $error =~ /$type/ not ref $type index($error, $type) != -1
Truth table0%0/7
A and B or not(C) and D
A= ref $type eq "Regexp"B= $error =~ /$type/C= not ref $typeD= index($error, $type) != -1
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
249 (ref($type) eq 'Regexp' && $error =~ /$type/) ||
250 (!ref($type) && index($error, $type) > -1)
251 );
252 }
253 }
254  
255 0 return;
256 }
257  
258 }
259  
260  
261 2 1;
262  
263 __END__
↑ Summary