http://qs1969.pair.com?node_id=293084

semio has asked for the wisdom of the Perl Monks concerning the following question:

fellow monks

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.

#!/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"; }
The output to this would look something like this:
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
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:
01:03:39 01:03:40 01:03:42 01:03:45 01:03:48 01:03:51 01:04:54 01:04:57
Thanks in advance for any suggestions.

cheers, -dave