package Menu; =head1 NAME Menu -- Menu utilities for extropy =head1 SYNOPSIS my $command = Menu->menu("Do this", THIS, "Do that", THAT, "Do something else", SOMETHING); =head1 DESCRIPTION This package provides menu utilities for the extropy. system. =head1 VERSION 0.001 (last update: 6/30/04) =head1 AUTHOR Chet Langin, clangin@siu.edu SIU Plant Biotechnology and Genomics Core-facility =head1 BUGS None known. =head1 SEE ALSO extropy ExtropyUtils ExtropyConstants Extropy::MenuMain =head1 COPYRIGHT Copyright 2004, Chet Langin, All Rights Reserved. This program is free software. You may copy or redistribute it under the same terms as Perl itself. =head1 METHODS The remainder of this document describes the methods available to the programmer. =cut # load the pragmas use warnings; use strict; # load other modules use ExtropyUtils; use ExtropyConstants; # ******************************** menu ***************************** =head2 menu(...) Displays a menu and returns an integer which represents the selection. The parameters are string/int pairs that indicate menu items and the integers representing each item. When an item is selected, the integer representing that item is returned. For example: my $command = Menu->menu("Do this", THIS, "Do that", THAT, "Do something else", SOMETHING); The first argument indicates that six more arguments follow. The menu items that will be displayed are "Do this," "Do that," and "Do something else." If "Do that" is selected, the constant THAT will be returned. =cut # -------------------------------------------------------------------- sub menu { shift; my $num_params = scalar @_; my $num_items = $num_params / 2; my @item_array = (); my @command_array = (); my @char_array = (); my $user_input = ""; my $command = NONE; for(my $lcv = 0; $lcv < $num_items; $lcv++) { $item_array[$lcv] = shift; $command_array[$lcv] = shift; $item_array[$lcv] =~ /\((\w)\)/; $char_array[$lcv] = $1; } # for my $repeat = TRUE; while($repeat) { print("\n\n"); print(" **************************************\n"); print(" *********** extropy menu ***********\n"); print(" **************************************\n"); print(" * *\n"); for(my $lcv = 0; $lcv < $num_items; $lcv++) { my $item_number = $lcv + 1; my $output = sprintf("% 2s %-28s", $item_number, $item_array[$lcv]); print(" * $output *\n"); } # for print(" * *\n"); print(" **************************************\n"); print(" **************************************\n\n"); print(" Enter a menu item...\n\n "); chomp($user_input = ); $user_input =~ s/^\s*//; $user_input =~ s/\s*$//; for(my $lcv = 0; $lcv < $num_items; $lcv++) { my $item_number = $lcv + 1; if($user_input eq $item_number || uc($user_input) eq uc($char_array[$lcv])) { $command = $command_array[$lcv]; $repeat = FALSE; } # if } # for if($repeat) { message_start; message("The last menu selection was not valid."); blank_line; message("Enter a menu item number or else enter the character"); message("in parentheses () representing a menu item."); blank_line; message("For example, enter a \"1\" (without the quote marks)"); message("to select the first menu item."); blank_line; message("Or enter \"Q\" (without the quote marks) to select the"); message("(Q)uit menu item."); blank_line; press_enter; $repeat = FALSE; } # if } # while $command } # menu 1