#! /usr/bin/perl -w

use strict;

#Input: the absolute url (http://...) of a page
#Output: the content of the page in a single string
sub fetch_page{
  my $url = shift(@_);
  
  my $ua;
  my $req;
  my $res;
  my $title;
  my $toping;

  # Check to see if the host is alive
  use Net::Ping;
  
  # Create a user agent object
  
  use LWP::UserAgent;
  $ua = new LWP::UserAgent;
  $ua->agent("AgentName/0.1 " . $ua->agent);
  #$ua->timeout(300);
 

  # Create a request
  
  $req = new HTTP::Request GET => $url;

  # set signal handler to ignore die signals 
  local $SIG{PIPE} = 'IGNORE';
  local $SIG{__DIE__} = 'IGNORE';

  # Pass request to the user agent and get a response back
  $res = $ua->request($req);

  if (!defined($res)){
    print "Page not accessible!\n";
    return undef;
  }

  # Check the outcome of the response
  if ($res->is_success) {
    return $res->content;
  } else {
    print "Error: ", $res->status_line, "\n";
    return undef;
  }
}
