Heys,

There are a couple ways you could do this - what I'd probably do is set a constant $MAX_RETRIES and then either locally decrement this, or count a loopcounter up to this, using last if a connection is made.

For example: (untested code)

my $MAX_RETRIES = 3; foreach (@host) { foreach $attempt ( 1 .. $MAX_RETRIES ) { my $sock = IO::Socket::INET->new(PeerAddr => $_, PeerPort => $port, Proto => 'tcp', timeout => '10') and $connected = 1; last if $connected; } unless $connected print "$_ is NOT connected.\n" && run_fix(); }

You could extend this to have the script sleep for a set number of seconds before retries, which is maybe a good idea if there's lag involved on the network.

Just some quick ideas ..
Hope that helps.
-- Foxcub


In reply to Re: Trying Not To Cause an Infinite Loop by Tanalis
in thread Trying Not To Cause an Infinite Loop by Anonymous Monk

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.