Examples in Perl

Retrieve webpages

You can use the LWP::UserAgent module. The following is an example taken almost as it is from the module documentation :


use strict;
use LWP::UserAgent;
my $content;

my $ua = new LWP::UserAgent;

# Various enhancement possibilities:
# $ua->max_size(100000);  # 100k byte limit
# $ua->timeout(3);        # 3 sec timeout is default
# $ua->proxy(['http'], 'http://myproxy.mycorp.com/');  # set proxy
# $ua->env_proxy()        # load proxy info from environment variables
# $ua->no_proxy('localhost', 'mycorp.com');   # No proxy for local machines

$ua->agent("Mozilla/6.0");  # Or something equally mysterious

my $req = HTTP::Request->new('GET','http://www.yahoo.com/');
my $res = $ua->request($req);
if ($res->is_success) 
  $content= $res->content;

  print "Content-type: text/html", "\n\n";
  print "$content";

  print "Content-type: text/html", "\n\n";
  print "not successful
res->status_line :

" . $res->status_line . "\n"; die "Could not get content"; }

This displays the webpage retrieved or displays an error message if unsuccessful.

Back to Table of Contents