I have been working on a menu system and have hit a snag. I cannot get the system to work when passing arguments to the different menu subroutines in a hashref form. It will work when the sub is called with no variables. The arguments are making it to the sub and work fine but when the menu_system sub is called a second time it begins to act in a very bizarre manner.
It is hard to explain exactly how this system worked so please see the many comments in the code below. To reproduce the problem use the following in the code below under the "menu_hash".
To fix the problem but lose arguments:"1" => \&get_ifc_name({'ifc_default' => 'test_me',}),
This menu system has worked fine in the past when no arguments are included in the subroutine call. I think it may be due the complexity of the subroutine being a reference inside of the "menu_hash"."1" => \&get_ifc_name,
I am learning perl on a case by case basis so expect to see some bad habits in in the code above.#!/usr/bin/perl use warnings; print "************************************\n"; print "* test - shift9999\n"; print "* Version 0.1 - \"The mule\"\n"; print "************************************\n"; print "\n"; #Define args my($ifc_name); my($ipaddress); #run the menu_system for the first time. &menu_system; sub menu_system($bs_index){ #Define our menu system here with the appropriate subs and their argum +ents my %menu_hash = ( "1" => \&get_ifc_name({'ifc_default' => 'test_me',}), "2" => \&get_ip_address, ); #first run check to setup the first menu if(!$bs_index) { #start things off at the first menu $bs_index = "1"; } else { #Get menuid from Arg - Don't mind this, never removed the unnecessary. my($temp_hld) = @_; $bs_index = $temp_hld; } #check to see if the menu index is defined, otherwise display an error +. if (defined $menu_hash{$bs_index}) { #menu is valid, run the sub $menu_hash{$bs_index}->(); } else { print "***Menu System Error***\n"; } } sub get_ifc_name{ #get hashref from args my ($inp) = @_; #prompt user for input print "Enter the Public Interface: [ \"$inp->{'ifc_default'}\" +]\n"; $ifc_name = <STDIN>; chomp($ifc_name); #if no input use default value from the arguments passed to this sub if (!$ifc_name) { print "Using Deafult Interface: [ \"$inp->{'ifc_default'}\" + ]\n\n"; $ifc_name = $inp->{'ifc_default'}; #call the next menu &menu_system("2"); } else { #*** will push these checks into a reusable sub later but for now +just take care of it here. #check if the user wants to go back or restart the section. if($ifc_name =~ /back/){ print "\n***Cannot go back any further***\n\n"; &menu_system("1"); } elsif ($ifc_name =~ /skip/){ print "\n***Cannot skip this step***\n\n"; &menu_system("1"); } else { # user entered a valid name, lets use that instead of the defa +ult. print "Using Device Name: [ \"$ifc_name\" ]\n\n"; &menu_system("2"); } } } sub get_ip_address{ #get ip address print "IP Address for: [ \"192.168.6.100\" ]\n"; $ipaddress = <STDIN>; chomp($ipaddress); } exit;
In reply to Subroutine references inside of a hash with arguments. by shift9999
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |