leocharre has asked for the wisdom of the Perl Monks concerning the following question:
I have this sub to prompt the user for a response.
I'd like to have something like this that times out, what's good way of doing that?
sub yn { my $question = shift; $question ||='Your answer? '; my $val = undef; until (defined $val){ print "$question (y/n): "; $val = <STDIN>; chomp $val; # $val = # $val eq 'y' ? 1 : ( # $val eq 'n' ? 0 : undef # ); if ($val eq 'y'){ $val = 1; } elsif ($val eq 'n'){ $val = 0; } else { $val = undef; } } return $val; }
(I guess what I'm missing is some kind of a counter, because the thing hangs waiting for stdin forever.. Maybe I could force to read stdin every 1 second, and thus I can count time.. but that would possibly yank the user's input before they concede, which is brutal. hmm.. )
I want to use this in tests, so that if there's no human being I can go on as needed. Are there some special precautions I should take when retrieving user input in my tests? I know they're picky about stdout (Test::Simple), right?
I want to optionally conduct intrusive tests (for example connecting to a database during testing ), defaulting to no.
update
This look interesting too, will test if the script was called via the command line?
Is there a major CAVEAT? Like, only runs on POSIX (muahhaha)? Is this kind of check reliable?# from http://pleac.sourceforge.net/pleac_perl/userinterfaces.html sub I_am_interactive { return -t STDIN && -t STDOUT; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: prompt and timeout in tests
by kyle (Abbot) on Jan 15, 2008 at 21:49 UTC | |
|
Re: prompt and timeout in tests
by chromatic (Archbishop) on Jan 15, 2008 at 22:49 UTC | |
by leocharre (Priest) on Jan 15, 2008 at 23:10 UTC | |
|
Re: prompt and timeout in tests
by Joost (Canon) on Jan 15, 2008 at 22:05 UTC | |
by leocharre (Priest) on Jan 15, 2008 at 23:04 UTC | |
|
Re: prompt and timeout in tests
by webfiend (Vicar) on Jan 16, 2008 at 02:32 UTC | |
|
Re: prompt and timeout in tests
by jwkrahn (Abbot) on Jan 16, 2008 at 00:40 UTC |