line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package WWW::Mechanize::Plugin::Sleepy; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
our $VERSION = '0.003'; # VERSION |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
# ABSTRACT: A WWW::Mechanize plugin to provide the behaviour of WWW::Mechanize::Sleepy while using WWW::Mechanize::Pluggable |
6
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
494942
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
40
|
|
8
|
1
|
|
|
1
|
|
7
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
29
|
|
9
|
1
|
|
|
1
|
|
5
|
use Carp qw/ croak /; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
127
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub import { |
13
|
1
|
|
|
1
|
|
6109
|
my ( $class, %args ) = @_; |
14
|
1
|
50
|
|
|
|
9
|
$WWW::Mechanize::Pluggable::Sleepy = $args{sleep} |
15
|
|
|
|
|
|
|
if defined $args{sleep}; |
16
|
|
|
|
|
|
|
} |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
sub init { |
19
|
6
|
|
|
6
|
0
|
53066
|
my ( $class, $pluggable, %args ) = @_; |
20
|
|
|
|
|
|
|
|
21
|
1
|
|
|
1
|
|
6
|
no strict 'refs'; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
669
|
|
22
|
6
|
|
|
|
|
21
|
*{ caller() . '::sleep' } = \&sleep; |
|
6
|
|
|
|
|
20
|
|
23
|
6
|
|
|
|
|
182
|
*{ caller() . '::_sleep' } = \&_sleep; |
|
6
|
|
|
|
|
242
|
|
24
|
|
|
|
|
|
|
|
25
|
6
|
|
|
|
|
180
|
foreach my $method ( |
26
|
|
|
|
|
|
|
qw/ get put reload back request follow_link submit submit_form/) |
27
|
|
|
|
|
|
|
{ |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
# return 0; - ensures carries on to rest of parent method |
30
|
48
|
|
|
10
|
|
575
|
$pluggable->pre_hook( $method, sub { $_[0]->_sleep(); 0; } ); |
|
10
|
|
|
|
|
2109
|
|
|
10
|
|
|
|
|
117
|
|
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
6
|
100
|
50
|
|
|
87
|
my $sleep |
34
|
|
|
|
|
|
|
= defined $args{sleep} |
35
|
|
|
|
|
|
|
? $args{sleep} |
36
|
|
|
|
|
|
|
: $WWW::Mechanize::Pluggable::Sleepy || 0; |
37
|
|
|
|
|
|
|
|
38
|
6
|
|
|
|
|
22
|
_set_sleep( $pluggable, $sleep ); |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
sub sleep { |
43
|
6
|
|
|
6
|
1
|
290901
|
my ( $self, $arg ) = @_; |
44
|
6
|
100
|
|
|
|
32
|
_set_sleep( $self, $arg ) if defined $arg; |
45
|
6
|
|
|
|
|
43
|
return $self->{Sleepy_Time}; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
# sets sleep time and method |
49
|
|
|
|
|
|
|
sub _set_sleep { |
50
|
8
|
|
|
8
|
|
16
|
my ( $self, $arg ) = @_; |
51
|
|
|
|
|
|
|
|
52
|
8
|
|
|
|
|
12
|
my $method; |
53
|
8
|
50
|
|
|
|
72
|
if ( !defined $arg ) { |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
54
|
0
|
|
|
0
|
|
0
|
$method = sub { }; |
|
0
|
|
|
|
|
0
|
|
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
} elsif ( my ( $from, $to ) = $arg =~ m/^(\d+)\.\.(\d+)$/ ) { |
57
|
1
|
50
|
|
|
|
8
|
croak "sleep range (i1..i2) must have i1 < i2" |
58
|
|
|
|
|
|
|
if $1 >= $2; |
59
|
|
|
|
|
|
|
$method |
60
|
1
|
|
|
5
|
|
9
|
= sub { CORE::sleep( int( rand( ( $to + 1 ) - $from ) ) + $from ) }; |
|
5
|
|
|
|
|
12000797
|
|
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
} elsif ( $arg !~ m/\D/ ) { |
63
|
3
|
|
|
5
|
|
13
|
$method = sub { CORE::sleep($arg); }; |
|
5
|
|
|
|
|
10000651
|
|
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
} else { |
66
|
4
|
|
|
|
|
104
|
croak "sleep parameter must be an integer or a range i1..i2"; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
4
|
|
|
|
|
11
|
$self->{Sleepy_Time} = $arg; |
70
|
4
|
|
|
|
|
17
|
$self->{Sleepy_Method} = $method; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
# performs sleep |
74
|
|
|
|
|
|
|
sub _sleep { |
75
|
10
|
|
|
10
|
|
22
|
my ($self) = @_; |
76
|
10
|
|
|
|
|
39
|
$self->{Sleepy_Method}->(); |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
1; |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
__END__ |