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" } }