File Coverage

blib/lib/WWW/GetPageTitle.pm
Criterion Covered Total %
statement 15 33 45.4
branch 0 10 0.0
condition 0 3 0.0
subroutine 5 7 71.4
pod 2 2 100.0
total 22 55 40.0


line stmt bran cond sub pod time code
1             package WWW::GetPageTitle;
2              
3 1     1   235602 use warnings;
  1         4  
  1         71  
4 1     1   11 use strict;
  1         4  
  1         120  
5              
6             our $VERSION = '0.0104';
7              
8 1     1   9 use LWP::UserAgent;
  1         11  
  1         42  
9 1     1   8 use HTML::Entities;
  1         3  
  1         137  
10 1     1   8 use base 'Class::Data::Accessor';
  1         3  
  1         869  
11             __PACKAGE__->mk_classaccessors( qw/
12             error
13             title
14             ua
15             uri
16             /);
17              
18             sub new {
19 0     0 1   my ( $class, %args ) = @_;
20 0           my $self = bless {}, $class;
21              
22 0   0       $self->ua(
23             $args{ua}
24             ||
25             LWP::UserAgent->new(
26             agent => "Mozilla",
27             timeout => 30,
28             max_size => 2000,
29             )
30             );
31              
32 0           return $self;
33             }
34              
35             sub get_title {
36 0     0 1   my ( $self, $uri ) = @_;
37              
38 0 0         $uri = "http://$uri"
39             unless $uri =~ m{^(?:https?|ftps?)://}i;
40              
41 0           $self->uri( $uri );
42              
43             $self->$_(undef)
44 0           for qw/title error/;
45              
46 0           my $response = $self->ua->get($uri);
47              
48 0 0         unless ( $response->is_success ) {
49 0           $self->error("Network error: " . $response->status_line );
50 0           return;
51             }
52              
53 0           my $title = $response->title;
54              
55 0 0         unless ( defined $title ) {
56 0           ( $title ) = $response->decoded_content =~ m|]*>(.+?)|si;
57 0 0         decode_entities( $title )
58             if defined $title;
59             }
60              
61 0 0         $title = 'N/A'
62             unless defined $title;
63              
64 0           return $self->title( $title );
65             }
66              
67             1;
68             __END__