Rhodium has asked for the wisdom of the Perl Monks concerning the following question:

Guten tag monks

I have a simple little subroutine which given a question and an "approved" answer list will verify that the user gave a valid answer and if a default answer (from a config file for example) is provided will validate that as well, without questioning. Here is that little bit of code:

. . $runset{CHECK} = &AskQuestion("CHECK - Enter the check mode you want", + my @checkmode = qw/flat cell hier comp/, $conf{CHECK}); . . $runset{CNAMES} = &AskQuestion("CNAMES - Do you want the run Case Sens +itive", my @CS = qw/yes no/, $conf{CNAMES}); . . # Subroutine - AskQuestion # This will take a simple text for an answer and given a predetermined # array of answers will only accept one of the choices # # Obviously this returns a valid answer # sub AskQuestion (*\@;$){ my $prompt = shift; my $conf = pop; my @answers = @_; my $response; while ( ! $response ) { if ( ! $conf ) { print "$prompt [$_[0]]: "; $response = <STDIN>; chomp $response; if ( $response eq "" ) { $response = $answers[0]; } if ( $response eq "?") { $response = ""; print " Valid answers are: \n"; print " @answers\n"; } } else { $response = $conf; } my $match; for ( my $i = 0; $i < @answers ; $i++ ){ if ( $response eq $answers[$i] ){ $match = "true"; return $response; } } if ( ! $match ){ $response = ""; $conf = ""} } }

Now keep in mind I am asking about 50 questions and a fair number of these are "yes no" or "no yes" type questions. The difference being a "yes no" defaults to "yes". Anyway my users want to be able to simply type y or n for these type of questions. Can anyone come up with a way to modify the existing subroutine to allow a y or n answer when the question has a default answer of "yes no" or "no yes".
Thanks muchos


Rhodium

The <it>seeker</it> of perl wisdom.

Replies are listed 'Best First'.
Re: The default question subroutine YesNo
by derby (Abbot) on May 02, 2002 at 19:38 UTC
    why stop at y or n? If y, ye, yes are all acceptable, take a look at Text::Abbrev.

    -derby