Giving up on user input - keyboard timeout in Perl
Archive - Originally posted on "The Horse's Mouth" - 2009-08-28 10:55:03 - Graham EllisThere's an old piece of code that I've used to demontstrate how (in Perl) you can prompt for a user input ... but then time out the input on an alarm signal if the user doesn't enter data within a certain time. The code's [here] if you want to have a look. But let me warn you ... it's one of those thisng that may be somewhat operating system dependent, even though most Perl code is pretty darned portable.
A correspondent writes: "try the code; it hangs after the first warning. The comeon signal handler needs to call the alarm and increment the counter because the code in the main loop that looks to do so is never reached when no entry is typed. a correction would look like [this] (good exercise for student, by the way, so thanks)"
Hmmm ... on the laptops that I'm working on today (Mac OS X / Perl 5.8.x) both versions work perfectly. The second version (from M.K.) illustrates his point, but with a degree of code duplication so it could really do with being tidied up ... and I have not yet tested it across a range of platforms. I'm posting links to both examples, and these notes, because waiting for a user input ... but only for a while, before carrying on ... is a very useful capability, and these notes may be of some help to the reader, even though I have not fully researched the story.
What is does remind me of is that saying:
and the need to check and recheck code across platforms, and HTML and JavaScript across browsers!