Consider the following wrapper function that retrys a given function some given number of times if the function throws (not sure why the formatting is wonky):
sub tryit{
my $fun = shift;
my $times = shift;
my @args = @_;
my $ret;
do{
$times--;
eval{
$ret = $fun->(@args);
};
if($@){
print "Error attemping cmd: $@\n";
}
else{
return $ret;
}
}while($times > 0);
return;
}
How can this be extended so that the return value of the parameter function is properly propigated up no matter what kind of value is returned? For instance, this function won’t pass an array up properly. You can’t just return $fun->() because the return only takes you out of the eval block.
You can do this with wantarray. (It is formatting wonky for me, too; sorry)
I am sure a monster Perl hacker could find a way to tighten this up, but that is the basic idea.