http://www.perlmonks.org?node_id=282322

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

I am trying to find a way to do time-limited input of a list of words. Basically:

use strict; print "Prompt: "; my $end = time() + 10; my $input; while ($end > time()) { $input .= <STDIN>; } my @words = split /\s+/, $input; print "$_\n" foreach @words;

This works OK. If I press enter after every word, once I am over the time limit it stops taking input. However, I do not want to rely on the user pressing enter.

What I think would work is to set $/ to whitespace. I tried using $/ = '\s'; with no luck -- \s is for regexes, IIRC.

Obviously, a user could sit in the middle of a word for as long as they want, but for my purposes that is OK. As long as whitespace is the separator, they can't cheat by entering 100 words on the same line before pressing enter.

Although, if monks have any ideas about how to stop input at 10 seconds regardless of wether the user was in the middle of a word or not, I'd love to hear them.

</ajdelore>