Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I recover from a timeout with Perl's WWW::Mechanize?

I'm using WWW::Mechanize to read a particular webpage in a loop that runs every few seconds. Occasionally, the 'GET' times out and the script stops running. How can I recover from one such timeout such that it continues the loop and tries the 'GET' the next time around?

like image 578
aks Avatar asked Sep 22 '10 05:09

aks


1 Answers

Use eval :

eval {
    my $resp = $mech->get($url);
    $resp->is_success or die $resp->status_line;
    # your code
};

if ($@) {
    print "Recovered from a GET error\n";    
}

The eval block will catch any error while GETing the page.

like image 89
Eugene Yarmash Avatar answered Oct 07 '22 20:10

Eugene Yarmash