in reply to questions, questions

Your use of grep was wrong. It's for testing lists - you just wanted a single match there. The redo isn't wrong, but can catch someone off guard. Overall the thing looks much like C. More idiomatically, it looks like this:
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
    Thanks! Good points all round. As to style, guess I'm a grunt.
    Only thing is "uc". It limits the avalible args

      It does, but if you wanted case sensitive behaviour, the /$answer/i in your original code was wrong. In any case, just drop the ucs if that's what you need. One could also make that an option..

      Don't worry about style, it's just a matter of experience. :-) Being a Pascalite, my first Perl scripts looked rather.. odd.

      Makeshifts last the longest.