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__

In reply to Re: Trouble Creating a Menu by Marshall
in thread Trouble Creating a Menu by at2marty

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.