Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Trouble Creating a Menu

by Marshall (Canon)
on Aug 13, 2012 at 13:29 UTC ( [id://987115]=note: print w/replies, xml ) Need Help??


in reply to Trouble Creating a Menu

These command loops are tricky to write.

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__

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://987115]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2024-04-24 12:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found