Here simple function (in a program demonstrating an example use) to present a menu of choices, and call code based on the selection. No modules are required for this quick and dirty menu. Menus can be dynamically generated if you like.
N.B. Anyone considering the use of this snippet will most likely benefit from the code offered in the replies.
#!/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 )} ], [ "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]}; } # Do something 1 sub ds1 { print "\nIn ds1\n\n"; &menu($menu1); } # Do something 2 sub ds2 { print "\nIn ds2\n\n"; &menu($menu2); } ## TEST &menu($menu1);

In reply to Simple text menu by del

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.