| 1 | package WWW::HTMLTagAttributeCounter; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 2 | our $VERSION = '1.001001'; # VERSION | |||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 2 | ▶ | use LWP::UserAgent; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 2 | ▶ | use HTML::TokeParser::Simple; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 2 | ▶ | use overload q|""| => sub { shift->result_readable }; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | 2 | ▶ | use base 'Class::Accessor::Grouped'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | 2 | __PACKAGE__->mk_group_accessors( simple => qw/ | |||||||||||||||||||
| 14 | ua | ||||||||||||||||||||
| 15 | result | ||||||||||||||||||||
| 16 | error | ||||||||||||||||||||
| 17 | /); | ||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | 1 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||
| 20 | 1 | my ( $class, %args ) = @_; | |||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | 1 | ▶ | $args{ua} ||= LWP::UserAgent->new( | ||||||||||||||||||
|
Condition33%1/3
$args{"ua"} ||= "LWP::UserAgent"->new("timeout", 30, "agent", "Opera 9.5")
MC/DC0%0/2
$args{"ua"} ||= "LWP::UserAgent"->new("timeout", 30, "agent", "Opera 9.5")
$args{"ua"}
"LWP::UserAgent"->new("timeout", 30, "agent", "Opera 9.5")
Truth table33%1/3
A ||= B
A= $args{"ua"}B= "LWP::UserAgent"->new("timeout", 30, "agent", "Opera 9.5")
| |||||||||||||||||||||
| 23 | timeout => 30, | ||||||||||||||||||||
| 24 | agent => 'Opera 9.5', | ||||||||||||||||||||
| 25 | ); | ||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | 1 | my $self = bless {}, $class; | |||||||||||||||||||
| 28 | 1 | $self->ua( $args{ua} ); | |||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | 1 | return $self; | |||||||||||||||||||
| 31 | } | ||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | 4 | ▶ | sub count { | ||||||||||||||||||
|
Subroutine
count: called CC=7 SCAR=23.4
Pod
documented
| |||||||||||||||||||||
| 34 | 4 | my ( $self, $where, $what, $type, ) = @_; | |||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | $self->$_(undef) | ||||||||||||||||||||
| 37 | 4 | for qw/error result/; | |||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | 4 | ▶ | $what = [ $what ] | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 40 | unless ref $what eq 'ARRAY'; | ||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | 4 | ▶ | defined $type | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 43 | or $type = 'tag'; | ||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | 4 | my $content; | |||||||||||||||||||
| 46 | 4 | ▶ | if ( ref $where eq 'SCALAR' ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 47 | 4 | $content = $$where; | |||||||||||||||||||
| 48 | } | ||||||||||||||||||||
| 49 | else { | ||||||||||||||||||||
| 50 | 0 | ▶ | $where =~ m{^https?://} | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 51 | or $where = "http://$where"; | ||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | 0 | my $response = $self->ua->get( $where ); | |||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | 0 | ▶ | $response->is_success | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 56 | or return $self->_set_error( $response ); | ||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | 0 | $content = $response->decoded_content; | |||||||||||||||||||
| 59 | } | ||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | 4 | return $self->result( $self->_count( $what, $type, $content ) ); | |||||||||||||||||||
| 62 | } | ||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | 3 | ▶ | sub result_readable { | ||||||||||||||||||
|
Subroutine
result_readable: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||
| 65 | 3 | my $result = shift->result; | |||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | 3 | my @out; | |||||||||||||||||||
| 68 | 3 | for ( sort keys %$result ) { | |||||||||||||||||||
| 69 | 9 | push @out, "$result->{$_} $_"; | |||||||||||||||||||
| 70 | } | ||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | 3 | ▶ | return $out[0] | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 73 | if @out == 1; | ||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | 3 | return (join q|, |, @out[0..$#out-1]) . ' and ' . $out[-1]; | |||||||||||||||||||
| 76 | } | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | 4 | ▶ | sub _count { | ||||||||||||||||||
|
Subroutine
_count: called CC=11 SCAR=24.0
| |||||||||||||||||||||
| 79 | 4 | my ( $self, $what, $type, $content ) = @_; | |||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | 4 | my $p = HTML::TokeParser::Simple->new( \$content ); | |||||||||||||||||||
| 82 | 4 | my $count = {}; | |||||||||||||||||||
| 83 | 4 | ▶ | while ( my $t = $p->get_token ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 84 | next | ||||||||||||||||||||
| 85 | 52 | ▶ | unless $t->is_start_tag; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | 16 | ▶ | if ( $type eq 'tag' ) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 88 | 12 | for ( @$what ) { | |||||||||||||||||||
| 89 | $t->is_start_tag($_) | ||||||||||||||||||||
| 90 | 28 | ▶ | and $count->{$_}++; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 91 | } | ||||||||||||||||||||
| 92 | } | ||||||||||||||||||||
| 93 | elsif ( $type eq 'attr') { | ||||||||||||||||||||
| 94 | 4 | for ( @$what ) { | |||||||||||||||||||
| 95 | defined $t->get_attr($_) | ||||||||||||||||||||
| 96 | 4 | ▶ | and $count->{$_}++; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 97 | } | ||||||||||||||||||||
| 98 | } | ||||||||||||||||||||
| 99 | } | ||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | defined $count->{$_} | ||||||||||||||||||||
| 102 | or $count->{$_} = 0 | ||||||||||||||||||||
| 103 | 4 | ▶ | for @$what; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | 4 | return $count; | |||||||||||||||||||
| 106 | } | ||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | 0 | ▶ | sub _set_error { | ||||||||||||||||||
|
Subroutine
_set_error: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 109 | 0 | my ( $self, $response ) = @_; | |||||||||||||||||||
| 110 | 0 | $self->error('Network error: ' . $response->status_line ); | |||||||||||||||||||
| 111 | 0 | return; | |||||||||||||||||||
| 112 | } | ||||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | 2 | 1; | |||||||||||||||||||
| 115 | __END__ | ||||||||||||||||||||