Tweet

How can I use Perl to test whether a WWW document is accessible?

Solution 1: LWP::Simple

The LWP::Simple module provides a simple interface to the LWP (lib-www-perl) modules. This sample script uses the head() subroutine to determine whether or not we can access the URL:

    #!/usr/bin/perl
    use strict;
    use warnings;
    use LWP::Simple;

    if (head("http://www.perlmeme.org/faqs")) {
        print "Yes\n";
    } else {
        print "No\n";
    }
    exit 0;

The head() subroutine can also be used to retrieve some document headers for a web page:

    #!/usr/bin/perl
    use strict;
    use warnings;
    use LWP::Simple;

    my ($content_type, $doc_length, $mod_time, $expires, $server);

    if (($content_type, $doc_length, $mod_time, $expires, $server) =
        head("http://www.perlmeme.org/faqs")) {
        print "Yes, document length is: $doc_length\n";
    } else {
        print "No\n";
    }
    exit 0;

Solution 2: LWP

The LWP object oriented style allows for more flexible behaviour:

    #!/usr/bin/perl 
    use strict;
    use warnings;
    use LWP::UserAgent;

    my $ua = LWP::UserAgent->new;
    $ua->agent("Mozilla/8.0");	# Pretend to be Mozilla

    my $req = HTTP::Request->new(GET => 'http://www.perlmeme.org');
    my $res = $ua->request($req);

    if ($res->is_success) {
        print "Success!\n";
    } else {
        print "Error: " . $res->status_line . "\n";
    }

    exit 0;

this sample will return:

    Success!

if the URL can be found, and (depending on the error), something like the following if not:

    Error: 404 Not Found

See Also

    perldoc LWP::Simple
    perldoc lwpcook
    perldoc LWP
[Top]