Wise and great monks, I know you'll put me on the right path to enlightenment. I'm sure I'm overlooking something simple. I have a text menu where you select an option by number. If you select an $answer >= 4 it loops just fine back to the menu options, but if the user just hits the "Enter" key it errors out. This line sort of works, if ( $answer >= 4 || $answer eq "" ), I still get a warning, but it does loop and print the menu again. Also, I've tried the following pieces of code in different parts of my code, but no success:

$answer =~ s/\s+$//g; $answer =~ tr/\015//d;

Below is my code:

#!/usr/bin/perl use strict; use warnings; use diagnostics; my $x = 0; my $answer; my %menu_selection = ( '1' => "Option 1....", '2' => "Option 2....", '3' => "Option 3...." ); sub menu () { print STDOUT <<EOF; Please select one of the following locations: 1.) Option 1.... 2.) Option 2.... 3.) Option 3.... EOF print "\nSelection: "; chomp( $answer = <STDIN> ); } while ( $x == 0 ) { &menu; if ( %menu_selection ) { if ( $answer >= 4 || $answer eq "" ) { print "The menu selection is: $answer\n"; print "\nWrong selection, please make a different selec +tion.\n\n"; $x = 0; } else { print "$menu_selection{$answer}\n"; $x = 1; } } }

Should I assign a string or numeric value to $answer before making any reference to it? I know there is the "Term::Menu" module (which may simplify things), but I don't want to install it on a 1200+ nodes. Just FYI, the script runs on Linux only. Thank you in advance for any help!


In reply to Text Menu Not Looping on CR/LF by ritz303

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.