in reply to retrying an action that could fail

You wanted an equivelent that uses the loop operators:

my $count=0; ATTEMPT:{ do_something() or ($count++ < $maximum and redo ATTEMPT) }

Style to taste. :-)


---
demerphq

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


Replies are listed 'Best First'.
Re: Re: retrying an action that could fail
by perrin (Chancellor) on May 24, 2004 at 16:41 UTC
    Thanks. Here's a working demo, adapting form the code I had before. (Note that $max normally comes from my config file.)
    my $max = 3; retry( sub { die "failure" } ); sub retry { my $sub_ref = shift; my $try = 0; ATTEMPT: { eval { $sub_ref->(); }; if ( $@ and $try++ < $max ) { warn "Failed try $try, retrying. Error: $@\n"; redo ATTEMPT; } } if ($@) { die "failed after $max tries: $@\n" } }

      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