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

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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)


In reply to Re: WWW::Mechanize and connection errors by almut
in thread WWW::Mechanize and connection errors by Gangabass

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-03-28 21:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found