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>
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Time-limited input on STDIN
by adrianh (Chancellor) on Aug 08, 2003 at 22:20 UTC | |
by Ackolight (Initiate) on Apr 07, 2009 at 23:21 UTC | |
Re: Time-limited input on STDIN
by jamesduncan (Novice) on Aug 08, 2003 at 22:33 UTC | |
by bobn (Chaplain) on Aug 08, 2003 at 22:42 UTC | |
by ajdelore (Pilgrim) on Aug 11, 2003 at 17:58 UTC | |
Use POE, was Re: Time-limited input on STDIN
by RMGir (Prior) on Aug 09, 2003 at 09:51 UTC |