perrin has asked for the wisdom of the Perl Monks concerning the following question:
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:
And this is the definition of the retry() sub:retry( sub { $browser->get( $link->url() ); } );
This assumes that a failed action will throw an exception, which is what happens with the settings I'm using with Mechanize.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 +" } }
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: retrying an action that could fail
by tachyon (Chancellor) on May 24, 2004 at 03:27 UTC | |
by perrin (Chancellor) on May 24, 2004 at 03:57 UTC | |
|
Re: retrying an action that could fail
by dws (Chancellor) on May 24, 2004 at 04:12 UTC | |
|
Re: retrying an action that could fail
by Fletch (Bishop) on May 24, 2004 at 03:27 UTC | |
by perrin (Chancellor) on May 24, 2004 at 03:56 UTC | |
|
Re: retrying an action that could fail
by gjb (Vicar) on May 24, 2004 at 07:40 UTC | |
|
Re: retrying an action that could fail
by Abigail-II (Bishop) on May 24, 2004 at 09:17 UTC | |
|
Re: retrying an action that could fail
by demerphq (Chancellor) on May 24, 2004 at 12:57 UTC | |
by perrin (Chancellor) on May 24, 2004 at 16:41 UTC | |
by demerphq (Chancellor) on May 24, 2004 at 19:15 UTC | |
|
Re: retrying an action that could fail
by fglock (Vicar) on May 24, 2004 at 15:27 UTC | |
by simonm (Vicar) on May 24, 2004 at 18:54 UTC |