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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.