in reply to Re: loop control
in thread loop control

One minor little 'gotcha' when dealing with cadence -- you actually want the 43200/$n to be the time between successive starts, not between the completion of one and the start of the next (ie, the time to sleep).

To compensate, if we're dealing with something that takes any significant time to run an iteration, we need to track how long each run takes, and subtract that from the time that we're going to sleep:

use IO::Socket; my $sock = new IO::Socket::INET("$remote_server:43") || die $!; my $n = 5; # do this 5 times every 12 hours while (1) # loop forever { my $time = time(); # keep track of when we start print $sock "$data\r\n"; my @output = <$sock>; $output = join('', @output); if ($output =~ /$no_match/gi) { &true; } else { &false; } # sleep for the PERIOD, minus the DURATION sleep( int(43200 / $n) - ( time() - $time) ); }

Replies are listed 'Best First'.
Re^3: loop control
by webshark (Acolyte) on Dec 02, 2005 at 13:50 UTC
    cheers guys... just what I needed.. ;-)
Re^3: loop control
by webshark (Acolyte) on Dec 06, 2005 at 11:13 UTC
    hi guys.. does the loop restart the tcp connection or would this remain open until i close it? is there anyway of piping the servers resonse to log with date & time etc? I don't need to wait for servers response before sending data.. cheers
Re^3: loop control
by webshark (Acolyte) on Dec 10, 2005 at 12:41 UTC
    I think this is nested loops..?? if I have another loop within this routine... how can I still get the while (1) loop to work..
    my $n = 5; # do this 5 times every 12 hours while (1) # loop forever { my $time = time(); # keep track of when we start print $sock "$data\r\n"; my @output = <$sock>; $output = join('', @output); $|=1; while(<$sock>) { if ( $output is this)
    cheers steve