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;
:-)
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |