blib/lib/Venus/Try.pm

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