is a really crucial line. If the xferOpen returns true, the return &error is never run. If it returns false, it is.
Your replacement breaks this functionality. You run xferOpen 3 times whether or not any of them was successful. Then you always return an error. Your code as written can never get to the return $success line.
Some ways to do what you want:
my $connected; for (my $tries = 1; $tries < 4; $tries++) { print "Attempt $tries to connect\n"; $connected = &xferOpen ('SESSION', $clusterHost,"compression", "yes" +, @sshPort); last if $connected; } return $connected ? $success : &error("couldn't connect to $clusterHos +t : $xferError");
or
my $connected; my $tries = 1; do { print "Attempt $tries to connect\n"; $connected = &xferOpen ('SESSION', $clusterHost,"compression", " +yes", @sshPort); $tries++; } until $connected or $tries > 3;
or
eval (join '||', ('&xferOpen ("SESSION", $clusterHost,"compression", " +yes", @sshPort)') x 3) || return &error("couldn't connect to $cluster +Host : $xferError"); return $success;
Yes, that last was intentionally silly. And, in general, it's good to avoid unlabelled magic numbers like 3. You might like to find a good place to say something like %config = (establish_session_attempts => 3).
In reply to Re: How to wrap a subroutine call in a for loop
by Zed_Lopez
in thread How to wrap a subroutine call in a for loop
by Tuna
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |