#!/usr/bin/perl use strict; use Menu; my $username=getlogin(); my $infile="/tmp/$username.inFile"; my ($script,$infile=mainMenu($infile);
sub mainMenu { my $infile=shift; my ($choice,$notice); while (1) { system("clear"); print BOLD BLUE <<EOF; PLEASE CHOOSE AN OPTION FROM BELOW EOF print GREEN <<EOF; (A) Account Administration ------------------------------- EOF if ($notice ne "") { print RED "\n\n$notice\n\n"; $notice=""; } my $choice=getInput("Please enter your choice > "); if ($choice =~ /a/i) { accountMenu($infile); last; } else { $notice="INVALID OPTION!"; next; } } } sub accountMenu { my $infile=shift; my ($notice,$choice); while (1) { system("clear"); print BOLD BLUE <<EOF; PLEASE CHOOSE AN OPTION FROM BELOW EOF print GREEN <<EOF; (1) Add Account(s). ------------------------------------------------- (M) Back to Main Menu. EOF if ($choice == 1) { addAccount($infile); last; } else { $notice="INVALID OPTION"; next; } }
sub getInput { my $message = shift; my $return; print "$message"; chomp($return=<STDIN>); return $return; }
Now, the user can then load a file, at which point they type in full path to file, and then all options are validated and if file is good then their infile gets returned as the infile, and script gets returned. If a line in user infile is not valid then they must fix the error. Anyway, this is a huge convuluted way of doing a simple task, and I just know there is an easier way of doing this.sub addAccount { my $infile = shift; while (1) { system("clear"); print BOLD BLUE <<EOF; PLEASE CHOOSE AN OPTION FROM BELOW EOF print RED <<EOF; (1) Load User File. (2) Manually Enter Each User. ------------------------------------------------- (A) Back to Account Administration Menu. (M) Back to Main Menu. (E) Exit. EOF if ($notice ne "") { print RED "\n\n$notice\n\n"; $notice=""; } my $choice=getInput("Please enter your choice > "); if ($choice == 1) { loadFile(); last; } elsif ($choice == 2) { manuallyEnterUser(); last; } elsif ($choice =~ /a/i) { accountMenu(); } elsif ($choice =~ /m/i) { mainMenu(); } elsif ($choice =~ /e/i) { exit(0); } else { $notice="INVALID OPTION"; next; } } }
In reply to Re^2: Building a Perl Menu
by walkingthecow
in thread Building a Perl Menu
by walkingthecow
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |