blib/lib/WWW/Find.pm

filter: Statementstmt 34.2% 25 / 73 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 50.0% 7 / 14 Podpod 0.0% 0 / 6 totaltotal 26.0% 32 / 123 CC 15 SCAR 35.3
CC
15
Cov
25%
__ANON__
90.0
follow_sub
20.0
new
12.0
CRAP 34.0
↑ Summary
1 package WWW::Find;
2  
3 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use URI::Heuristic;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use HTML::LinkExtor;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 our $VERSION = '0.07';
12 1 my $depth = 0;
13 1 my %seen;
14  
15 # Default URL matching subroutine
16 0 ▶sub match_sub {
Subroutine match_sub: not called CC=2 SCAR=17.9
Pod undocumented
17 0 my($self) = shift;
18  
19 ## tests for URL's matching this REGEX
20 0 ▶ if($self->{REQUEST}->uri =~ /html?$/io) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"REQUEST"}->uri =~ /html?$/io) 0 0 0
21  
22 ## do something with matching URL's
23 ## print to STDOUT is the default action
24 0 print $self->{REQUEST}->uri . "\n";
25 }
26 return
27 0 }
28  
29 ## Default URL follow subtroutine
30 ## Should return true or false
31 0 ▶sub follow_sub {
Subroutine follow_sub: not called CC=4 SCAR=30.0
Pod undocumented
32 0 my $self = shift;
33 0 my $header = HTTP::Request->new(HEAD => $self->{REQUEST}->uri);
34 0 ▶ my $response = $self->{AGENT}->request($header) || next;
Condition0%0/2
$self->{'AGENT'}->request($header) || (next)
l!l
00
MC/DC0%0/1
$self->{'AGENT'}->request($header) || (next)
$self->{'AGENT'}->request($header)
Truth table0%0/2
A
A= $self->{'AGENT'}->request($header)
Aresult
11
00
35 0 ▶ $response->content_type eq 'text/html' && ref($self->{REQUEST}->uri) eq 'URI::http'
Branch: 1 branch
BranchTrueFalseTotal
$response->content_type eq 'text/html' && ref $self->{'REQUEST'}->uri eq 'URI::http' ? : 0 0 0
Condition0%0/3
$response->content_type eq "text/html" and ref $self->{"REQUEST"}->uri eq "URI::http"
!ll&&!rl&&r
000
MC/DC0%0/2
$response->content_type eq "text/html" and ref $self->{"REQUEST"}->uri eq "URI::http"
$response->content_type eq "text/html" ref $self->{"REQUEST"}->uri eq "URI::http"
Truth table0%0/3
A and B
A= $response->content_type eq "text/html"B= ref $self->{"REQUEST"}->uri eq "URI::http"
ABresult
0X0
100
111
36 ? return 1
37 : return 0
38 }
39  
40 ## Private methods
41  
42 my $_rec;
43 0 ▶$_rec = sub {
Subroutine __ANON__: not called CC=9 SCAR=45.0
44 0 my $find_obj = shift;
45 0 my $uri = URI->new($find_obj->{REQUEST}->uri);
46 # $seen{$uri}++;
47 # return if($seen{$uri} > 1);
48 0 ▶ return if($depth > $find_obj->{MAX_DEPTH});
Branch: 1 branch
BranchTrueFalseTotal
if $depth > $find_obj->{"MAX_DEPTH"} 0 0 0
49 0 $depth++;
50  
51 ## Request HTML Document
52 0 my $html = $find_obj->{AGENT}->request($find_obj->{REQUEST});
53  
54 ## Parse out HREF links
55 0 my $parser = HTML::LinkExtor->new(undef);
56 0 $parser->parse($html->content);
57 0 my @links = $parser->links;
58 0 foreach my $ln (@links)
59 {
60 0 my @element = @$ln;
61 0 my $type = shift @element;
62 0 ▶ next unless($type =~ /^a/io);
Branch: 1 branch
BranchTrueFalseTotal
unless $type =~ /^a/io 0 0 0
63 0 ▶ while(@element)
Branch: 1 branch
BranchTrueFalseTotal
if (@element) 0 0 0
64 {
65 0 my ($name, $value) = splice(@element, 0, 2);
66  
67 ## Make URL absolute
68 0 $find_obj->{REQUEST}->uri(URI->new_abs($value, $uri));
69 0 my $url = $find_obj->{REQUEST}->uri;
70  
71 ## Check recursion depth
72 0 ▶ next if($depth > $find_obj->{MAX_DEPTH});
Branch: 1 branch
BranchTrueFalseTotal
if $depth > $find_obj->{"MAX_DEPTH"} 0 0 0
73  
74 ## Skip if duplicate
75 0 $seen{$url}++;
76 0 ▶ next if($seen{$url} > 1);
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$url} > 1 0 0 0
77 ## User defined matching subroutine
78 0 $find_obj->{MATCH_SUB}($find_obj);
79  
80 ## Check recursion depth
81 # next if($depth > $find_obj->{MAX_DEPTH});
82  
83 ## Modify request object for next request
84 0 ▶ if(ref($find_obj->{REQUEST}->uri))
Branch: 1 branch
BranchTrueFalseTotal
if (ref $find_obj->{"REQUEST"}->uri) 0 0 0
85 {
86 0 $find_obj->{REQUEST}->uri(URI::Heuristic::uf_urlstr($find_obj->{REQUEST}->uri));
87  
88 ## User defined follow subroutine
89 0 ▶ &$_rec($find_obj) if ($find_obj->{FOLLOW_SUB}($find_obj));
Branch: 1 branch
BranchTrueFalseTotal
if $find_obj->{"FOLLOW_SUB"}->($find_obj) 0 0 0
90 }
91 }
92 }
93 0 $depth--;
94  
95 1 };
96  
97 # constructor
98 sub new
99 0 ▶{
Subroutine new: not called CC=3 SCAR=24.8
Pod undocumented
100 0 my($class, %parm) = @_;
101 0 ▶ croak 'Expecting a class' if ref $class;
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 0 0 0
102 0 my $self = { MAX_DEPTH => 2,
103 DIRECTORY => '.',
104 MATCH_SUB => \&match_sub,
105 FOLLOW_SUB => \&follow_sub
106 };
107 ## Parms should be validated, but I'm feeling lazy
108 0 ▶ while(my($k, $v) = each(%parm)) { $self->{$k} = $v};
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %parm) 0 0 0
109 0 bless $self, $class;
110 0 return $self;
111 }
112  
113 ## Public methods
114 0 ▶sub go {
Subroutine go: not called CC=1 SCAR=6.9
Pod undocumented
115 0 my($self, %parm) = @_;
116 0 $self->{REQUEST}->uri(URI::Heuristic::uf_urlstr($self->{REQUEST}->uri));
117 0 &$_rec($self);
118 }
119  
120 0 ▶sub set_match {
Subroutine set_match: not called CC=1 SCAR=6.9
Pod undocumented
121 0 my($self, $sub_ref) = @_;
122 0 $self->{MATCH_SUB} = $sub_ref;
123 0 return $self->{MATCH_SUB};
124 }
125  
126 0 ▶sub set_follow {
Subroutine set_follow: not called CC=1 SCAR=6.9
Pod undocumented
127 0 my($self, $sub_ref) = @_;
128 0 $self->{FOLLOW_SUB} = $sub_ref;
129 0 return $self->{FOLLOW_SUB};
130 }
131  
132 1 1;
133  
134 __END__
↑ Summary