0

I have the following very simple piece of code:

#!/usr/bin/perl

use strict;
use warnings;

use WWW::Mechanize::Timed;
my $ua = WWW::Mechanize::Timed->new();
my $url = 'www.stackoverflow.com';

$ua->get($url);    
print "Total time: " . $ua->client_total_time . "\n";
print "Elapsed time: " . $ua->client_elapsed_time . "\n";

The error I am getting:

When running the program I sometimes get the following error, it does not happen all the time, its seems to appear intermittently.

Use of uninitialized value in addition (+) at /usr/local/share/perl/5.10.1/WWW/Mechanize/Timed.pm line 52.

Is there something obvious I am overlooking, as I cant understand why this error is occurring, let alone why it only happens sometimes?

Your help is much appreciated, many thanks

Andy Lester
  • 91,102
  • 13
  • 100
  • 152
yonetpkbji
  • 1,019
  • 2
  • 21
  • 35
  • 2
    You should finish the message: `at ...` what line and what file? Also consider using `Carp::Always` to get a more helpful message. – mob Mar 06 '13 at 22:38
  • @mob - added the rest of the error to my question, thanks – yonetpkbji Mar 06 '13 at 22:43

1 Answers1

2

I believe the problem is that

my $url = 'www.stackoverflow.com';

is not an actual URL. That's just a host name. Are you saying that your program sometimes works without specifying the http:// scheme? You need

my $url = 'http://www.stackoverflow.com';

Also, you want to check the return code from ->get(), or add autocheck => 1 to the constructor. At some point in ::Mechanize's history that became the default, but I don't know what version you're on.

Andy Lester
  • 91,102
  • 13
  • 100
  • 152
  • 1
    Yes, you are correct, thank you very much. In my program each of the urls were being parsed in from a large text file, it just so happened that a few of the urls were being put into the text file without the `http://` prefix, which is why the problem only seemed to be occurring occasionally when it came across these urls without the `http://`, thanks for your quick answer. – yonetpkbji Mar 06 '13 at 23:25