in reply to Re: Subroutine references inside of a hash with arguments.
in thread Subroutine references inside of a hash with arguments.

Thanks for all the great responses! I have not had a chance to read through all of them but I did get the script working the way I wanted, albeit, maybe not the most efficient way.

I have changed the hash into an array as I dropped the menu names early on but never changed the hash. I am going solely off of Indexes now (thanks for pointing that out ikegami!).

I then used an  eval() on the menu subroutine names in the array elements rather than the $menu_hash{$bs_index}->();

I have attached a simple example of the script without the neat menu navigation back\forward\restart\ options that are in the real deal. This is just an example so don't expect a whole lot of error checking and polish.

#!/usr/bin/perl use warnings; print "********************************************\n"; print "* test app\n"; print "* Version 0.2 - \"The mule\"\n"; print "********************************************\n"; print "\n"; #Define global args my @menu_list = ( "&default", "&get_ifc_name({'ifc_default' => 'test.ifc0'})", "&get_ip_address({'ipaddr_default' => '10.20.1.1'})", "&headr_section", ); &menu_system; sub menu_system(){ #Define our menu system here with the appropriate subs and thier argum +ents $bs_index = shift; #first run check to setup the first menu if(!$bs_index){ #start things off at the first menu $bs_index = "1"; } print "\n*** DBG MENU SYSTEM: Index from args: $bs_index ***"; #check to see if the menu index is defined, otherwise display an error +. if (defined $menu_list[$bs_index]){ #menu is valid, run the sub print "\n*** DBG MENU SYSTEM: running ID: $bs_index ***\n\n" +; eval($menu_list[$bs_index]); } else { print "*** Unknown Menu Called***\n"; } } sub get_ip_address() { my($arg) = shift; print "IP for the Interface [ \"$arg->{'ipaddr_default' +}\" ]\n"; $ip_addr = <STDIN>; chomp($ip_addr); &menu_system("3"); } sub get_ifc_name() { my($arg) = shift; print "Enter an Interface Name [ \"$arg->{'ifc_default' +}\" ]\n"; $device_name = <STDIN>; chomp($device_name); &menu_system("2"); } sub headr_section{ print "*******************************************\n"; print "* Section 2 Header\n"; print "* To skip a step, type \"skip\"\n"; print "* To go back a step, type \"back\"\n"; print "* To restart this step, type \"restart\"\n"; print "********************************************\n\n"; }
This may not be the best way to do this but it is working for me without any side effects so far. Any pointers are welcome! I don't mind the criticism.