Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: WWW::Mechanize and connection errors

by almut (Canon)
on Oct 12, 2009 at 19:33 UTC ( [id://800781]=note: print w/replies, xml ) Need Help??


in reply to WWW::Mechanize and connection errors

I'm sure i need to subclass WWW::Mechanize but don't know how.

Subclassing could look something like this:

package MyMech; use WWW::Mechanize; our @ISA = "WWW::Mechanize"; sub _wrapped { my $self = shift; my $method = "SUPER::".shift; my $result; RETRY: for (1..3) { eval { $result = $self->$method(@_); }; if ($@) { warn gmtime(time).": $@"; $result = undef; # just in case sleep 5; } else { last RETRY; } } return $result; } sub get { shift->_wrapped('get', @_) } sub put { shift->_wrapped('put', @_) } # ...

and then in the main script:

#!/usr/bin/perl use MyMech; my $mech = MyMech->new(autocheck => 1); my $resp = $mech->get("http://localhost:9999/foo"); # ... __END__ Mon Oct 12 19:32:48 2009: Error GETing http://localhost:9999/foo: Can' +t connect to localhost:9999 (connect: Connection refused) at ./800725 +.pl line 7 Mon Oct 12 19:32:53 2009: Error GETing http://localhost:9999/foo: Can' +t connect to localhost:9999 (connect: Connection refused) at ./800725 +.pl line 7 Mon Oct 12 19:32:58 2009: Error GETing http://localhost:9999/foo: Can' +t connect to localhost:9999 (connect: Connection refused) at ./800725 +.pl line 7

(note that in the example I use a generic wrapper doing the error checking and retries, so you don't have to write the same boilerplate code for every subclassed method that's meant to be handled similarly.  You of course don't have to do it that way...  in which case the call to the respective superclass method would simply be $self->SUPER::get(@_), etc. instead)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://800781]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2024-04-16 07:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found