Your method is probably quite sufficient and satisfactory, but I'm trying to get into the habit of offering alternatives. I don't think this is particularly viable for Windows, since I don't believe Perl supports the
alarm() call, and I don't know how signals are supported, but something like this might also accomplish your ends:
$SIG{ALRM} = \&Done;
alarm $timeout; # Set your timeout here, in secs
while (<INPUT>) {
print OUTPUT $_; # Do whatever
}
alarm 0; # So as not to trip ALRM after we're done
+with INPUT
close(OUTPUT); # Likewise, in case we got an EOF in INPUT
+ before $timeout
close(INPUT);
sub Done {
close(OUTPUT); # Do your cleanup and optionally exit
close(INPUT);
exit 0;
}
Correct me if I'm wrong, but once Done returns (assuming you let it), the
while() loop can't continue (since the
INPUT file handle is closed), so program execution could proceed normally at that point. If that's the case, you might want to
localize
$SIG{ALRM}. You can also use a closure instead of a sub reference for the value of
$SIG{ALRM}.
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.