jmClifford has asked for the wisdom of the Perl Monks concerning the following question:

Hi. I have a simple text-base menu system (from the web). I am surprised that all the subroutines involved keep adding the calls to the stack with continual usage. The code is (along with a console output for an execution) ;

# THE CODE ############################# #!/usr/bin/perl -w # a simple text-based menu system use strict; my ($menu1, $menu2); # Sample menus are defined below. Each menu is an anonymous # array. The first element is the title of the menu. The following # elements are anonymous arrays containing a description of the # menu item (it will be printed) and a reference to a routine to call # should that item be selected. # The following is a shortcut that can be used in other menus. my $quit_menu_item = [ "Quit", sub{exit;} ]; $menu1 = [ "Title of First Menu", [ "Do Something (first)", \&ds1 ], [ "Second Menu", sub{ &menu( $menu2 )} ], [ "Continue", sub {}], [ "Quit", sub {exit;} ], # We could have used our shortcut. ]; $menu2 = [ "Title of Second Menu", [ "Do Something (second)", \&ds2 ], [ "First Menu", sub{ &menu( $menu1 )} ], $quit_menu_item, # This is our shortcut. ]; ##### The menu routine itself. ##### sub menu { my $m = shift; my $choice; while (1) { print "$m->[0]:\n"; print map { "\t$_. $m->[$_][0]\n" } (1..$#$m); print "> "; chomp ($choice = <>); last if ( ($choice > 0) && ($choice <= $#$m )); print "You chose '$choice'. That is not a valid option.\n\n"; } &{$m->[$choice][1]}; } print "a\n"; # Do something 1 sub ds1 { print "\nIn ds1\n\n"; &menu($menu1); print "In ds1 after return to menu \n"; } print "b\n"; # Do something 2 sub ds2 { print "\nIn ds2\n\n"; &menu($menu2); print "In ds2 after return to menu \n"; } print "c\n"; ## TEST &menu($menu1); print "Continue after menu call \n"; # THE CONSOLE ############################# a b c Title of First Menu: 1. Do Something (first) 2. Second Menu 3. Continue 4. Quit > 1 In ds1 Title of First Menu: 1. Do Something (first) 2. Second Menu 3. Continue 4. Quit > 1 In ds1 Title of First Menu: 1. Do Something (first) 2. Second Menu 3. Continue 4. Quit > 2 Title of Second Menu: 1. Do Something (second) 2. First Menu 3. Quit > 1 In ds2 Title of Second Menu: 1. Do Something (second) 2. First Menu 3. Quit > 1 In ds2 Title of Second Menu: 1. Do Something (second) 2. First Menu 3. Quit > 2 Title of First Menu: 1. Do Something (first) 2. Second Menu 3. Continue 4. Quit > 3 In ds2 after return to menu In ds2 after return to menu In ds1 after return to menu In ds1 after return to menu Continue after menu call

With the console output (above), by using the 3 option I continue the program but calling a dummy subroutine (instead of an exit type routine). With this type of usage, you can then appreciate the sequence calls made in accordance to how I have used the menu selections. My concern here is, am I using up the resources of the machine when all I want is a single stack frame stored for just the last subroutine call ?? and a knowledge of where the program counter should continue from when an option 3 (continue) type of operation is performed.

Regards JC....

Replies are listed 'Best First'.
Re: Menu system uses the "calling stack"
by Corion (Patriarch) on Dec 31, 2024 at 09:01 UTC

    You might want to restructure your code such that the menu() function only returns the selection instead of immediately executing it.

    Then, you would have a main loop that repeatedly invokes menu() and then executes the choice:

    sub menu { return int(rand(4)+1); } our $main_menu = [ [ "Second Menu", sub{ $current_menu = $menu2 } ], ]; our $menu2 = [ [ "Return", sub{ $current_menu = $main_menu } ], ]; sub mainloop { do { my $choice = menu(); $m->[$choice]->[1]->(); } while( $choice != 4 ); }

    To incorporate the second menu, you could either have a reference to the "current menu", or a second "main"loop:

    our $current_menu; sub menu { for my $entry (@$current_menu) { say $entry->[0]; }; return int(rand(@$current_menu)+1); } sub mainloop { do { my $choice = menu(); $current_menu->[$choice]->[1]->(); } while( $choice != 4 ); }
      I believe that's known as a "trampoline" pattern, in that (my explanation of the term) it returns something to jump off (so the caller can use it, keeping the same depth of stack), rather than doing the jumping itself (i.e. increasing the depth of stack).
Re: Menu system uses the "calling stack"
by LanX (Saint) on Dec 31, 2024 at 12:08 UTC
    Your menu() function has a loop.

    I would call the entries inside and not after the loop.

    The entries have to explicitly return after being done, not calling the menu() again.

    If you want a menu for another level call menu() explicitly, like that your sub-menu is in the next call stack.

    FWIW, if you really need this flow but without littering the call stack, you might also use the goto &NAME feature in perl after pushing the arguments onto @_ .

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    see Wikisyntax for the Monastery