Hello Everyone, first time posting on PM.

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".

"1" => \&get_ifc_name({'ifc_default' => 'test_me',}),
To fix the problem but lose arguments:
"1" => \&get_ifc_name,

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".
#!/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;
I am learning perl on a case by case basis so expect to see some bad habits in in the code above.

In reply to Subroutine references inside of a hash with arguments. by shift9999

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.