line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Sub::Throttler::algo; |
2
|
8
|
|
|
8
|
|
2697
|
use 5.010001; |
|
8
|
|
|
|
|
17
|
|
|
8
|
|
|
|
|
228
|
|
3
|
8
|
|
|
8
|
|
27
|
use warnings; |
|
8
|
|
|
|
|
14
|
|
|
8
|
|
|
|
|
142
|
|
4
|
8
|
|
|
8
|
|
24
|
use strict; |
|
8
|
|
|
|
|
8
|
|
|
8
|
|
|
|
|
182
|
|
5
|
8
|
|
|
8
|
|
92
|
use utf8; |
|
8
|
|
|
|
|
8
|
|
|
8
|
|
|
|
|
31
|
|
6
|
8
|
|
|
8
|
|
122
|
use Carp; |
|
8
|
|
|
|
|
10
|
|
|
8
|
|
|
|
|
561
|
|
7
|
|
|
|
|
|
|
our @CARP_NOT = qw( Sub::Throttler ); |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
our $VERSION = 'v0.2.2'; |
10
|
|
|
|
|
|
|
|
11
|
8
|
|
|
8
|
|
33
|
use Scalar::Util qw( blessed ); |
|
8
|
|
|
|
|
10
|
|
|
8
|
|
|
|
|
469
|
|
12
|
8
|
|
|
8
|
|
31
|
use List::Util qw( any ); |
|
8
|
|
|
|
|
11
|
|
|
8
|
|
|
|
|
589
|
|
13
|
8
|
|
|
8
|
|
1745
|
use Sub::Throttler qw( throttle_add ); |
|
8
|
|
|
|
|
16
|
|
|
8
|
|
|
|
|
36
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
|
16
|
8
|
|
|
8
|
|
6011
|
use constant DEFAULT_KEY => 'default'; |
|
8
|
|
|
|
|
9
|
|
|
8
|
|
|
|
|
3675
|
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
sub apply_to { |
20
|
38
|
|
|
38
|
1
|
6684
|
goto &throttle_add; |
21
|
|
|
|
|
|
|
} |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
sub apply_to_functions { |
24
|
3
|
|
|
3
|
1
|
22
|
my ($self, @func) = @_; |
25
|
2
|
50
|
|
|
|
42
|
my %func = map { $_ => DEFAULT_KEY } |
|
2
|
|
|
|
|
16
|
|
26
|
3
|
|
|
|
|
283
|
map {/::/ms ? $_ : caller().q{::}.$_} @func; |
27
|
|
|
|
|
|
|
$self->apply_to(sub { |
28
|
11
|
|
|
11
|
|
20
|
my ($this, $name) = @_; |
29
|
11
|
100
|
|
|
|
44
|
my $key |
|
|
100
|
|
|
|
|
|
30
|
|
|
|
|
|
|
= $this ? undef |
31
|
|
|
|
|
|
|
: @func ? $func{$name} |
32
|
|
|
|
|
|
|
: DEFAULT_KEY |
33
|
|
|
|
|
|
|
; |
34
|
11
|
100
|
|
|
|
39
|
return $key ? {$key=>1} : undef; |
35
|
3
|
|
|
|
|
32
|
}); |
36
|
3
|
|
|
|
|
20
|
return $self; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub apply_to_methods { |
40
|
7
|
|
|
7
|
1
|
13
|
my ($self, $class_or_obj, @func) = @_; |
41
|
7
|
50
|
66
|
|
|
42
|
croak 'require class or object' |
42
|
|
|
|
|
|
|
if ref $class_or_obj && !blessed($class_or_obj); |
43
|
7
|
50
|
|
6
|
|
78
|
croak 'method must not contain ::' if any {/::/ms} @func; |
|
6
|
|
|
|
|
18
|
|
44
|
7
|
|
|
|
|
17
|
my %func = map { $_ => DEFAULT_KEY } @func; |
|
6
|
|
|
|
|
20
|
|
45
|
7
|
50
|
|
|
|
42
|
if (1 == @_) { |
|
|
100
|
|
|
|
|
|
46
|
|
|
|
|
|
|
$self->apply_to(sub { |
47
|
0
|
|
|
0
|
|
0
|
my ($this) = @_; |
48
|
0
|
0
|
|
|
|
0
|
my $key = $this ? DEFAULT_KEY : undef; |
49
|
0
|
0
|
|
|
|
0
|
return $key ? {$key=>1} : undef; |
50
|
0
|
|
|
|
|
0
|
}); |
51
|
|
|
|
|
|
|
} elsif (ref $class_or_obj) { |
52
|
4
|
|
|
|
|
5
|
my $obj = $class_or_obj; |
53
|
|
|
|
|
|
|
$self->apply_to(sub { |
54
|
4
|
|
|
4
|
|
5
|
my ($this, $name) = @_; |
55
|
4
|
50
|
33
|
|
|
40
|
my $key |
|
|
50
|
|
|
|
|
|
56
|
|
|
|
|
|
|
= !$this || !ref $this || $this != $obj ? undef |
57
|
|
|
|
|
|
|
: @func ? $func{$name} |
58
|
|
|
|
|
|
|
: DEFAULT_KEY |
59
|
|
|
|
|
|
|
; |
60
|
4
|
50
|
|
|
|
14
|
return $key ? {$key=>1} : undef; |
61
|
4
|
|
|
|
|
16
|
}); |
62
|
|
|
|
|
|
|
} else { |
63
|
3
|
|
|
|
|
5
|
my $class = $class_or_obj; |
64
|
|
|
|
|
|
|
$self->apply_to(sub { |
65
|
23
|
|
|
23
|
|
23
|
my ($this, $name) = @_; |
66
|
23
|
|
|
|
|
63
|
my $key |
67
|
23
|
100
|
|
|
|
21
|
= !eval {local $SIG{__DIE__}; $this->isa($class)} ? undef |
|
23
|
100
|
|
|
|
169
|
|
68
|
|
|
|
|
|
|
: @func ? $func{$name} |
69
|
|
|
|
|
|
|
: DEFAULT_KEY |
70
|
|
|
|
|
|
|
; |
71
|
23
|
100
|
|
|
|
83
|
return $key ? {$key=>1} : undef; |
72
|
3
|
|
|
|
|
26
|
}); |
73
|
|
|
|
|
|
|
} |
74
|
7
|
|
|
|
|
17
|
return $self; |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
1; # Magic true value required at end of module |
79
|
|
|
|
|
|
|
__END__ |