Venus/Try.pm

filter: Statementstmt 99.0% 104 / 105 Branchbran 88.2% 30 / 34 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 23 / 23 Podpod 86.6% 13 / 15 totaltotal 96.1% 175 / 182 CC 20 SCAR 15.3
CC
20
Cov
96%
result
11.0
callback
5.0
execute
5.0
CRAP 4.6
« Venus/Json.pm ↑ Summary Venus/Yaml.pm »
1 package Venus::Try;
2  
3 61 ▶use 5.018;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 61 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 61 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 61 ▶use Venus::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 61 base 'Venus::Kind::Utility';
11  
12 61 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # ATTRIBUTES
15  
16 61 attr 'invocant';
17 61 attr 'arguments';
18 61 attr 'on_try';
19 61 attr 'on_catch';
20 61 attr 'on_default';
21 61 attr 'on_finally';
22  
23 # BUILDERS
24  
25 sub build_arg {
26 4 ▶ my ($self, $data) = @_;
Subroutine build_arg: called CC=1 SCAR=0.0
Pod undocumented
27  
28 return {
29 4 on_try => $data,
30 };
31 }
32  
33 sub build_self {
34 1971 ▶ my ($self, $data) = @_;
Subroutine build_self: called CC=2 SCAR=7.1
Pod undocumented
35  
36 1971 ▶ $self->on_catch([]) if !defined $self->on_catch;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->on_catch 1971 0 1971
37  
38 1971 return $self;
39 }
40  
41 # METHODS
42  
43 sub call {
44 1962 ▶ my ($self, $callback) = @_;
Subroutine call: called CC=1 SCAR=0.0
Pod documented
45  
46 1962 $self->on_try($self->callback($callback));
47  
48 1962 return $self;
49 }
50  
51 sub callback {
52 1975 ▶ my ($self, $callback) = @_;
Subroutine callback: called CC=5 SCAR=16.1
Pod documented
53  
54 1975 ▶ if (not(UNIVERSAL::isa($callback, 'CODE'))) {
Branch: 1 branch
BranchTrueFalseTotal
unless (UNIVERSAL::isa($callback, "CODE")) 1949 26 1975
55 1949 my $invocant = $self->invocant;
56 1949 ▶ my $method = $invocant ? $invocant->can($callback) : $self->can($callback);
Branch: 1 branch
BranchTrueFalseTotal
$invocant ? : 1948 1 1949
57  
58 1949 ▶ $self->throw->error({
Branch: 2 branches
BranchTrueFalseTotal
unless $method 1 1948 1949
$invocant ? : 0 1 1
59 message => sprintf(qq(Can't locate object method "%s" on package "%s"),
60 ($callback, $invocant ? ref($invocant) : ref($self)))
61 })
62 if !$method;
63  
64 1948 ▶ $callback = sub {goto $method};
Subroutine __ANON__: called CC=1 SCAR=0.0
65 }
66  
67 1974 return $callback;
68 }
69  
70 sub catch {
71 3 ▶ my ($self, $package, $callback) = @_;
Subroutine catch: called CC=1 SCAR=0.0
Pod documented
72  
73 3 push @{$self->on_catch}, [$package, $self->callback($callback)];
74  
75 3 return $self;
76 }
77  
78 sub default {
79 4 ▶ my ($self, $callback) = @_;
Subroutine default: called CC=1 SCAR=0.0
Pod documented
80  
81 4 $self->on_default($self->callback($callback));
82  
83 4 return $self;
84 }
85  
86 sub error {
87 95 ▶ my ($self, $variable) = @_;
Subroutine error: called CC=1 SCAR=0.0
Pod documented
88  
89 95 ▶ $self->on_default(sub{($$variable) = @_});
Subroutine __ANON__: called CC=1 SCAR=0.0
90  
91 95 return $self;
92 }
93  
94 sub execute {
95 1949 ▶ my ($self, $callback, @args) = @_;
Subroutine execute: called CC=5 SCAR=16.1
Pod documented
96  
97 1928 unshift @args, @{$self->arguments}
98 1949 ▶ if $self->arguments && @{$self->arguments};
Branch: 1 branch
BranchTrueFalseTotal
if $self->arguments and @{$self->arguments;} 1928 21 1949
Condition100%3/3
$self->arguments and @{$self->arguments;}
!ll&&!rl&&r
1561928
MC/DC100%2/2
$self->arguments and @{$self->arguments;}
$self->arguments @{$self->arguments;}
Truth table100%3/3
A and B
A= $self->argumentsB= @{$self->arguments;}
ABresult
0X0
100
111
99 1949 ▶ unshift @args, $self->invocant
Branch: 1 branch
BranchTrueFalseTotal
if $self->invocant 1934 15 1949
100 if $self->invocant;
101  
102 1949 ▶ return wantarray ? ($callback->(@args)) : $callback->(@args);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1946 3 1949
103 }
104  
105 sub finally {
106 3 ▶ my ($self, $callback) = @_;
Subroutine finally: called CC=1 SCAR=0.0
Pod documented
107  
108 3 $self->on_finally($self->callback($callback));
109  
110 3 return $self;
111 }
112  
113 sub maybe {
114 1 ▶ my ($self) = @_;
Subroutine maybe: called CC=1 SCAR=0.0
Pod documented
115  
116 1 ▶ $self->on_default(sub{''});
Subroutine __ANON__: called CC=1 SCAR=0.0
117  
118 1 return $self;
119 }
120  
121 sub no_catch {
122 1 ▶ my ($self) = @_;
Subroutine no_catch: called CC=1 SCAR=0.0
Pod documented
123  
124 1 $self->on_catch([]);
125  
126 1 return $self;
127 }
128  
129 sub no_default {
130 2 ▶ my ($self) = @_;
Subroutine no_default: called CC=1 SCAR=0.0
Pod documented
131  
132 2 $self->on_default(undef);
133  
134 2 return $self;
135 }
136  
137 sub no_finally {
138 1 ▶ my ($self) = @_;
Subroutine no_finally: called CC=1 SCAR=0.0
Pod documented
139  
140 1 $self->on_finally(undef);
141  
142 1 return $self;
143 }
144  
145 sub no_try {
146 1 ▶ my ($self) = @_;
Subroutine no_try: called CC=1 SCAR=0.0
Pod documented
147  
148 1 $self->on_try(undef);
149  
150 1 return $self;
151 }
152  
153 sub result {
154 1946 ▶ my ($self, @args) = @_;
Subroutine result: called CC=11 SCAR=24.0
Pod documented
155  
156 1946 my $dollarat = $@;
157 1946 my @returned;
158  
159 # try
160 1946 my $error = do {
161 1946 local $@;
162 1946 eval {
163 1946 my $tryer = $self->on_try;
164 1946 @returned = ($self->execute($tryer, @args));
165 };
166 1946 $@;
167 };
168  
169 # catch
170 1946 ▶ if ($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 60 1886 1946
171 60 my $caught = $error;
172 60 my $catchers = $self->on_catch;
173 60 my $default = $self->on_default;
174  
175 60 for my $catcher (@$catchers) {
176 1 ▶ if (UNIVERSAL::isa($caught, $catcher->[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($caught, $catcher->[0])) 1 0 1
177 1 @returned = ($catcher->[1]->($caught));
178 1 last;
179 }
180 }
181  
182 # catchall
183 60 ▶ if(!@returned) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@returned) 59 1 60
184 59 ▶ if ($default) {
Branch: 1 branch
BranchTrueFalseTotal
if ($default) 55 4 59
185 55 @returned = ($default->($caught))
186 }
187 }
188  
189 # uncaught
190 60 ▶ if(!@returned) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@returned) 4 56 60
191 4 ▶ if (Scalar::Util::blessed($caught)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($caught)) { } 2 2 4
192 2 die $caught;
193 }
194 else {
195 2 ▶ if (UNIVERSAL::isa($caught, 'Venus::Error')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($caught, 'Venus::Error')) { } 0 2 2
196 0 $caught->throw;
197 }
198 else {
199 2 require Venus::Error;
200 2 Venus::Error->throw($caught);
201 }
202 }
203 }
204 }
205  
206 # finally
207 1942 ▶ if (my $finally = $self->on_finally) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $finally = $self->on_finally) 2 1940 1942
208 2 $self->execute($finally, @args);
209 }
210  
211 1942 $@ = $dollarat;
212  
213 1942 ▶ return wantarray ? (@returned) : $returned[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 76 1866 1942
214 }
215  
216 61 1;
217  
218  
219  
220 =head1 NAME
221  
222 Venus::Try - Try Class
223  
224 =cut
225  
226 =head1 ABSTRACT
227  
228 Try Class for Perl 5
229  
230 =cut
231  
232 =head1 SYNOPSIS
233  
234 package main;
235  
236 use Venus::Try;
237  
238 my $try = Venus::Try->new;
239  
240 $try->call(sub {
241 my (@args) = @_;
242  
243 # try something
244  
245 return time;
246 });
247  
248 $try->catch('Example::Error', sub {
249 my ($caught) = @_;
250  
251 # caught an error (exception)
252  
253 return;
254 });
255  
256 $try->default(sub {
257 my ($caught) = @_;
258  
259 # catch the uncaught
260  
261 return;
262 });
263  
264 $try->finally(sub {
265 my (@args) = @_;
266  
267 # always run after try/catch
268  
269 return;
270 });
271  
272 my @args;
273  
274 my $result = $try->result(@args);
275  
276 =cut
277  
278 =head1 DESCRIPTION
279  
280 This package provides an object-oriented interface for performing complex
281 try/catch operations.
282  
283 =cut
284  
285 =head1 ATTRIBUTES
286  
287 This package has the following attributes:
288  
289 =cut
290  
291 =head2 invocant
292  
293 invocant(Object)
294  
295 This attribute is read-only, accepts C<(Object)> values, and is optional.
296  
297 =cut
298  
299 =head2 arguments
300  
301 arguments(ArrayRef)
302  
303 This attribute is read-only, accepts C<(ArrayRef)> values, and is optional.
304  
305 =cut
306  
307 =head2 on_try
308  
309 on_try(CodeRef)
310  
311 This attribute is read-write, accepts C<(CodeRef)> values, and is optional.
312  
313 =cut
314  
315 =head2 on_catch
316  
317 on_catch(ArrayRef[CodeRef])
318  
319 This attribute is read-write, accepts C<(ArrayRef[CodeRef])> values, is optional, and defaults to C<[]>.
320  
321 =cut
322  
323 =head2 on_default
324  
325 on_default(CodeRef)
326  
327 This attribute is read-write, accepts C<(CodeRef)> values, and is optional.
328  
329 =cut
330  
331 =head2 on_finally
332  
333 on_finally(CodeRef)
334  
335 This attribute is read-write, accepts C<(CodeRef)> values, and is optional.
336  
337 =cut
338  
339 =head1 INHERITS
340  
341 This package inherits behaviors from:
342  
343 L<Venus::Kind::Utility>
344  
345 =cut
346  
347 =head1 METHODS
348  
349 This package provides the following methods:
350  
351 =cut
352  
353 =head2 call
354  
355 call(Str | CodeRef $method) (Try)
356  
357 The call method takes a method name or coderef, registers it as the tryable
358 routine, and returns the object. When invoked, the callback will received an
359 C<invocant> if one was provided to the constructor, the default C<arguments> if
360 any were provided to the constructor, and whatever arguments were provided by
361 the invocant.
362  
363  
364  
365  
366 I<Since C<0.01>>
367  
368 =over 4
369  
370 =item call example 1
371  
372 package main;
373  
374 use Venus::Try;
375  
376 my $try = Venus::Try->new;
377  
378 my $call = $try->call(sub {
379 my (@args) = @_;
380  
381 return [@args];
382 });
383  
384 # bless({ on_catch => ... }, "Venus::Try")
385  
386 =back
387  
388 =cut
389  
390 =head2 callback
391  
392 callback(Str | CodeRef $method) (CodeRef)
393  
394 The callback method takes a method name or coderef, and returns a coderef for
395 registration. If a coderef is provided this method is mostly a passthrough.
396  
397 I<Since C<0.01>>
398  
399 =over 4
400  
401 =item callback example 1
402  
403 package main;
404  
405 use Venus::Try;
406  
407 my $try = Venus::Try->new;
408  
409 my $callback = $try->callback(sub {
410 my (@args) = @_;
411  
412 return [@args];
413 });
414  
415 # sub { ... }
416  
417 =back
418  
419 =over 4
420  
421 =item callback example 2
422  
423 package Example1;
424  
425 sub new {
426 bless {};
427 }
428  
429 sub test {
430 my (@args) = @_;
431  
432 return [@args];
433 }
434  
435 package main;
436  
437 use Venus::Try;
438  
439 my $try = Venus::Try->new(
440 invocant => Example1->new,
441 );
442  
443 my $callback = $try->callback('test');
444  
445 # sub { ... }
446  
447 =back
448  
449 =over 4
450  
451 =item callback example 3
452  
453 package main;
454  
455 use Venus::Try;
456  
457 my $try = Venus::Try->new;
458  
459 my $callback = $try->callback('missing_method');
460  
461 # Exception! Venus::Try::Error (isa Venus::Error)
462  
463 =back
464  
465 =cut
466  
467 =head2 catch
468  
469 catch(Str $isa, Str | CodeRef $method) (Try)
470  
471 The catch method takes a package or ref name, and when triggered checks whether
472 the captured exception is of the type specified and if so executes the given
473 callback.
474  
475 I<Since C<0.01>>
476  
477 =over 4
478  
479 =item catch example 1
480  
481 package main;
482  
483 use Venus::Try;
484  
485 my $try = Venus::Try->new;
486  
487 $try->call(sub {
488 my (@args) = @_;
489  
490 die $try;
491 });
492  
493 my $catch = $try->catch('Venus::Try', sub {
494 my (@args) = @_;
495  
496 return [@args];
497 });
498  
499 # bless({ on_catch => ... }, "Venus::Try")
500  
501 =back
502  
503 =cut
504  
505 =head2 default
506  
507 default(Str | CodeRef $method) (Try)
508  
509 The default method takes a method name or coderef and is triggered if no
510 C<catch> conditions match the exception thrown.
511  
512 I<Since C<0.01>>
513  
514 =over 4
515  
516 =item default example 1
517  
518 package main;
519  
520 use Venus::Try;
521  
522 my $try = Venus::Try->new;
523  
524 $try->call(sub {
525 my (@args) = @_;
526  
527 die $try;
528 });
529  
530 my $default = $try->default(sub {
531 my (@args) = @_;
532  
533 return [@args];
534 });
535  
536 # bless({ on_catch => ... }, "Venus::Try")
537  
538 =back
539  
540 =cut
541  
542 =head2 error
543  
544 error(Ref $variable) (Try)
545  
546 The error method takes a scalar reference and assigns any uncaught exceptions
547 to it during execution.
548  
549 I<Since C<0.01>>
550  
551 =over 4
552  
553 =item error example 1
554  
555 package main;
556  
557 use Venus::Try;
558  
559 my $try = Venus::Try->new;
560  
561 $try->call(sub {
562 my (@args) = @_;
563  
564 die $try;
565 });
566  
567 my $error = $try->error(\my $object);
568  
569 # bless({ on_catch => ... }, "Venus::Try")
570  
571 =back
572  
573 =cut
574  
575 =head2 execute
576  
577 execute(CodeRef $code, Any @args) (Any)
578  
579 The execute method takes a coderef and executes it with any given arguments.
580 When invoked, the callback will received an C<invocant> if one was provided to
581 the constructor, the default C<arguments> if any were provided to the
582 constructor, and whatever arguments were passed directly to this method. This
583 method can return a list of values in list-context.
584  
585 I<Since C<0.01>>
586  
587 =over 4
588  
589 =item execute example 1
590  
591 package Example2;
592  
593 sub new {
594 bless {};
595 }
596  
597 package main;
598  
599 use Venus::Try;
600  
601 my $try = Venus::Try->new(
602 invocant => Example2->new,
603 arguments => [1,2,3],
604 );
605  
606 my $execute = $try->execute(sub {
607 my (@args) = @_;
608  
609 return [@args];
610 });
611  
612 # [bless({}, "Example2"), 1, 2, 3]
613  
614 =back
615  
616 =cut
617  
618 =head2 finally
619  
620 finally(Str | CodeRef $method) (Try)
621  
622 The finally method takes a package or ref name and always executes the callback
623 after a try/catch operation. The return value is ignored. When invoked, the
624 callback will received an C<invocant> if one was provided to the constructor,
625 the default C<arguments> if any were provided to the constructor, and whatever
626 arguments were provided by the invocant.
627  
628 I<Since C<0.01>>
629  
630 =over 4
631  
632 =item finally example 1
633  
634 package Example3;
635  
636 sub new {
637 bless {};
638 }
639  
640 package main;
641  
642 use Venus::Try;
643  
644 my $try = Venus::Try->new(
645 invocant => Example3->new,
646 arguments => [1,2,3],
647 );
648  
649 $try->call(sub {
650 my (@args) = @_;
651  
652 return $try;
653 });
654  
655 my $finally = $try->finally(sub {
656 my (@args) = @_;
657  
658 $try->{args} = [@args];
659 });
660  
661 # bless({ on_catch => ... }, "Venus::Try")
662  
663 =back
664  
665 =cut
666  
667 =head2 maybe
668  
669 maybe() (Try)
670  
671 The maybe method registers a default C<catch> condition that returns falsy,
672 i.e. an empty string, if an exception is encountered.
673  
674 I<Since C<0.01>>
675  
676 =over 4
677  
678 =item maybe example 1
679  
680 package main;
681  
682 use Venus::Try;
683  
684 my $try = Venus::Try->new;
685  
686 $try->call(sub {
687 my (@args) = @_;
688  
689 die $try;
690 });
691  
692 my $maybe = $try->maybe;
693  
694 # bless({ on_catch => ... }, "Venus::Try")
695  
696 =back
697  
698 =cut
699  
700 =head2 no_catch
701  
702 no_catch() (Try)
703  
704 The no_catch method removes any configured catch conditions and returns the
705 object.
706  
707 I<Since C<0.01>>
708  
709 =over 4
710  
711 =item no_catch example 1
712  
713 package main;
714  
715 use Venus::Try;
716  
717 my $try = Venus::Try->new;
718  
719 $try->call(sub {
720 my (@args) = @_;
721  
722 die $try;
723 });
724  
725 $try->catch('Venus::Try', sub {
726 my (@args) = @_;
727  
728 return [@args];
729 });
730  
731  
732 my $no_catch = $try->no_catch;
733  
734 # bless({ on_catch => ... }, "Venus::Try")
735  
736 =back
737  
738 =cut
739  
740 =head2 no_default
741  
742 no_default() (Try)
743  
744 The no_default method removes any configured default condition and returns the
745 object.
746  
747 I<Since C<0.01>>
748  
749 =over 4
750  
751 =item no_default example 1
752  
753 package main;
754  
755 use Venus::Try;
756  
757 my $try = Venus::Try->new;
758  
759 $try->call(sub {
760 my (@args) = @_;
761  
762 die $try;
763 });
764  
765 my $default = $try->default(sub {
766 my (@args) = @_;
767  
768 return [@args];
769 });
770  
771 my $no_default = $try->no_default;
772  
773 # bless({ on_catch => ... }, "Venus::Try")
774  
775 =back
776  
777 =cut
778  
779 =head2 no_finally
780  
781 no_finally() (Try)
782  
783 The no_finally method removes any configured finally condition and returns the
784 object.
785  
786 I<Since C<0.01>>
787  
788 =over 4
789  
790 =item no_finally example 1
791  
792 package Example4;
793  
794 sub new {
795 bless {};
796 }
797  
798 package main;
799  
800 use Venus::Try;
801  
802 my $try = Venus::Try->new(
803 invocant => Example4->new,
804 arguments => [1,2,3],
805 );
806  
807 $try->call(sub {
808 my (@args) = @_;
809  
810 return $try;
811 });
812  
813 $try->finally(sub {
814 my (@args) = @_;
815  
816 $try->{args} = [@args];
817 });
818  
819 my $no_finally = $try->no_finally;
820  
821 # bless({ on_catch => ... }, "Venus::Try")
822  
823 =back
824  
825 =cut
826  
827 =head2 no_try
828  
829 no_try() (Try)
830  
831 The no_try method removes any configured C<try> operation and returns the
832 object.
833  
834 I<Since C<0.01>>
835  
836 =over 4
837  
838 =item no_try example 1
839  
840 package main;
841  
842 use Venus::Try;
843  
844 my $try = Venus::Try->new;
845  
846 $try->call(sub {
847 my (@args) = @_;
848  
849 return [@args];
850 });
851  
852 my $no_try = $try->no_try;
853  
854 # bless({ on_catch => ... }, "Venus::Try")
855  
856 =back
857  
858 =cut
859  
860 =head2 result
861  
862 result(Any @args) (Any)
863  
864 The result method executes the try/catch/default/finally logic and returns
865 either 1) the return value from the successfully tried operation 2) the return
866 value from the successfully matched catch condition if an exception was thrown
867 3) the return value from the default catch condition if an exception was thrown
868 and no catch condition matched. When invoked, the C<try> and C<finally>
869 callbacks will received an C<invocant> if one was provided to the constructor,
870 the default C<arguments> if any were provided to the constructor, and whatever
871 arguments were passed directly to this method. This method can return a list of
872 values in list-context.
873  
874 I<Since C<0.01>>
875  
876 =over 4
877  
878 =item result example 1
879  
880 package main;
881  
882 use Venus::Try;
883  
884 my $try = Venus::Try->new;
885  
886 $try->call(sub {
887 my (@args) = @_;
888  
889 return [@args];
890 });
891  
892 my $result = $try->result;
893  
894 # []
895  
896 =back
897  
898 =over 4
899  
900 =item result example 2
901  
902 package main;
903  
904 use Venus::Try;
905  
906 my $try = Venus::Try->new;
907  
908 $try->call(sub {
909 my (@args) = @_;
910  
911 return [@args];
912 });
913  
914 my $result = $try->result(1..5);
915  
916 # [1..5]
917  
918 =back
919  
920 =over 4
921  
922 =item result example 3
923  
924 package main;
925  
926 use Venus::Try;
927  
928 my $try = Venus::Try->new;
929  
930 $try->call(sub {die});
931  
932 my $result = $try->result;
933  
934 # Exception! Venus::Error
935  
936 =back
937  
938 =cut
« Venus/Json.pm ↑ Summary Venus/Yaml.pm »