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 | |
|
Re^3: loop control
by webshark (Acolyte) on Dec 06, 2005 at 11:13 UTC | |
|
Re^3: loop control
by webshark (Acolyte) on Dec 10, 2005 at 12:41 UTC |