#!/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);