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


In reply to enforce timeout in loop by semio

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.