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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |