in reply to questions, questions
use Term::ReadKey; sub Request { my ($prompt, $letters, $linefeed) = @_; return unless $prompt; $letters ||= "Ny"; $linefeed ||= 1; my ($answer, $is_correct); do { print "$prompt? [$letters] "; ReadMode 'cbreak'; $answer = uc ReadKey 0; ReadMode 'normal'; $is_correct = (index uc $letters, $answer >= 0); print "\nplease answer [$letters]\n" unless $is_correct; } until $is_correct; print "\n" if $linefeed; return $answer; }
Note the use of uc to force things to uppercase so we needn't juggle with case-insensitivity in comparisons. You can then use it as in if(Request(...) eq 'Y') { ... }.
Update: Oops. Now complies with strict.
Makeshifts last the longest.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: questions, questions
by halxd2 (Monk) on Sep 19, 2002 at 20:38 UTC | |
by Aristotle (Chancellor) on Sep 19, 2002 at 20:42 UTC |