For a project using
WWW::Mechanize for some site-scraping, I ended up needing to wrap most of the calls that do something requiring a network to allow them to be retried. The combination of networks and servers is just a little too flaky to make thousands of HTTP requests without any of them failing, but they usually go through if you retry them a couple of times.
I wanted a succint way to add this to any action, so I wrote it to take sub refs. I can call it like this:
retry(
sub {
$browser->get( $link->url() );
}
);
And this is the definition of the retry() sub:
sub retry {
my $sub_ref = shift;
for ( 1 .. $conf->max_tries() ) {
eval { $sub_ref->(); };
last unless $@;
warn "Failed try $_, retrying. Error: $@\n"
if $conf->debug();
}
if ($@) { die "failed after " . $conf->max_tries() . " tries: $@\n
+" }
}
This assumes that a failed action will throw an exception, which is what happens with the settings I'm using with Mechanize.
Now this works great, but I felt like it was sort of a naive approach, and I just wondered if anyone had something clever for this, maybe using some of those less commonly applied loop control constructs that Perl allows. I'm not looking for golf here (although feel free to amuse yourself if you think it sounds fun), but really just wondering if there's a more elegant solution.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.