Check out Conway's Perl Best Practices. From Chapter 13: Error Handling, Section Recoverable Failure, we find the following technique:
use Carp qw(croak);
use English qw(-no_match_vars);
use Net::FTP;
use Time::HiRes qw(sleep);
use strict;
my $MAX_TRIES = 15;
my $ftp;
TRY:
for my $try (1..$MAX_TRIES) {
# If successful, we're done.
eval {
$ftp = Net::FTP->new("some.host.name", Debug => 0)
or die "Cannot connect to some.host.name: $@";
last TRY;
};
# Report non-recoverable failure if no more tries
croak( $EVAL_ERROR ) if $try == $MAX_TRIES;
# Try again after a nap (approximate fibonacci)
sleep( rand (1.618 ** $try) );
}
- Miller
Update: Missing Semi-colon. |