title: How can I build a simple menu in Perl?
body: <p>I'm working on a Perl script that requires some basic menu functionality. Ultimately I would like each menu to have a few options and then the option to either return to the previous menu or exit.</p>
  
  <p>example:</p>
  
  <p>This is a menu:</p>
  
  <ol>
  <li>Choice 1</li>
  <li>Choice 2</li>
  <li>Return to previous menu</li>
  <li>Exit</li>
  </ol>
  
  <p>Select an option:</p>
  
  <p>I currently have a menu subroutine making the menus, but there is no functionality allowing it to go back to the previous menu.</p>
  
  <pre><code>    sub menu
      {
          for (;;) {
              print "--------------------\n";
              print "$_[0]\n";
              print "--------------------\n";
              for (my $i = 0; $i &lt; scalar(@{ $_[1]}); $i++) {
                  print $i + 1, "\.\t ${ $_[1] }[$i]\n";
              }
              print "\n?: ";
              my $i = &lt;STDIN&gt;; chomp $i;
              if ($i &amp;&amp; $i =~ m/[0-9]+/ &amp;&amp; $i &lt;= scalar(@{ $_[1]})) {
                  return ${ $_[1] }[$i - 1];
              } else {
                  print "\nInvalid input.\n\n";
              }
          }
      }
  
      # Using the menu
      my $choice1  = menu('Menu1 header', \@list_of_choices1);
  
      # I would like this menu to give the option to go back to
      # the first menu to change $choice1
      my $choice2 = menu('Menu2 header', \@list_of_choices2);
  </code></pre>
  
  <p>I don't want to hard code all of the menus and use if/elsif statements for all of the processing so I turned the menu into a function.</p>
  
  <p>My menus currently look like this...</p>
  
  <p>Menu Header:</p>
  
  <ol>
  <li>Choice1</li>
  <li>Choice2</li>
  <li>Choice3</li>
  </ol>
  
  <p>?: (Enter input here)</p>
  
  <p>This solution still doesn't allow the user to go back to the previous menu or exit though. I was considering making a menu class to handle the menus, but I am still not very good with object oriented Perl.  This is a small program with only a few menus so using a complex menu building module may be overkill.  I would like to keep my code as light as possible.</p>
  
  <h1>EDIT:</h1>
  
  <p>Thanks for the quick responses! However there is still an issue. When I select an option from "Menu1" and it progresses to "Menu2" I would like the save the choice from "Menu1" for later use:</p>
  
  <p>Menu1:</p>
  
  <ol>
  <li>Choice1 &lt;-- store value if selected and go to next menu</li>
  <li>Choice2 &lt;-- ...</li>
  <li>Exit    &lt;-- quit</li>
  </ol>
  
  <p>Menu2:</p>
  
  <ol>
  <li>Choice1 &lt;-- store value if selected and go to next menu</li>
  <li>Choice2 &lt;-- ...</li>
  <li>Back    &lt;-- go back to previous menu to reselect value</li>
  <li>Exit    &lt;-- quit</li>
  </ol>
  
  <p>Selecting either Choice1 or Choice2 should store a value in a variable for later use <strong><em>and</em></strong> progress to the next menu.  Then if you choose to go back to the first menu from Menu2, it will give you the option to reselect your choice and redefine the variable.  I'm trying to avoid using global variables which makes this quite difficult.</p>
  
  <p>After progressing through all of the menus and setting the values of all of these variables, I want to run a subroutine to process all of the choices and print a final output.</p>
  
  <pre><code> sub main () {
  
     # DO MENU STUFF HERE
  
     # PROCESS RESULTS FROM MENU CHOICES
     my $output = process($menu1_choice, $menu2_choice, $menu3_choice, ... );
   }
  </code></pre>
  
  <p>Also if anyone has an object oriented approach to this using classes or some other data structure, although it may be overkill, I would still love to see it and try to wrap my head around the idea!</p>