perlquestion
semio
fellow monks <p>
I would like to enforce a time-out for each element in an array as it is processed in a foreach loop. After a prescribed time limit, I would like to go to the next element in an array. To illustrate this, I have put together the following sample script. <br>
<code>
#!/usr/bin/perl -w
use strict;
use POSIX qw/strftime/;
strftime "%H:%M:%S", localtime;
my @sleepvals = qw /1 2 3 4 5 6 7 8/;
print strftime "%H:%M:%S", localtime;
print "\n";
foreach my $sleepval (@sleepvals) {
sleep($sleepval);
print strftime "%H:%M:%S", localtime;
print "\n";
}
</code>
The output to this would look something like this: <br>
<code>
01:03:39
01:03:40
01:03:42
01:03:45
01:03:49
01:03:54
01:04:00
01:04:07
01:04:15
</code>
I would like to enforce a time-out where, if sleep() takes longer than 3 seconds, sleep() is terminated, the rest of the foreach loop is processed and the script proceeds to the next element in the array. As an arbitrary example, I would like to set each iteration to, say 3 seconds, so the output would look as follows:<br>
<code>
01:03:39
01:03:40
01:03:42
01:03:45
01:03:48
01:03:51
01:04:54
01:04:57
</code>
Thanks in advance for any suggestions.<p>
cheers, -dave