Create a wrapper function (we'll call it wrapper) that returns true on success and false on failure. It might look like this, then:

sub wrapper { eval { ## do some stuff that might die }; if ($@) { ## handle some error things, die if unrecoverable return 0; # returns false if recoverable } return 1; #returns true if we should retry }

To retry a few times before giving up, then, you may write:

use constant RETRIES => 3; for (1..RETRIES) { last if wrapper() }

In this way, you will try up to three times before the loop will exit. If you need more complexity, you might expand to something like:

use constant RETRIES => 3; our $give_up = 0; for (1..RETRIES) { $give_up = 0; last if wrapper(); $give_up = 1; } die ('Tried '.RETRIES.' times without success. Giving up.') if $give_up;

This will set $give_up on each try in such a way that a sucessful run will result in $give_up == 0, while a failed run will result in $give_up == 1. We can then check that value to see if we gave up or no.

radiantmatrix
require General::Disclaimer;
s//2fde04abe76c036c9074586c1/; while(m/(.)/g){print substr(' ,JPacehklnorstu',hex($1),1)}


In reply to Re: catching failures and retrying by radiantmatrix
in thread catching failures and retrying by mickey

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.