in reply to Fetching titles and error handling

http://search.cpan.org/grep?cpanid=GAAS&release=libwww-perl-5.828&string=title&i=1&n=1&C=0
#!/usr/bin/perl -- use strict; use warnings; #use open ':locale'; use open ':encoding(UTF-8)'; die "\nUsage: $0 url\n" unless @ARGV; main(@ARGV); sub main { my ($url) = @_; use LWP::Simple 5.827 '$ua'; $ua->agent("Mozilla/5.0"); $ua->timeout(15); my $resp = $ua->get($url); die "Error getting $url: ", $resp->status_line, "\n" unless $resp->is_success; die "Not HTML, it's ", $resp->content_type, "\n" unless $resp->content_type eq 'text/html'; if ( my $title = $resp->title ) { print "Title: '$title'\n"; } }
WWW::Mechanize
#!/usr/bin/perl -- use strict; use warnings; #use open ':locale'; use open ':encoding(UTF-8)'; die "\nUsage: $0 url\n" unless @ARGV; main(@ARGV); sub main { my ($url) = @_; use WWW::Mechanize 1.54; my $ua = WWW::Mechanize->new( agent => "Mozilla/5.0", timeout => 15, ); $ua->get($url); die "Not HTML, it's ", $ua->ct, "\n" unless $ua->is_html(); if ( my $title = $ua->title ) { print "Title: '$title'\n"; } }
$ perl test.pl Usage: test.pl url $ perl test.pl / Error GETing /:(400): URL must be absolute at test.pl line 22 $ perl test.pl http://cpan.org Title: 'CPAN' $ perl test.pl http://perlmonks.org Title: 'PerlMonks - The Monastery Gates'