line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Sub::Retry; |
2
|
2
|
|
|
2
|
|
45906
|
use strict; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
73
|
|
3
|
2
|
|
|
2
|
|
10
|
use warnings; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
53
|
|
4
|
2
|
|
|
2
|
|
46
|
use 5.008001; |
|
2
|
|
|
|
|
10
|
|
|
2
|
|
|
|
|
122
|
|
5
|
|
|
|
|
|
|
our $VERSION = '0.06'; |
6
|
2
|
|
|
2
|
|
1750
|
use parent qw/Exporter/; |
|
2
|
|
|
|
|
636
|
|
|
2
|
|
|
|
|
10
|
|
7
|
2
|
|
|
2
|
|
3143
|
use Time::HiRes qw/sleep/; |
|
2
|
|
|
|
|
4144
|
|
|
2
|
|
|
|
|
10
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
our @EXPORT = qw/retry/; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
sub retry { |
12
|
12
|
|
|
12
|
1
|
25035
|
my ( $times, $delay, $code, $retry_if ) = @_; |
13
|
|
|
|
|
|
|
|
14
|
12
|
|
|
|
|
15
|
my $err; |
15
|
12
|
|
100
|
49
|
|
67
|
$retry_if ||= sub { $err = $@ }; |
|
49
|
|
|
|
|
135
|
|
16
|
12
|
|
|
|
|
19
|
my $n = 0; |
17
|
12
|
|
|
|
|
29
|
while ( $times-- > 0 ) { |
18
|
71
|
|
|
|
|
94
|
$n++; |
19
|
71
|
100
|
|
|
|
154
|
if (wantarray) { |
|
|
100
|
|
|
|
|
|
20
|
12
|
|
|
|
|
17
|
my @ret = eval { $code->($n) }; |
|
12
|
|
|
|
|
30
|
|
21
|
12
|
100
|
|
|
|
104
|
unless ($retry_if->(@ret)) { |
22
|
3
|
|
|
|
|
23
|
return @ret; |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
elsif (not defined wantarray) { |
26
|
21
|
|
|
|
|
27
|
eval { $code->($n) }; |
|
21
|
|
|
|
|
47
|
|
27
|
21
|
100
|
|
|
|
153
|
unless ($retry_if->()) { |
28
|
2
|
|
|
|
|
10
|
return; |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
else { |
32
|
38
|
|
|
|
|
45
|
my $ret = eval { $code->($n) }; |
|
38
|
|
|
|
|
75
|
|
33
|
38
|
100
|
|
|
|
234
|
unless ($retry_if->($ret)) { |
34
|
4
|
|
|
|
|
23
|
return $ret; |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
} |
37
|
62
|
100
|
|
|
|
4814
|
sleep $delay if $times; # Do not sleep in last time |
38
|
|
|
|
|
|
|
} |
39
|
3
|
100
|
|
|
|
19
|
die $err if $err; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
1; |
43
|
|
|
|
|
|
|
__END__ |