in reply to Printing a subset of a data structure
I think what you're trying to achieve here (correct me if I'm wrong) is to display a submenu only when it's needed, i.e. if the user has selected an item from the main menu which has a submenu. To do this, you're wanting to defer the printing of a submenu. Your code, as shown, does not acheive this; even though you're taking a reference to the print_sub_menu function, you're still calling it right away, and putting the result in the data structure. Instead, you should put a code reference in the data structure. E.g.:
my %menus = ( '1' => { 'ITEM' => 'List and Kill UDT* processes by user', 'ACTION' => sub { $RunCMD->('ListKillProc') }, }, '2' => { 'ITEM' => 'List and Kill Print Jobs', 'ACTION' => sub { $PrintSubmenu->('2') }, . . .
Then, when the user selects an item, you can execute the action; something like this:
$menu->{$MenuItem}->{'ACTION'}->();
Other than that, there's a lot you could do to this to make it more generic and robust.
Might I also suggest that you're suffering from (or inflicting) Indentation Hell. Try it this way:
my %menus = ( '1' => { 'ITEM' => 'List and Kill UDT* processes by user', 'ACTION' => $RunCMD->('ListKillProc'), }, '2' => { 'ITEM' => 'List and Kill Print Jobs', 'ACTION' => $PrintSubmenu->('2'), 'SUBMENU' => { '1' => { 'ITEM' => 'Show all Printers', 'ACTION' => $RunCMD->('showprintersall'), }, '2' => { 'ITEM' => 'Show user print jobs', 'ACTION' => $RunCMD->('showprintersuser'), }, '3' => { 'ITEM' => 'Show single printer', 'ACTION' => $RunCMD->('showprinter'), }, '4' => { 'ITEM' => 'Kill a print job', 'ACTION' => $RunCMD->('killprint'), }, } }, '3' => { 'ITEM' => 'Manage user accounts', 'ACTION' => $PrintSubmenu->('3'), 'SUBMENU' => { '1' => { 'ITEM' => 'Unlock user account', 'ACTION' => $RunCMD->('unlockuser'), }, '2' => { 'ITEM' => 'Change account password', 'ACTION' => $RunCMD->('changepass'), }, }, }, '4' => { 'ITEM' => 'Run App', 'ACTION' => $RunCMD->('APP'), }, '5' => { 'ITEM' => 'quit', 'ACTION' => $RunCMD->('quit'), }, );
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Simple Console Menuing System
by jdporter (Paladin) on May 01, 2006 at 17:49 UTC |