line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Test::Auto::Try; |
2
|
|
|
|
|
|
|
|
3
|
6
|
|
|
6
|
|
42
|
use strict; |
|
6
|
|
|
|
|
14
|
|
|
6
|
|
|
|
|
182
|
|
4
|
6
|
|
|
6
|
|
30
|
use warnings; |
|
6
|
|
|
|
|
11
|
|
|
6
|
|
|
|
|
147
|
|
5
|
|
|
|
|
|
|
|
6
|
6
|
|
|
6
|
|
29
|
use Moo; |
|
6
|
|
|
|
|
9
|
|
|
6
|
|
|
|
|
30
|
|
7
|
6
|
|
|
6
|
|
5262
|
use Try::Tiny (); |
|
6
|
|
|
|
|
10168
|
|
|
6
|
|
|
|
|
5965
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
our $VERSION = '0.12'; # VERSION |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
# ATTRIBUTES |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
has 'invocant' => ( |
14
|
|
|
|
|
|
|
is => 'ro' |
15
|
|
|
|
|
|
|
); |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
has 'arguments' => ( |
18
|
|
|
|
|
|
|
is => 'ro' |
19
|
|
|
|
|
|
|
); |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
has 'on_try' => ( |
22
|
|
|
|
|
|
|
is => 'rw' |
23
|
|
|
|
|
|
|
); |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
has 'on_catch' => ( |
26
|
|
|
|
|
|
|
is => 'rw' |
27
|
|
|
|
|
|
|
); |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
has 'on_default' => ( |
30
|
|
|
|
|
|
|
is => 'rw' |
31
|
|
|
|
|
|
|
); |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
has 'on_finally' => ( |
34
|
|
|
|
|
|
|
is => 'rw' |
35
|
|
|
|
|
|
|
); |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
# BUILD |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub BUILD { |
40
|
40
|
|
|
40
|
0
|
8939
|
my ($self, $args) = @_; |
41
|
|
|
|
|
|
|
|
42
|
40
|
50
|
|
|
|
159
|
$self->{'on_catch'} = [] if !$args->{'on_catch'}; |
43
|
|
|
|
|
|
|
|
44
|
40
|
|
|
|
|
276
|
return $args; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
# METHODS |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub call { |
50
|
40
|
|
|
40
|
0
|
109
|
my ($self, $callback) = @_; |
51
|
|
|
|
|
|
|
|
52
|
40
|
|
|
|
|
128
|
$self->on_try($self->callback($callback)); |
53
|
|
|
|
|
|
|
|
54
|
40
|
|
|
|
|
236
|
return $self; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub callback { |
58
|
40
|
|
|
40
|
0
|
86
|
my ($self, $callback) = @_; |
59
|
|
|
|
|
|
|
|
60
|
40
|
|
|
|
|
256
|
require Carp; |
61
|
|
|
|
|
|
|
|
62
|
40
|
100
|
|
|
|
226
|
unless (UNIVERSAL::isa($callback, 'CODE')) { |
63
|
38
|
50
|
|
|
|
320
|
my $method = $self->invocant |
64
|
|
|
|
|
|
|
? $self->invocant->can($callback) : $self->can($callback); |
65
|
38
|
50
|
|
|
|
113
|
Carp::confess(sprintf( |
66
|
|
|
|
|
|
|
qq(Can't locate object method "%s" on package "%s"), |
67
|
|
|
|
|
|
|
($callback, ref $self) |
68
|
|
|
|
|
|
|
)) if !$method; |
69
|
38
|
|
|
38
|
|
157
|
$callback = sub { goto $method }; |
|
38
|
|
|
|
|
174
|
|
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
40
|
|
|
|
|
155
|
return $callback; |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
sub catch { |
76
|
0
|
|
|
0
|
0
|
0
|
my ($self, $class, $callback) = @_; |
77
|
|
|
|
|
|
|
|
78
|
0
|
|
|
|
|
0
|
push @{$self->on_catch}, [$class, $self->callback($callback)]; |
|
0
|
|
|
|
|
0
|
|
79
|
|
|
|
|
|
|
|
80
|
0
|
|
|
|
|
0
|
return $self; |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub default { |
84
|
0
|
|
|
0
|
0
|
0
|
my ($self, $callback) = @_; |
85
|
|
|
|
|
|
|
|
86
|
0
|
|
|
|
|
0
|
$self->on_default($self->callback($callback)); |
87
|
|
|
|
|
|
|
|
88
|
0
|
|
|
|
|
0
|
return $self; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
sub execute { |
92
|
38
|
|
|
38
|
0
|
112
|
my ($self, $callback, @args) = @_; |
93
|
|
|
|
|
|
|
|
94
|
38
|
|
|
|
|
136
|
unshift @args, @{$self->arguments} |
95
|
38
|
50
|
33
|
|
|
170
|
if $self->arguments && @{$self->arguments}; |
|
38
|
|
|
|
|
149
|
|
96
|
38
|
50
|
|
|
|
158
|
unshift @args, $self->invocant |
97
|
|
|
|
|
|
|
if $self->invocant; |
98
|
|
|
|
|
|
|
|
99
|
38
|
|
|
|
|
94
|
return $callback->(@args); |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
sub finally { |
103
|
0
|
|
|
0
|
0
|
0
|
my ($self, $callback) = @_; |
104
|
|
|
|
|
|
|
|
105
|
0
|
|
|
|
|
0
|
$self->on_finally($self->callback($callback)); |
106
|
|
|
|
|
|
|
|
107
|
0
|
|
|
|
|
0
|
return $self; |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
sub maybe { |
111
|
0
|
|
|
0
|
0
|
0
|
my ($self) = @_; |
112
|
|
|
|
|
|
|
|
113
|
0
|
|
|
0
|
|
0
|
$self->on_default(sub{''}); |
|
0
|
|
|
|
|
0
|
|
114
|
|
|
|
|
|
|
|
115
|
0
|
|
|
|
|
0
|
return $self; |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
sub no_catch { |
119
|
0
|
|
|
0
|
0
|
0
|
my ($self) = @_; |
120
|
|
|
|
|
|
|
|
121
|
0
|
|
|
|
|
0
|
$self->on_catch([]); |
122
|
|
|
|
|
|
|
|
123
|
0
|
|
|
|
|
0
|
return $self; |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
sub no_default { |
127
|
0
|
|
|
0
|
0
|
0
|
my ($self) = @_; |
128
|
|
|
|
|
|
|
|
129
|
0
|
|
|
|
|
0
|
$self->on_default(undef); |
130
|
|
|
|
|
|
|
|
131
|
0
|
|
|
|
|
0
|
return $self; |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
sub no_finally { |
135
|
0
|
|
|
0
|
0
|
0
|
my ($self) = @_; |
136
|
|
|
|
|
|
|
|
137
|
0
|
|
|
|
|
0
|
$self->on_finally(undef); |
138
|
|
|
|
|
|
|
|
139
|
0
|
|
|
|
|
0
|
return $self; |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
sub no_try { |
143
|
0
|
|
|
0
|
0
|
0
|
my ($self) = @_; |
144
|
|
|
|
|
|
|
|
145
|
0
|
|
|
|
|
0
|
$self->on_try(undef); |
146
|
|
|
|
|
|
|
|
147
|
0
|
|
|
|
|
0
|
return $self; |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
sub result { |
151
|
38
|
|
|
38
|
0
|
278
|
my ($self, @args) = @_; |
152
|
|
|
|
|
|
|
|
153
|
38
|
|
|
|
|
143
|
require Carp; |
154
|
|
|
|
|
|
|
|
155
|
38
|
|
|
|
|
61
|
my $returned; |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
Try::Tiny::try(sub { |
158
|
38
|
|
|
38
|
|
2893
|
my $tryer = $self->on_try; |
159
|
|
|
|
|
|
|
|
160
|
38
|
|
|
|
|
128
|
$returned = $self->execute($tryer, @args); |
161
|
|
|
|
|
|
|
}, Try::Tiny::catch(sub { |
162
|
0
|
|
|
0
|
|
0
|
my $caught = $_; |
163
|
0
|
|
|
|
|
0
|
my $catchers = $self->on_catch; |
164
|
0
|
|
|
|
|
0
|
my $default = $self->on_default; |
165
|
|
|
|
|
|
|
|
166
|
0
|
|
|
|
|
0
|
for my $catcher (@$catchers) { |
167
|
0
|
0
|
|
|
|
0
|
if (UNIVERSAL::isa($caught, $catcher->[0])) { |
168
|
0
|
|
|
|
|
0
|
$returned = $catcher->[1]->($caught); |
169
|
0
|
|
|
|
|
0
|
last; |
170
|
|
|
|
|
|
|
} |
171
|
|
|
|
|
|
|
} |
172
|
|
|
|
|
|
|
|
173
|
0
|
0
|
|
|
|
0
|
if(!$returned) { |
174
|
0
|
0
|
|
|
|
0
|
$returned = $default->($caught) if $default; |
175
|
0
|
0
|
|
|
|
0
|
Carp::confess($caught) if not defined $returned; |
176
|
|
|
|
|
|
|
} |
177
|
|
|
|
|
|
|
}, Try::Tiny::finally(sub { |
178
|
38
|
|
|
38
|
|
900
|
my $finally = $self->on_finally; |
179
|
|
|
|
|
|
|
|
180
|
38
|
50
|
|
|
|
118
|
$self->execute($finally, @args) if $finally; |
181
|
38
|
|
|
|
|
360
|
}))); |
182
|
|
|
|
|
|
|
|
183
|
38
|
|
|
|
|
820
|
return $returned; |
184
|
|
|
|
|
|
|
} |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
1; |