blib/lib/Venus/Try.pm

filter: Statementstmt 98.5% 134 / 136 Branchbran 89.4% 34 / 38 Conditioncond 50.0% 6 / 12 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 96.5% 28 / 29 Podpod 83.3% 15 / 18 totaltotal 90.8% 219 / 241 CC 25 SCAR 14.2
CC
25
Cov
94%
result
11.0
execute
5.0
error_on_callback
4.5
CRAP 4.1
« blib/lib/Venus/Yaml.pm ↑ Summary blib/lib/Venus/Role/Defaultable.pm »
1 package Venus::Try;
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::Class 'attr', 'base';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # INHERITS
13  
14 119 base 'Venus::Kind::Utility';
15  
16 119 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 # ATTRIBUTES
19  
20 119 attr 'invocant';
21 119 attr 'arguments';
22 119 attr 'on_try';
23 119 attr 'on_catch';
24 119 attr 'on_default';
25 119 attr 'on_finally';
26  
27 # BUILDERS
28  
29 200 ▶sub build_arg {
Subroutine build_arg: called CC=1 SCAR=0.0
Pod undocumented
30 200 my ($self, $data) = @_;
31  
32 return {
33 200 on_try => $data,
34 };
35 }
36  
37 18400 ▶sub build_self {
Subroutine build_self: called CC=2 SCAR=7.1
Pod undocumented
38 18400 my ($self, $data) = @_;
39  
40 18400 ▶ $self->on_catch([]) if !defined $self->on_catch;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->on_catch 18400 0 18400
41  
42 18400 return $self;
43 }
44  
45 # METHODS
46  
47 2 ▶sub any {
Subroutine any: called CC=1 SCAR=0.0
Pod documented
48 2 my ($self) = @_;
49  
50 2 ▶ $self->on_default(sub{(@_)});
Subroutine __ANON__: called CC=1 SCAR=0.0
51  
52 2 return $self;
53 }
54  
55 18194 ▶sub call {
Subroutine call: called CC=1 SCAR=0.0
Pod documented
56 18194 my ($self, $callback) = @_;
57  
58 18194 $self->on_try($self->callback($callback));
59  
60 18194 return $self;
61 }
62  
63 18316 ▶sub callback {
Subroutine callback: called CC=4 SCAR=13.9
Pod documented
64 18316 my ($self, $callback) = @_;
65  
66 18316 ▶ if (not(UNIVERSAL::isa($callback, 'CODE'))) {
Branch: 1 branch
BranchTrueFalseTotal
unless (UNIVERSAL::isa($callback, "CODE")) 18150 166 18316
67 18150 my $method;
68 18150 my $invocant = $self->invocant;
69  
70 18150 ▶ if (defined($invocant)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $invocant) { } 18148 2 18150
71 18148 $method = $invocant->can($callback);
72 }
73 else {
74 2 $method = $self->can($callback);
75 }
76  
77 18150 ▶ if (!$method) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($method) 1 18149 18150
78 1 $self->error_on_callback({invocant => $invocant, callback => $callback})
79 ->throw;
80 }
81  
82 18149 ▶ $callback = sub {goto $method};
Subroutine __ANON__: called CC=1 SCAR=0.0
83 }
84  
85 18315 return $callback;
86 }
87  
88 111 ▶sub catch {
Subroutine catch: called CC=2 SCAR=7.0
Pod documented
89 111 my ($self, $package, $callback) = @_;
90  
91 111 ▶ $callback ||= sub{(@_)};
Subroutine __ANON__: called CC=1 SCAR=0.1
Condition66%2/3
$callback ||= sub { @_; }
l!l&&r!l&&!r
41070
MC/DC0%0/2
$callback ||= sub { @_; }
$callback sub { @_; }
Truth table66%2/3
A ||= B
A= $callbackB= sub { @_; }
ABresult
1X1
011
000
92  
93 111 push @{$self->on_catch}, [$package, $self->callback($callback)];
94  
95 111 return $self;
96 }
97  
98 5 ▶sub default {
Subroutine default: called CC=1 SCAR=0.0
Pod documented
99 5 my ($self, $callback) = @_;
100  
101 5 $self->on_default($self->callback($callback));
102  
103 5 return $self;
104 }
105  
106 13079 ▶sub error {
Subroutine error: called CC=2 SCAR=6.9
Pod documented
107 13079 my ($self, $variable) = @_;
108  
109 13079 ▶ if ($variable) {
Branch: 1 branch
BranchTrueFalseTotal
if ($variable) { } 12992 87 13079
110 447 ▶ $self->on_default(sub{($$variable) = @_})
Subroutine __ANON__: called CC=1 SCAR=0.0
111 12992 }
112 else {
113 87 $self->catch('Venus::Error');
114 }
115  
116 13079 return $self;
117 }
118  
119 18344 ▶sub execute {
Subroutine execute: called CC=5 SCAR=16.1
Pod documented
120 18344 my ($self, $callback, @args) = @_;
121  
122 17851 unshift @args, @{$self->arguments}
123 18344 ▶ if $self->arguments && @{$self->arguments};
Branch: 1 branch
BranchTrueFalseTotal
if $self->arguments and @{$self->arguments;} 17851 492 18343
Condition100%3/3
$self->arguments and @{$self->arguments;}
!ll&&!rl&&r
21627617851
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
124  
125 18343 ▶ unshift @args, $self->invocant
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->invocant 18127 216 18343
126 if defined($self->invocant);
127  
128 18343 ▶ return wantarray ? ($callback->(@args)) : $callback->(@args);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 18340 3 18343
129 }
130  
131 9 ▶sub fault {
Subroutine fault: called CC=2 SCAR=7.0
Pod documented
132 9 my ($self, $variable) = @_;
133  
134 9 ▶ if ($variable) {
Branch: 1 branch
BranchTrueFalseTotal
if ($variable) { } 1 8 9
135 0 ▶ $self->on_default(sub{($$variable) = @_})
Subroutine __ANON__: not called CC=1 SCAR=6.9
136 1 }
137 else {
138 8 $self->catch('Venus::Fault');
139 }
140  
141 9 return $self;
142 }
143  
144 3 ▶sub finally {
Subroutine finally: called CC=1 SCAR=0.0
Pod documented
145 3 my ($self, $callback) = @_;
146  
147 3 $self->on_finally($self->callback($callback));
148  
149 3 return $self;
150 }
151  
152 6 ▶sub maybe {
Subroutine maybe: called CC=1 SCAR=0.0
Pod documented
153 6 my ($self) = @_;
154  
155 6 ▶ $self->on_default(sub{(undef)});
Subroutine __ANON__: called CC=1 SCAR=0.0
156  
157 6 return $self;
158 }
159  
160 1 ▶sub no_catch {
Subroutine no_catch: called CC=1 SCAR=0.0
Pod documented
161 1 my ($self) = @_;
162  
163 1 $self->on_catch([]);
164  
165 1 return $self;
166 }
167  
168 2 ▶sub no_default {
Subroutine no_default: called CC=1 SCAR=0.0
Pod documented
169 2 my ($self) = @_;
170  
171 2 $self->on_default(undef);
172  
173 2 return $self;
174 }
175  
176 1 ▶sub no_finally {
Subroutine no_finally: called CC=1 SCAR=0.0
Pod documented
177 1 my ($self) = @_;
178  
179 1 $self->on_finally(undef);
180  
181 1 return $self;
182 }
183  
184 1 ▶sub no_try {
Subroutine no_try: called CC=1 SCAR=0.0
Pod documented
185 1 my ($self) = @_;
186  
187 1 $self->on_try(undef);
188  
189 1 return $self;
190 }
191  
192 18340 ▶sub result {
Subroutine result: called CC=11 SCAR=24.0
Pod documented
193 18340 my ($self, @args) = @_;
194  
195 18340 my $dollarat = $@;
196 18340 my @returned;
197  
198 # try
199 18340 my $error = do {
200 18340 local $@;
201 18340 eval {
202 18340 my $tryer = $self->on_try;
203 18340 @returned = ($self->execute($tryer, @args));
204 };
205 18340 $@;
206 };
207  
208 # catch
209 18340 ▶ if ($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 545 17795 18340
210 545 my $caught = $error;
211 545 my $catchers = $self->on_catch;
212 545 my $default = $self->on_default;
213  
214 545 for my $catcher (@$catchers) {
215 88 ▶ if (UNIVERSAL::isa($caught, $catcher->[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($caught, $catcher->[0])) 88 0 88
216 88 local $_ = $caught;
217 88 @returned = ($catcher->[1]->($caught));
218 88 last;
219 }
220 }
221  
222 # catchall
223 545 ▶ if(!@returned) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@returned) 457 88 545
224 457 ▶ if ($default) {
Branch: 1 branch
BranchTrueFalseTotal
if ($default) 453 4 457
225 453 local $_ = $caught;
226 453 @returned = ($default->($caught))
227 }
228 }
229  
230 # uncaught
231 545 ▶ if(!@returned) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@returned) 4 541 545
232 4 ▶ if (Scalar::Util::blessed($caught)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($caught)) { } 2 2 4
233 2 die $caught;
234 }
235 else {
236 2 ▶ if (UNIVERSAL::isa($caught, 'Venus::Error')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($caught, 'Venus::Error')) { } 0 2 2
237 0 $caught->throw;
238 }
239 else {
240 2 require Venus::Error;
241 2 Venus::Error->throw($caught);
242 }
243 }
244 }
245 }
246  
247 # finally
248 18336 ▶ if (my $finally = $self->on_finally) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $finally = $self->on_finally) 2 18334 18336
249 2 $self->execute($finally, @args);
250 }
251  
252 18336 $@ = $dollarat;
253  
254 18336 ▶ return wantarray ? (@returned) : $returned[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 13020 5316 18336
255 }
256  
257 # ERRORS
258  
259 1 ▶sub error_on_callback {
Subroutine error_on_callback: called CC=4 SCAR=15.0
Pod undocumented
260 1 my ($self, $data) = @_;
261  
262 1 my $error = $self->throw->error->sysinfo;
263  
264 1 my $callback = $data->{callback};
265 1 my $invocant = $data->{invocant};
266  
267 1 ▶ my $message = sprintf(
Branch: 1 branch
BranchTrueFalseTotal
$invocant ? : 0 1 1
Condition16%1/6
ref $invocant or $invocant
l!l&&r!l&&!r
000
ref $self or $self
l!l&&r!l&&!r
100
MC/DC0%0/2
ref $invocant or $invocant
ref $invocant $invocant
MC/DC0%0/2
ref $self or $self
ref $self $self
Truth table0%0/3
A or B
A= ref $invocantB= $invocant
ABresult
1X1
011
000
Truth table33%1/3
A or B
A= ref $selfB= $self
ABresult
1X1
011
000
268 qq(Can't locate object method "%s" on package "%s"), ($callback,
269 $invocant ? (ref($invocant) || $invocant) : (ref($self) || $self))
270 );
271  
272 1 $error->name('on.callback');
273 1 $error->message($message);
274 1 $error->offset(1);
275 1 $error->stash($data);
276 1 $error->reset;
277  
278 1 return $error;
279 }
280  
281 119 1;
282  
283  
284  
285 =head1 NAME
286  
287 Venus::Try - Try Class
288  
289 =cut
290  
291 =head1 ABSTRACT
292  
293 Try Class for Perl 5
294  
295 =cut
296  
297 =head1 SYNOPSIS
298  
299 package main;
300  
301 use Venus::Try;
302  
303 my $try = Venus::Try->new;
304  
305 $try->call(sub {
306 my (@args) = @_;
307  
308 # try something
309  
310 return time;
311 });
312  
313 $try->catch('Example::Error', sub {
314 my ($caught) = @_;
315  
316 # caught an error (exception)
317  
318 return;
319 });
320  
321 $try->default(sub {
322 my ($caught) = @_;
323  
324 # catch the uncaught
325  
326 return;
327 });
328  
329 $try->finally(sub {
330 my (@args) = @_;
331  
332 # always run after try/catch
333  
334 return;
335 });
336  
337 my @args;
338  
339 my $result = $try->result(@args);
340  
341 =cut
342  
343 =head1 DESCRIPTION
344  
345 This package provides an object-oriented interface for performing complex
346 try/catch operations.
347  
348 =cut
349  
350 =head1 ATTRIBUTES
351  
352 This package has the following attributes:
353  
354 =cut
355  
356 =head2 invocant
357  
358 invocant(Object)
359  
360 This attribute is read-only, accepts C<(Object)> values, and is optional.
361  
362 =cut
363  
364 =head2 arguments
365  
366 arguments(ArrayRef)
367  
368 This attribute is read-only, accepts C<(ArrayRef)> values, and is optional.
369  
370 =cut
371  
372 =head2 on_try
373  
374 on_try(CodeRef)
375  
376 This attribute is read-write, accepts C<(CodeRef)> values, and is optional.
377  
378 =cut
379  
380 =head2 on_catch
381  
382 on_catch(ArrayRef[CodeRef])
383  
384 This attribute is read-write, accepts C<(ArrayRef[CodeRef])> values, is optional, and defaults to C<[]>.
385  
386 =cut
387  
388 =head2 on_default
389  
390 on_default(CodeRef)
391  
392 This attribute is read-write, accepts C<(CodeRef)> values, and is optional.
393  
394 =cut
395  
396 =head2 on_finally
397  
398 on_finally(CodeRef)
399  
400 This attribute is read-write, accepts C<(CodeRef)> values, and is optional.
401  
402 =cut
403  
404 =head1 INHERITS
405  
406 This package inherits behaviors from:
407  
408 L<Venus::Kind::Utility>
409  
410 =cut
411  
412 =head1 METHODS
413  
414 This package provides the following methods:
415  
416 =cut
417  
418 =head2 any
419  
420 any() (Venus::Try)
421  
422 The any method registers a default C<catch> condition that returns whatever
423 value was encoutered on error and returns it as a result.
424  
425 I<Since C<2.32>>
426  
427 =over 4
428  
429 =item any example 1
430  
431 package main;
432  
433 use Venus::Try;
434  
435 my $try = Venus::Try->new;
436  
437 $try->call(sub {
438 die 'Oops!';
439 });
440  
441 my $any = $try->any;
442  
443 # bless({ on_catch => ... }, "Venus::Try")
444  
445 =back
446  
447 =over 4
448  
449 =item any example 2
450  
451 package main;
452  
453 use Venus::Try;
454  
455 my $try = Venus::Try->new;
456  
457 $try->call(sub {
458 die $try;
459 });
460  
461 my $any = $try->any;
462  
463 # bless({ on_catch => ... }, "Venus::Try")
464  
465 =back
466  
467 =cut
468  
469 =head2 call
470  
471 call(string | coderef $method) (Venus::Try)
472  
473 The call method takes a method name or coderef, registers it as the tryable
474 routine, and returns the object. When invoked, the callback will received an
475 C<invocant> if one was provided to the constructor, the default C<arguments> if
476 any were provided to the constructor, and whatever arguments were provided by
477 the invocant.
478  
479  
480  
481  
482 I<Since C<0.01>>
483  
484 =over 4
485  
486 =item call example 1
487  
488 package main;
489  
490 use Venus::Try;
491  
492 my $try = Venus::Try->new;
493  
494 my $call = $try->call(sub {
495 my (@args) = @_;
496  
497 return [@args];
498 });
499  
500 # bless({ on_catch => ... }, "Venus::Try")
501  
502 =back
503  
504 =cut
505  
506 =head2 callback
507  
508 callback(string | coderef $method) (coderef)
509  
510 The callback method takes a method name or coderef, and returns a coderef for
511 registration. If a coderef is provided this method is mostly a passthrough.
512  
513 I<Since C<0.01>>
514  
515 =over 4
516  
517 =item callback example 1
518  
519 package main;
520  
521 use Venus::Try;
522  
523 my $try = Venus::Try->new;
524  
525 my $callback = $try->callback(sub {
526 my (@args) = @_;
527  
528 return [@args];
529 });
530  
531 # sub { ... }
532  
533 =back
534  
535 =over 4
536  
537 =item callback example 2
538  
539 package Example1;
540  
541 sub new {
542 bless {};
543 }
544  
545 sub test {
546 my (@args) = @_;
547  
548 return [@args];
549 }
550  
551 package main;
552  
553 use Venus::Try;
554  
555 my $try = Venus::Try->new(
556 invocant => Example1->new,
557 );
558  
559 my $callback = $try->callback('test');
560  
561 # sub { ... }
562  
563 =back
564  
565 =over 4
566  
567 =item callback example 3
568  
569 package main;
570  
571 use Venus::Try;
572  
573 my $try = Venus::Try->new;
574  
575 my $callback = $try->callback('missing_method');
576  
577 # Exception! (isa Venus::Try::Error) (see error_on_callback)
578  
579 =back
580  
581 =over 4
582  
583 =item B<may raise> L<Venus::Try::Error> C<on.callback>
584  
585 package main;
586  
587 use Venus::Try;
588  
589 my $try = Venus::Try->new;
590  
591 $try->call('execute')->result;
592  
593 # Error! (on.callback)
594  
595 =back
596  
597 =cut
598  
599 =head2 catch
600  
601 catch(string $isa, string | coderef $method) (Venus::Try)
602  
603 The catch method takes a package or ref name, and when triggered checks whether
604 the captured exception is of the type specified and if so executes the given
605 callback. If no callback is provided the exception is captured in a L</default>
606 operation and returned as a result.
607  
608 I<Since C<0.01>>
609  
610 =over 4
611  
612 =item catch example 1
613  
614 package main;
615  
616 use Venus::Try;
617  
618 my $try = Venus::Try->new;
619  
620 $try->call(sub {
621 my (@args) = @_;
622  
623 die $try;
624 });
625  
626 my $catch = $try->catch('Venus::Try', sub {
627 my (@args) = @_;
628  
629 return [@args];
630 });
631  
632 # bless({ on_catch => ... }, "Venus::Try")
633  
634 =back
635  
636 =over 4
637  
638 =item catch example 2
639  
640 package main;
641  
642 use Venus::Try;
643  
644 my $try = Venus::Try->new;
645  
646 $try->call(sub {
647 my (@args) = @_;
648  
649 $try->throw->error;
650 });
651  
652 my $catch = $try->catch('Venus::Try::Error', sub {
653  
654 return (@_);
655 });
656  
657 # bless({ on_catch => ... }, "Venus::Try")
658  
659 =back
660  
661 =over 4
662  
663 =item catch example 3
664  
665 package main;
666  
667 use Venus::Try;
668  
669 my $try = Venus::Try->new;
670  
671 $try->call(sub {
672  
673 $try->throw->error;
674 });
675  
676 my $catch = $try->catch('Venus::Try::Error');
677  
678 # bless({ on_catch => ... }, "Venus::Try")
679  
680 =back
681  
682 =cut
683  
684 =head2 default
685  
686 default(string | coderef $method) (Venus::Try)
687  
688 The default method takes a method name or coderef and is triggered if no
689 C<catch> conditions match the exception thrown.
690  
691 I<Since C<0.01>>
692  
693 =over 4
694  
695 =item default example 1
696  
697 package main;
698  
699 use Venus::Try;
700  
701 my $try = Venus::Try->new;
702  
703 $try->call(sub {
704 my (@args) = @_;
705  
706 die $try;
707 });
708  
709 my $default = $try->default(sub {
710 my (@args) = @_;
711  
712 return [@args];
713 });
714  
715 # bless({ on_catch => ... }, "Venus::Try")
716  
717 =back
718  
719 =cut
720  
721 =head2 error
722  
723 error(Ref $variable) (Venus::Try)
724  
725 The error method takes a scalar reference and assigns any uncaught exceptions
726 to it during execution. If no variable is provided a L</catch> operation will
727 be registered to capture all L<Venus::Error> exceptions.
728  
729 I<Since C<0.01>>
730  
731 =over 4
732  
733 =item error example 1
734  
735 package main;
736  
737 use Venus::Try;
738  
739 my $try = Venus::Try->new;
740  
741 $try->call(sub {
742 my (@args) = @_;
743  
744 die $try;
745 });
746  
747 my $error = $try->error(\my $object);
748  
749 # bless({ on_catch => ... }, "Venus::Try")
750  
751 =back
752  
753 =over 4
754  
755 =item error example 2
756  
757 package main;
758  
759 use Venus::Try;
760  
761 my $try = Venus::Try->new;
762  
763 $try->call(sub {
764 my (@args) = @_;
765  
766 $try->throw->error;
767 });
768  
769 my $error = $try->error;
770  
771 # bless({ on_catch => ... }, "Venus::Try")
772  
773 =back
774  
775 =cut
776  
777 =head2 execute
778  
779 execute(coderef $code, any @args) (any)
780  
781 The execute method takes a coderef and executes it with any given arguments.
782 When invoked, the callback will received an C<invocant> if one was provided to
783 the constructor, the default C<arguments> if any were provided to the
784 constructor, and whatever arguments were passed directly to this method. This
785 method can return a list of values in list-context.
786  
787 I<Since C<0.01>>
788  
789 =over 4
790  
791 =item execute example 1
792  
793 package Example2;
794  
795 sub new {
796 bless {};
797 }
798  
799 package main;
800  
801 use Venus::Try;
802  
803 my $try = Venus::Try->new(
804 invocant => Example2->new,
805 arguments => [1,2,3],
806 );
807  
808 my $execute = $try->execute(sub {
809 my (@args) = @_;
810  
811 return [@args];
812 });
813  
814 # [bless({}, "Example2"), 1, 2, 3]
815  
816 =back
817  
818 =cut
819  
820 =head2 fault
821  
822 fault(Ref $variable) (Venus::Try)
823  
824 The fault method takes a scalar reference and assigns any uncaught exceptions
825 to it during execution. If no variable is provided a L</catch> operation will
826 be registered to capture all L<Venus::Fault> exceptions.
827  
828 I<Since C<4.15>>
829  
830 =over 4
831  
832 =item fault example 1
833  
834 package main;
835  
836 use Venus 'fault';
837  
838 use Venus::Try;
839  
840 my $try = Venus::Try->new;
841  
842 $try->call(sub {
843 my (@args) = @_;
844  
845 fault;
846 });
847  
848 my $fault = $try->fault(\my $object);
849  
850 # bless({ on_catch => ... }, "Venus::Try")
851  
852 =back
853  
854 =over 4
855  
856 =item fault example 2
857  
858 package main;
859  
860 use Venus 'fault';
861  
862 use Venus::Try;
863  
864 my $try = Venus::Try->new;
865  
866 $try->call(sub {
867 my (@args) = @_;
868  
869 fault;
870 });
871  
872 my $fault = $try->fault;
873  
874 # bless({ on_catch => ... }, "Venus::Try")
875  
876 =back
877  
878 =cut
879  
880 =head2 finally
881  
882 finally(string | coderef $method) (Venus::Try)
883  
884 The finally method takes a package or ref name and always executes the callback
885 after a try/catch operation. The return value is ignored. When invoked, the
886 callback will received an C<invocant> if one was provided to the constructor,
887 the default C<arguments> if any were provided to the constructor, and whatever
888 arguments were provided by the invocant.
889  
890 I<Since C<0.01>>
891  
892 =over 4
893  
894 =item finally example 1
895  
896 package Example3;
897  
898 sub new {
899 bless {};
900 }
901  
902 package main;
903  
904 use Venus::Try;
905  
906 my $try = Venus::Try->new(
907 invocant => Example3->new,
908 arguments => [1,2,3],
909 );
910  
911 $try->call(sub {
912 my (@args) = @_;
913  
914 return $try;
915 });
916  
917 my $finally = $try->finally(sub {
918 my (@args) = @_;
919  
920 $try->{args} = [@args];
921 });
922  
923 # bless({ on_catch => ... }, "Venus::Try")
924  
925 =back
926  
927 =cut
928  
929 =head2 maybe
930  
931 maybe() (Venus::Try)
932  
933 The maybe method registers a default C<catch> condition that returns falsy,
934 i.e. an undefined value, if an exception is encountered.
935  
936 I<Since C<0.01>>
937  
938 =over 4
939  
940 =item maybe example 1
941  
942 package main;
943  
944 use Venus::Try;
945  
946 my $try = Venus::Try->new;
947  
948 $try->call(sub {
949 my (@args) = @_;
950  
951 die $try;
952 });
953  
954 my $maybe = $try->maybe;
955  
956 # bless({ on_catch => ... }, "Venus::Try")
957  
958 =back
959  
960 =cut
961  
962 =head2 new
963  
964 new(any @args) (Venus::Try)
965  
966 The new method constructs an instance of the package.
967  
968 I<Since C<4.15>>
969  
970 =over 4
971  
972 =item new example 1
973  
974 package main;
975  
976 use Venus::Try;
977  
978 my $new = Venus::Try->new;
979  
980 # bless(..., "Venus::Try")
981  
982 =back
983  
984 =over 4
985  
986 =item new example 2
987  
988 package main;
989  
990 use Venus::Try;
991  
992 my $new = Venus::Try->new(invocant => (bless{}), arguments => [1..4]);
993  
994 # bless(..., "Venus::Try")
995  
996 =back
997  
998 =cut
999  
1000 =head2 no_catch
1001  
1002 no_catch() (Venus::Try)
1003  
1004 The no_catch method removes any configured catch conditions and returns the
1005 object.
1006  
1007 I<Since C<0.01>>
1008  
1009 =over 4
1010  
1011 =item no_catch example 1
1012  
1013 package main;
1014  
1015 use Venus::Try;
1016  
1017 my $try = Venus::Try->new;
1018  
1019 $try->call(sub {
1020 my (@args) = @_;
1021  
1022 die $try;
1023 });
1024  
1025 $try->catch('Venus::Try', sub {
1026 my (@args) = @_;
1027  
1028 return [@args];
1029 });
1030  
1031  
1032 my $no_catch = $try->no_catch;
1033  
1034 # bless({ on_catch => ... }, "Venus::Try")
1035  
1036 =back
1037  
1038 =cut
1039  
1040 =head2 no_default
1041  
1042 no_default() (Venus::Try)
1043  
1044 The no_default method removes any configured default condition and returns the
1045 object.
1046  
1047 I<Since C<0.01>>
1048  
1049 =over 4
1050  
1051 =item no_default example 1
1052  
1053 package main;
1054  
1055 use Venus::Try;
1056  
1057 my $try = Venus::Try->new;
1058  
1059 $try->call(sub {
1060 my (@args) = @_;
1061  
1062 die $try;
1063 });
1064  
1065 my $default = $try->default(sub {
1066 my (@args) = @_;
1067  
1068 return [@args];
1069 });
1070  
1071 my $no_default = $try->no_default;
1072  
1073 # bless({ on_catch => ... }, "Venus::Try")
1074  
1075 =back
1076  
1077 =cut
1078  
1079 =head2 no_finally
1080  
1081 no_finally() (Venus::Try)
1082  
1083 The no_finally method removes any configured finally condition and returns the
1084 object.
1085  
1086 I<Since C<0.01>>
1087  
1088 =over 4
1089  
1090 =item no_finally example 1
1091  
1092 package Example4;
1093  
1094 sub new {
1095 bless {};
1096 }
1097  
1098 package main;
1099  
1100 use Venus::Try;
1101  
1102 my $try = Venus::Try->new(
1103 invocant => Example4->new,
1104 arguments => [1,2,3],
1105 );
1106  
1107 $try->call(sub {
1108 my (@args) = @_;
1109  
1110 return $try;
1111 });
1112  
1113 $try->finally(sub {
1114 my (@args) = @_;
1115  
1116 $try->{args} = [@args];
1117 });
1118  
1119 my $no_finally = $try->no_finally;
1120  
1121 # bless({ on_catch => ... }, "Venus::Try")
1122  
1123 =back
1124  
1125 =cut
1126  
1127 =head2 no_try
1128  
1129 no_try() (Venus::Try)
1130  
1131 The no_try method removes any configured C<try> operation and returns the
1132 object.
1133  
1134 I<Since C<0.01>>
1135  
1136 =over 4
1137  
1138 =item no_try example 1
1139  
1140 package main;
1141  
1142 use Venus::Try;
1143  
1144 my $try = Venus::Try->new;
1145  
1146 $try->call(sub {
1147 my (@args) = @_;
1148  
1149 return [@args];
1150 });
1151  
1152 my $no_try = $try->no_try;
1153  
1154 # bless({ on_catch => ... }, "Venus::Try")
1155  
1156 =back
1157  
1158 =cut
1159  
1160 =head2 result
1161  
1162 result(any @args) (any)
1163  
1164 The result method executes the try/catch/default/finally logic and returns
1165 either 1) the return value from the successfully tried operation 2) the return
1166 value from the successfully matched catch condition if an exception was thrown
1167 3) the return value from the default catch condition if an exception was thrown
1168 and no catch condition matched. When invoked, the C<try> and C<finally>
1169 callbacks will received an C<invocant> if one was provided to the constructor,
1170 the default C<arguments> if any were provided to the constructor, and whatever
1171 arguments were passed directly to this method. This method can return a list of
1172 values in list-context.
1173  
1174 I<Since C<0.01>>
1175  
1176 =over 4
1177  
1178 =item result example 1
1179  
1180 package main;
1181  
1182 use Venus::Try;
1183  
1184 my $try = Venus::Try->new;
1185  
1186 $try->call(sub {
1187 my (@args) = @_;
1188  
1189 return [@args];
1190 });
1191  
1192 my $result = $try->result;
1193  
1194 # []
1195  
1196 =back
1197  
1198 =over 4
1199  
1200 =item result example 2
1201  
1202 package main;
1203  
1204 use Venus::Try;
1205  
1206 my $try = Venus::Try->new;
1207  
1208 $try->call(sub {
1209 my (@args) = @_;
1210  
1211 return [@args];
1212 });
1213  
1214 my $result = $try->result(1..5);
1215  
1216 # [1..5]
1217  
1218 =back
1219  
1220 =over 4
1221  
1222 =item result example 3
1223  
1224 package main;
1225  
1226 use Venus::Try;
1227  
1228 my $try = Venus::Try->new;
1229  
1230 $try->call(sub {die});
1231  
1232 my $result = $try->result;
1233  
1234 # Exception! Venus::Error
1235  
1236 =back
1237  
1238 =cut
1239  
1240 =head1 AUTHORS
1241  
1242 Awncorp, C<awncorp@cpan.org>
1243  
1244 =cut
1245  
1246 =head1 LICENSE
1247  
1248 Copyright (C) 2022, Awncorp, C<awncorp@cpan.org>.
1249  
1250 This program is free software, you can redistribute it and/or modify it under
1251 the terms of the Apache license version 2.0.
1252  
1253 =cut
« blib/lib/Venus/Yaml.pm ↑ Summary blib/lib/Venus/Role/Defaultable.pm »