#!/usr/bin/perl use strict; use warnings; my @answers = get_user_answers ("First Name", "Last Name"); print "User Said: @answers\n"; @answers = get_user_answers ("Animal1", "Animal2"); print "User Said: @answers\n"; # # get array of answers to an array # of prompts. Any non-blank answer, that # is also a single space separated token is allowed # sub get_user_answers { my (@prompts) = @_; my @answers; foreach my $prompt (@prompts) { my $answer = prompt4answer($prompt); push (@answers, $answer); } return @answers; } # # get a non_blank, single token answer # for a single prompt # sub prompt4answer { my $prompt = shift; my $answer; while ( (print "$prompt: "), $answer = , $answer !~ /^\s*\S+\s*$/) { if ($answer =~ /^\s*\S+\s+\S+/) { print "Error! Only a single word allowed!\n"; } # note: Simply re-prompts (no Error!) on a blank line } # Normal UI convention is to ignore leading and # trailing white spaces $answer =~ s/^\s*//; # trim leading spaces $answer =~ s/\s*$//; # trim trailing spaces # also deletes line endings (\n) return $answer; } __END__ Example session: C:\Projects_Perl\testing>perl UI_multi_prompt.pl First Name: First Name: clark kent Error! Only a single word allowed! First Name: clark Last Name: kent User Said: clark kent Animal1: dog cat Error! Only a single word allowed! Animal1: Animal1: dog Animal2: cat User Said: dog cat