blib/lib/Venus/Role/Catchable.pm

filter: Statementstmt 96.6% 29 / 30 Branchbran 90.0% 18 / 20 Conditioncond 75.0% 12 / 16 MC/DCmcdc 42.8% 3 / 7 Subroutinesub 100% 9 / 9 Podpod 60.0% 3 / 5 totaltotal 85.0% 74 / 87 CC 17 SCAR 18.5
CC
17
Cov
89%
caught
11.3
maybe
4.0
catch
3.0
CRAP 6.3
« blib/lib/Venus/Template.pm ↑ Summary blib/lib/Venus/Range.pm »
1 package Venus::Role::Catchable;
2  
3 119 ▶use 5.018;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 119 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 119 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # IMPORTS
9  
10 119 ▶use Venus::Role 'fault';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # AUDITS
13  
14 128 ▶sub AUDIT {
Subroutine AUDIT: called CC=2 SCAR=7.6
Pod undocumented
15 128 my ($self, $from) = @_;
16  
17 128 ▶ if (!$from->does('Venus::Role::Tryable')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($from->does("Venus::Role::Tryable")) 0 128 128
18 0 fault "${self} requires ${from} to consume Venus::Role::Tryable";
19 }
20  
21 128 return $self;
22 }
23  
24 # METHODS
25  
26 12700 ▶sub catch {
Subroutine catch: called CC=3 SCAR=11.0
Pod documented
27 12700 my ($self, $method, @args) = @_;
28  
29 12700 my @result = $self->try($method, @args)->error(\my $error)->result;
30  
31 12700 ▶ return wantarray ? ($error ? ($error, undef) : ($error, @result)) : $error;
Branch: 2 branches
BranchTrueFalseTotal
wantarray ? : 12356 344 12700
$error ? : 9 12347 12356
32 }
33  
34 8 ▶sub caught {
Subroutine caught: called CC=11 SCAR=24.2
Pod documented
35 8 my ($self, $data, $type, $code) = @_;
36  
37 8 require Scalar::Util;
38  
39 8 ▶ ($type, my($name)) = @$type if ref $type eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $type eq "ARRAY" 4 4 8
40  
41 8 ▶ my $is_true = $data
Branch: 1 branch
BranchTrueFalseTotal
$data->name ? : 2 5 7
Condition75%12/16
$data && builtin::blessed($data) && $data->isa('Venus::Error') && $data->isa($type || 'Venus::Error') && ($data->name ? $data->of($name || '') : !$name)
!ll&&!rl&&r
125
$data and builtin::blessed($data) and $data->isa("Venus::Error") and $data->isa($type || "Venus::Error")
!ll&&!rl&&r
107
$data and builtin::blessed($data) and $data->isa("Venus::Error")
!ll&&!rl&&r
107
$data and builtin::blessed($data)
!ll&&!rl&&r
107
$type || 'Venus::Error'
l!l
61
$name || ''
l!l
20
MC/DC40%2/5
$data && builtin::blessed($data) && $data->isa('Venus::Error') && $data->isa($type || 'Venus::Error') && ($data->name ? $data->of($name || '') : !$name)
$data builtin::blessed($data) $data->isa("Venus::Error") $data->isa($type || "Venus::Error") ($data->name ? $data->of($name || '') : !$name)
MC/DC100%1/1
$type || 'Venus::Error'
$type
MC/DC0%0/1
$name || ''
$name
Truth table50%3/6
A and B and C and D && E
A= $dataB= builtin::blessed($data)C= $data->isa("Venus::Error")D= $data->isa($type || "Venus::Error")E= ($data->name ? $data->of($name || '') : !$name)
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
Truth table100%2/2
A
A= $type
Aresult
11
00
Truth table50%1/2
A
A= $name
Aresult
11
00
42 && Scalar::Util::blessed($data)
43 && $data->isa('Venus::Error')
44 && $data->isa($type || 'Venus::Error')
45 && ($data->name ? $data->of($name || '') : !$name);
46  
47 8 ▶ return undef unless $is_true;
Branch: 1 branch
BranchTrueFalseTotal
unless $is_true 3 5 8
48  
49 5 local $_ = $data;
50 5 ▶ return $code ? $code->($data) : $data;
Branch: 1 branch
BranchTrueFalseTotal
$code ? : 2 3 5
51 }
52  
53 3 ▶sub maybe {
Subroutine maybe: called CC=4 SCAR=13.9
Pod documented
54 3 my ($self, $method, @args) = @_;
55  
56 3 my @result = $self->try($method, @args)->error(\my $error)->result;
57  
58 3 ▶ return wantarray ? ($error ? (undef) : (@result)) : ($error ? undef : $result[0]);
Branch: 3 branches
BranchTrueFalseTotal
wantarray ? : 1 2 3
$error ? : 0 1 1
$error ? : 1 1 2
59 }
60  
61 # EXPORTS
62  
63 128 ▶sub EXPORT {
Subroutine EXPORT: called CC=1 SCAR=0.0
Pod undocumented
64 128 ['catch', 'caught', 'maybe']
65 }
66  
67 119 1;
68  
69  
70  
71 =head1 NAME
72  
73 Venus::Role::Catchable - Catchable Role
74  
75 =cut
76  
77 =head1 ABSTRACT
78  
79 Catchable Role for Perl 5
80  
81 =cut
82  
83 =head1 SYNOPSIS
84  
85 package Example;
86  
87 use Venus::Class;
88  
89 use Venus 'error';
90  
91 with 'Venus::Role::Tryable';
92 with 'Venus::Role::Catchable';
93  
94 sub pass {
95 true;
96 }
97  
98 sub fail {
99 error;
100 }
101  
102 package main;
103  
104 my $example = Example->new;
105  
106 # my $error = $example->catch('fail');
107  
108 =cut
109  
110 =head1 DESCRIPTION
111  
112 This package modifies the consuming package and provides methods for trapping
113 errors thrown from dispatched method calls.
114  
115 =cut
116  
117 =head1 METHODS
118  
119 This package provides the following methods:
120  
121 =cut
122  
123 =head2 catch
124  
125 catch(string $method, any @args) (any)
126  
127 The catch method traps any errors raised by executing the dispatched method
128 call and returns the error string or error object. This method can return a
129 list of values in list-context. This method supports dispatching, i.e.
130 providing a method name and arguments whose return value will be acted on by
131 this method.
132  
133 I<Since C<0.01>>
134  
135 =over 4
136  
137 =item catch example 1
138  
139 package main;
140  
141 my $example = Example->new;
142  
143 my $catch = $example->catch('fail');
144  
145 # bless({...}, "Venus::Error")
146  
147 =back
148  
149 =over 4
150  
151 =item catch example 2
152  
153 package main;
154  
155 my $example = Example->new;
156  
157 my $catch = $example->catch('pass');
158  
159 # undef
160  
161 =back
162  
163 =over 4
164  
165 =item catch example 3
166  
167 package main;
168  
169 my $example = Example->new;
170  
171 my ($catch, $result) = $example->catch('pass');
172  
173 # (undef, 1)
174  
175 =back
176  
177 =over 4
178  
179 =item catch example 4
180  
181 package main;
182  
183 my $example = Example->new;
184  
185 my ($catch, $result) = $example->catch('fail');
186  
187 # (bless({...}, "Venus::Error"), undef)
188  
189 =back
190  
191 =cut
192  
193 =head2 caught
194  
195 caught(object $error, string | tuple[string, string] $identity, coderef $block) (any)
196  
197 The caught method evaluates the value provided and validates its identity and
198 name (if provided) then executes the code block (if provided) returning the
199 result of the callback. If no callback is provided this function returns the
200 exception object on success and C<undef> on failure.
201  
202 I<Since C<4.15>>
203  
204 =over 4
205  
206 =item caught example 1
207  
208 package main;
209  
210 my $example = Example->new;
211  
212 my $catch = $example->catch('fail');
213  
214 my $result = $example->caught($catch);
215  
216 # bless(..., 'Venus::Error')
217  
218 =back
219  
220 =over 4
221  
222 =item caught example 2
223  
224 package main;
225  
226 my $example = Example->new;
227  
228 my $catch = $example->catch('fail');
229  
230 my $result = $example->caught($catch, 'Venus::Error');
231  
232 # bless(..., 'Venus::Error')
233  
234 =back
235  
236 =over 4
237  
238 =item caught example 3
239  
240 package main;
241  
242 my $example = Example->new;
243  
244 my $catch = $example->catch('fail');
245  
246 my $result = $example->caught($catch, 'Venus::Error', sub{
247 $example->{caught} = $_;
248 });
249  
250 ($example, $result)
251  
252 # (bless(..., 'Example'), bless(..., 'Venus::Error'))
253  
254 =back
255  
256 =over 4
257  
258 =item caught example 4
259  
260 package main;
261  
262 my $example = Example->new;
263  
264 my $catch = $example->catch('fail');
265  
266 $catch->name('on.issue');
267  
268 my $result = $example->caught($catch, ['Venus::Error', 'on.issue']);
269  
270 # bless(..., 'Venus::Error')
271  
272 =back
273  
274 =over 4
275  
276 =item caught example 5
277  
278 package main;
279  
280 my $example = Example->new;
281  
282 my $catch = $example->catch('fail');
283  
284 $catch->name('on.issue');
285  
286 my $result = $example->caught($catch, ['Venus::Error', 'on.issue'], sub{
287 $example->{caught} = $_;
288 });
289  
290 ($example, $result)
291  
292 # (bless(..., 'Example'), bless(..., 'Venus::Error'))
293  
294 =back
295  
296 =over 4
297  
298 =item caught example 6
299  
300 package main;
301  
302 my $example = Example->new;
303  
304 my $catch = $example->catch('fail');
305  
306 my $result = $example->caught($catch, ['Venus::Error', 'on.issue']);
307  
308 # undef
309  
310 =back
311  
312 =over 4
313  
314 =item caught example 7
315  
316 package main;
317  
318 my $example = Example->new;
319  
320 my $catch;
321  
322 my $result = $example->caught($catch);
323  
324 # undef
325  
326 =back
327  
328 =cut
329  
330 =head2 maybe
331  
332 maybe(string $method, any @args) (any)
333  
334 The maybe method traps any errors raised by executing the dispatched method
335 call and returns undefined on error, effectively supressing the error. This
336 method can return a list of values in list-context. This method supports
337 dispatching, i.e. providing a method name and arguments whose return value
338 will be acted on by this method.
339  
340 I<Since C<2.91>>
341  
342 =over 4
343  
344 =item maybe example 1
345  
346 package main;
347  
348 my $example = Example->new;
349  
350 my $maybe = $example->maybe('fail');
351  
352 # undef
353  
354 =back
355  
356 =over 4
357  
358 =item maybe example 2
359  
360 package main;
361  
362 my $example = Example->new;
363  
364 my $maybe = $example->maybe('pass');
365  
366 # true
367  
368 =back
369  
370 =over 4
371  
372 =item maybe example 3
373  
374 package main;
375  
376 my $example = Example->new;
377  
378 my (@maybe) = $example->maybe(sub {1..4});
379  
380 # (1..4)
381  
382 =back
383  
384 =cut
385  
386 =head1 AUTHORS
387  
388 Awncorp, C<awncorp@cpan.org>
389  
390 =cut
391  
392 =head1 LICENSE
393  
394 Copyright (C) 2022, Awncorp, C<awncorp@cpan.org>.
395  
396 This program is free software, you can redistribute it and/or modify it under
397 the terms of the Apache license version 2.0.
398  
399 =cut
« blib/lib/Venus/Template.pm ↑ Summary blib/lib/Venus/Range.pm »