Hi Perrin. That was mostly an example. Usually for that kind of thing its more intuitive to use a for loop and last out of it at the appropriate time. Where redo can be useful however is if you want to back out of an position in an if structure. But thats another node.

What I more would be thinking is the stuff below. :-) (This is incidentally how the SKIP blocks work in tests.)

#!perl -l use strict; use warnings; no warnings 'exiting'; my ($x); sub do_onething { if ($x) { print $x--; redo ATTEMPT } else { $x=10; print "Done onething."; } }; sub do_another { if ($x) { print "Done another."; last ATTEMPT } }; $x=10; ATTEMPT:{ do_onething } ATTEMPT:{ for ('A'..'Z') { print $_; do_another; } }

That is that you can precan a bunch of routines that know how to jump out of specifically named places. So now we can do stuff like

sub retry(&$) { my $sub_ref = shift; my $max = shift ||3; ATTEMPT: for my $try (1..$max) { eval { $sub_ref->(); }; last unless $@; warn "Failed $try, retrying. Error: $@\n" } if ($@) { die "failed after $max tries: $@\n" } } my $t=0; retry { print "Seeya!" and last ATTEMPT if $t++>4; die "failure"; } 10;

:-)


---
demerphq

    First they ignore you, then they laugh at you, then they fight you, then you win.
    -- Gandhi



In reply to Re: Re: Re: retrying an action that could fail by demerphq
in thread retrying an action that could fail by perrin

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.