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; }
In reply to prompt and timeout in tests by leocharre
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |