File Coverage

blib/lib/Data/Object/Role/Tryable.pm
Criterion Covered Total %
statement 23 23 100.0
branch n/a
condition n/a
subroutine 7 7 100.0
pod 1 1 100.0
total 31 31 100.0


line stmt bran cond sub pod time code
1             package Data::Object::Role::Tryable;
2              
3 1     1   31889 use 5.014;
  1         4  
4              
5 1     1   6 use strict;
  1         2  
  1         21  
6 1     1   5 use warnings;
  1         2  
  1         23  
7 1     1   5 use routines;
  1         2  
  1         6  
8              
9 1     1   2244 use Moo::Role;
  1         9519  
  1         7  
10 1     1   978 use Data::Object::Try;
  1         5734  
  1         105  
11              
12             our $VERSION = '2.00'; # VERSION
13              
14             # METHODS
15              
16 2     2 1 73541 method try($callback, @args) {
  2         5  
  2         4  
17 2         25 my $try = Data::Object::Try->new(invocant => $self, arguments => [@args]);
18              
19 2         1510 $callback = $try->callback($callback); # build callback
20              
21 2         27 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