
BillKSmith
Veteran
Jul 23, 2012, 8:36 PM
Post #2 of 3
(827 views)
|
|
Re: [cwells3rd] Dynamic menu using perl hash
[In reply to]
|
Can't Post
|
|
Because your options are always numeric, I would store the menu in an array rather than a hash. If you know all possible menu values, you can write a subroutine for each one. Build an array of code references to associate the code with the option numbers. The following code is a small sample of how that works. Of course, you would have to build the arrays dynamically.
use strict; use warnings; my @options = ( sub {}, \&all, \&default, \&web, ); print <<"MENU"; 0 exit 1 all 2 default 3 web MENU print "Option: "; while ((my $option = <>) != 0) { $options[$option]->(); print "Option: "; } sub all { print "executing all\n"; } sub default { print "executing default\n"; } sub web { print "executing web\n"; } Good Luck, Bill
|