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


In reply to Menu system uses the "calling stack" by jmClifford

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.