blib/lib/WWW/Mechanize/Plugin/Sleepy.pm

filter: Statementstmt 97.7% 43 / 44 Branchbran 78.5% 11 / 14 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 92.3% 12 / 13 Podpod 50.0% 1 / 2 totaltotal 89.4% 68 / 76 CC 10 SCAR 10.0
CC
10
Cov
92%
_set_sleep
5.1
init
4.0
import
2.1
CRAP 2.7
↑ Summary
1 package WWW::Mechanize::Plugin::Sleepy;
2  
3 2 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 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Carp qw/ croak /;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11  
12 1 ▶sub import {
Subroutine import: called CC=2 SCAR=7.2
13 1 my ( $class, %args ) = @_;
14 $WWW::Mechanize::Pluggable::Sleepy = $args{sleep}
15 1 ▶ if defined $args{sleep};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"sleep"} 0 1 1
16 }
17  
18 6 ▶sub init {
Subroutine init: called CC=4 SCAR=13.9
Pod undocumented
19 6 my ( $class, $pluggable, %args ) = @_;
20  
21 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
22 6 *{ caller() . '::sleep' } = \&sleep;
23 6 *{ caller() . '::_sleep' } = \&_sleep;
24  
25 6 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 ▶ $pluggable->pre_hook( $method, sub { $_[0]->_sleep(); 0; } );
Subroutine __ANON__: called CC=1 SCAR=0.0
31 }
32  
33 my $sleep
34 = defined $args{sleep}
35 ? $args{sleep}
36 6 ▶ : $WWW::Mechanize::Pluggable::Sleepy || 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'sleep'} ? : 5 1 6
Condition50%1/2
$WWW::Mechanize::Pluggable::Sleepy or 0
l!l
01
MC/DC0%0/1
$WWW::Mechanize::Pluggable::Sleepy or 0
$WWW::Mechanize::Pluggable::Sleepy
Truth table50%1/2
A
A= $WWW::Mechanize::Pluggable::Sleepy
Aresult
11
00
37  
38 6 _set_sleep( $pluggable, $sleep );
39 }
40  
41  
42 6 ▶sub sleep {
Subroutine sleep: called CC=2 SCAR=6.9
Pod documented
43 6 my ( $self, $arg ) = @_;
44 6 ▶ _set_sleep( $self, $arg ) if defined $arg;
Branch: 1 branch
BranchTrueFalseTotal
if defined $arg 2 4 6
45 6 return $self->{Sleepy_Time};
46 }
47  
48 # sets sleep time and method
49 8 ▶sub _set_sleep {
Subroutine _set_sleep: called CC=5 SCAR=16.3
50 8 my ( $self, $arg ) = @_;
51  
52 8 my $method;
53 8 ▶ if ( !defined $arg ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $arg) { } 0 8 8
elsif (() = $arg =~ /^(\d+)\.\.(\d+)$/) { } 1 7 8
elsif (not $arg =~ /\D/) { } 3 4 7
54 0 ▶ $method = sub { };
Subroutine __ANON__: not called CC=1 SCAR=6.9
55
56 } elsif ( my ( $from, $to ) = $arg =~ m/^(\d+)\.\.(\d+)$/ ) {
57 1 ▶ croak "sleep range (i1..i2) must have i1 < i2"
Branch: 1 branch
BranchTrueFalseTotal
if $1 >= $2 0 1 1
58 if $1 >= $2;
59 $method
60 1 ▶ = sub { CORE::sleep( int( rand( ( $to + 1 ) - $from ) ) + $from ) };
Subroutine __ANON__: called CC=1 SCAR=0.0
61
62 } elsif ( $arg !~ m/\D/ ) {
63 3 ▶ $method = sub { CORE::sleep($arg); };
Subroutine __ANON__: called CC=1 SCAR=0.0
64
65 } else {
66 4 croak "sleep parameter must be an integer or a range i1..i2";
67 }
68  
69 4 $self->{Sleepy_Time} = $arg;
70 4 $self->{Sleepy_Method} = $method;
71 }
72  
73 # performs sleep
74 10 ▶sub _sleep {
Subroutine _sleep: called CC=1 SCAR=0.0
75 10 my ($self) = @_;
76 10 $self->{Sleepy_Method}->();
77 }
78  
79  
80 2 1;
81  
82 __END__
↑ Summary