&xferOpen ('SESSION', $clusterHost,"compression", "yes", @sshPort) ||  return &error("couldn't connect to $clusterHost : $xferError");

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.