Hi,
I am trying to use "alarm" as a timeout mechanism inside a foreach loop. I am using the $SIG{ALRM} signal to execute a sub command that includes a "next" statement. Unfortunately, the sub command has trouble with the "next".
Here is my example:
#!/usr/bin/perl
use strict;
use warnings;
my @time=(4,12,16,8);
LABELME: foreach my $t (@time)
{
$SIG{ALRM} = sub {print "timeout\n"; next LABELME};
print "Start $t\n";
alarm(10);
sleep($t);
alarm(0);
print "done with $t\n";
}
print "All Done!\n";
And this is what prints:
Start 4
done with 4
Start 12
timeout
Exiting subroutine via next at postalarm.pl line 8.
Exiting eval via next at postalarm.pl line 8.
Label not found for "next LABELME" at postalarm.pl line 8.
Thank you in advance for your help!
-jb_using_perl
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.