in reply to Re: Building a Perl Menu
in thread Building a Perl Menu
#!/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; } } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Building a Perl Menu
by Bloodnok (Vicar) on Apr 29, 2009 at 09:40 UTC |