| 1 | package Data::Object::Role::Tryable; | ||
| 2 | |||
| 3 | use 5.014; | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | use routines; | ||
| 8 | |||
| 9 | use Moo::Role; | ||
| 10 | use Data::Object::Try; | ||
| 11 | |||
| 12 | our $VERSION = '2.00'; # VERSION | ||
| 13 | |||
| 14 | # METHODS | ||
| 15 | |||
| 16 | method try($callback, @args) { | ||
| 17 | my $try = Data::Object::Try->new(invocant => $self, arguments => [@args]); | ||
| 18 | |||
| 19 | $callback = $try->callback($callback); # build callback | ||
| 20 | |||
| 21 | return $try->call($callback); | ||
| 22 | } | ||
| 23 | |||
| 24 | 1; | ||
| 25 | |||
| 26 | =encoding utf8 | ||
| 27 | |||
| 28 | =head1 NAME | ||
| 29 | |||
| 30 | Data::Object::Role::Tryable | ||
| 31 | |||
| 32 | =cut | ||
| 33 | |||
| 34 | =head1 ABSTRACT | ||
| 35 | |||
| 36 | Tryable Role for Perl 5 | ||
| 37 | |||
| 38 | =cut | ||
| 39 | |||
| 40 | =head1 SYNOPSIS | ||
| 41 | |||
| 42 | package Example; | ||
| 43 | |||
| 44 | use Moo; | ||
| 45 | |||
| 46 | with 'Data::Object::Role::Tryable'; | ||
| 47 | |||
| 48 | package main; | ||
| 49 | |||
| 50 | use routines; | ||
| 51 | |||
| 52 | my $example = Example->new; | ||
| 53 | |||
| 54 | =cut | ||
| 55 | |||
| 56 | =head1 DESCRIPTION | ||
| 57 | |||
| 58 | This package provides a wrapper around the L<Data::Object::Try> class which | ||
| 59 | provides an object-oriented interface for performing complex try/catch | ||
| 60 | operations. | ||
| 61 | |||
| 62 | =cut | ||
| 63 | |||
| 64 | =head1 METHODS | ||
| 65 | |||
| 66 | This package implements the following methods: | ||
| 67 | |||
| 68 | =cut | ||
| 69 | |||
| 70 | =head2 try | ||
| 71 | |||
| 72 | try(CodeRef | Str $method) : InstanceOf['Data::Object::Try'] | ||
| 73 | |||
| 74 | The try method takes a method name or coderef and returns a | ||
| 75 | L<Data::Object::Try> object with the current object passed as the invocant | ||
| 76 | which means that C<try> and C<finally> callbacks will receive that as the first | ||
| 77 | argument. | ||
| 78 | |||
| 79 | =over 4 | ||
| 80 | |||
| 81 | =item try example #1 | ||
| 82 | |||
| 83 | # given: synopsis | ||
| 84 | |||
| 85 | my $tryer = $example->try(fun(@args) { | ||
| 86 | [@args] | ||
| 87 | }); | ||
| 88 | |||
| 89 | # $tryer->result(...) | ||
| 90 | |||
| 91 | =back | ||
| 92 | |||
| 93 | =over 4 | ||
| 94 | |||
| 95 | =item try example #2 | ||
| 96 | |||
| 97 | # given: synopsis | ||
| 98 | |||
| 99 | my $tryer = $example->try(fun(@args) { | ||
| 100 | die 'tried'; | ||
| 101 | }); | ||
| 102 | |||
| 103 | $tryer->default(fun($error) { | ||
| 104 | return ['tried'] if $error =~ 'tried'; | ||
| 105 | return [$error]; | ||
| 106 | }); | ||
| 107 | |||
| 108 | # $tryer->result(...) | ||
| 109 | |||
| 110 | =back | ||
| 111 | |||
| 112 | =cut | ||
| 113 | |||
| 114 | =head1 AUTHOR | ||
| 115 | |||
| 116 | Al Newkirk, C<awncorp@cpan.org> | ||
| 117 | |||
| 118 | =head1 LICENSE | ||
| 119 | |||
| 120 | Copyright (C) 2011-2019, Al Newkirk, et al. | ||
| 121 | |||
| 122 | This is free software; you can redistribute it and/or modify it under the terms | ||
| 123 | of the The Apache License, Version 2.0, as elucidated in the L<"license | ||
| 124 | file"|https://github.com/iamalnewkirk/data-object-role-tryable/blob/master/LICENSE>. | ||
| 125 | |||
| 126 | =head1 PROJECT | ||
| 127 | |||
| 128 | L<Wiki|https://github.com/iamalnewkirk/data-object-role-tryable/wiki> | ||
| 129 | |||
| 130 | L<Project|https://github.com/iamalnewkirk/data-object-role-tryable> | ||
| 131 | |||
| 132 | L<Initiatives|https://github.com/iamalnewkirk/data-object-role-tryable/projects> | ||
| 133 | |||
| 134 | L<Milestones|https://github.com/iamalnewkirk/data-object-role-tryable/milestones> | ||
| 135 | |||
| 136 | L<Contributing|https://github.com/iamalnewkirk/data-object-role-tryable/blob/master/CONTRIBUTE.md> | ||
| 137 | |||
| 138 | L<Issues|https://github.com/iamalnewkirk/data-object-role-tryable/issues> | ||
| 139 | |||
| 140 | =cut |