I should say that I do not often write menu driven command line interfaces because they are hard to automate. The UI and process is just easier with a Unix style command.
Perhaps here ">cold comp5" would be better than a menu choice?
Anyway, first off, I do have a decided preference for while loops as opposed to do{...}until loops. The reason is that I like to see the loop ending condition at the front of the loop. But there is no real issue with using a do loop.
I guess the general "rules" are:
1) prompt the user in a clear way.
2) ignore completely any leading or trailing spaces.
3) a blank line is just a re-prompt (not an error).
4) protect the user from "faults" - if 0 is not allowed
then that is an error input - error message and reprompt.
5) With a menu, have a clear way for the user to "quit" without
going to the extreme of the CTRL-C.
6) Quality of error messages vary widely - application specific.
There can be a lot of time spent on this. How much depends
upon who your users are. Same thing for help messages, etc.
Here is another way to write this menu loop:
#!/usr/bin/perl -w use strict; my $computer; my @pcs = qw(comp1 comp2 comp3 comp4 comp5); while ( $computer = prompt4number(@pcs), $computer !~/^\s*(Q|quit|exit)\s*$/i) { next if $computer =~ /^\s$/; #simple re-prompt on blank lines if( $computer !~ /^\s*\d\s*$/ or $computer>@pcs or $computer == 0) { print "Illegal Entry -- try again!\n\n"; next; } print "OK - cold starting $pcs[$computer-1]\n"; exit(0); } print "Program would just exit ...\n"; sub prompt4number { my @pc = @_; print "\nWhich computer would you like to cold start?\n"; my $menu =1; print $menu++, " - $_\n" foreach @pc; print "enter Q|q|quit|exit to stop this program!\n"; return <STDIN>; } __END__
In reply to Re: Trouble Creating a Menu
by Marshall
in thread Trouble Creating a Menu
by at2marty
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |