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)
|