my @choices = ("yes", "no", "y", "n"); my $choice = query("Can you use an array for the choices?", "YES", @choices); my $otherChoice = query("Can you use an invalid default to force them to type in their choice?", "yes/no", "yes", "y"); my $finalChoice = query("Oh, just enter whatever you want."); #### # &query(Prompt String, optional default, optional list of restrictive choices) # If the choices are provided at the end, only one of those can be chosen. # This is currently case insensitive. # # Example: my $val = query("Do you like config scripts?", "yes", "yes", "no", "y", "n"); # Will print: Do you like config scripts? [yes]: # The user can either press enter for yes, or enter one of the other arguments. sub query { my $prompt = (defined $_[0]) ? $_[0] : ""; my $default = (defined $_[1]) ? $_[1] : ""; my $answer; while(1) { print "$prompt [$default]: "; chomp($answer = ); $answer = $default if ($answer eq ""); last unless $#_ > 1; last if grep $answer =~ /^\Q$_\E$/i, @_[2 .. $#_]; } return $answer; }