=pod =head1 NAME Shell::POSIX::Select - The POSIX Shell's "select" loop for Perl =head1 ALERT Unfortunately, this module is difficult to install on many modern platforms. However, there is a package that installs easily on Debian and Ubuntu-based systems (and with some effort, possibly others), which you can find at http://www.google.com/search?sourceid=chrome&ie=UTF-8&q=libshell-posix-select. =head1 PURPOSE This module implements the C [ [ my | local | our ] scalar_var ] B<(> [LIST] B<)> B<{> [CODE] B<}> select [ [my|local|our] scalar_var ] ( [LIST] ) { [CODE] } In the above, the enclosing square brackets I<(not typed)> identify optional elements, and vertical bars separate mutually-exclusive choices: The required elements are the keyword C loop's prompt with a valid input (i.e., a number in the correct range), the variable C<$Reply> is set within the loop to that number. Of course, the actual item selected is usually of great interest than its number in the menu, but there are cases in which access to this number is useful (see L<"menu_ls.plx"> for an example). =head1 OVERVIEW This loop is syntactically similar to Perl's C loop, and functionally related, so we'll describe it in those terms. foreach $var ( LIST ) { CODE } The job of C is to run one iteration of CODE for each LIST-item, with the current item's value placed in Cized C<$var> (or if the variable is missing, Cized C<$_>). select $var ( LIST ) { CODE } In contrast, the C is like an interactive, multiple-choice version of a C loop. And that's cool! What's I so cool is that C loop of the Korn and Bash ("POSIX") shells for Perl. It accomplishes this through Filter::Simple's I service, allowing the programmer to blithely proceed as if this control feature existed natively in Perl. The Bash and Korn shells differ slightly in their handling of C loop uses Cized C<$_> by default (as does the native C loop). See L<"SYNTAX"> for details. The interface and behavior of the Shell versions has been retained where deemed desirable, and sensibly modified along Perlish lines elsewhere. Accordingly, the (primary) default LIST is B<@ARGV> (paralleling the Shell's B<"$@">), menu prompts can be customized by having the script import and set B<$Prompt> (paralleling the Shell's B<$PS3>), and the user's response to the prompt appears in the variable B<$Reply> (paralleling the Shell's B<$REPLY>), Cized to the loop. A deficiency of the shell implementation is the inability of the user to provide a I for each C loop. See L<"IMPORTS AND OPTIONS"> for details. Headings and prompts are displayed in reverse video on the terminal, if possible, to make them more visually distinct. Some shell versions simply ignore bad input, such as the entry of a number outside the menu's valid range, or alphabetic input. I can't imagine any argument in favor of this behavior being desirable when input is coming from a terminal, so this implementation gives clear warning messages for such cases by default (see L<"Warnings"> for details). After a menu's initial prompt is issued, some shell versions don't show it again unless the user enters an empty line. This is desirable in cases where the menu is sufficiently large as to cause preceding output to scroll off the screen, and undesirable otherwise. Accordingly, an option is provided to enable or disable automatic prompting (see L<"Prompts">). This implementation always issues a fresh prompt when a terminal user submits EOF as input to a nested C behaves differently than the native C loop, which nowadays employs automatic localization. foreach $othervar ( ) { } # variable localized automatically print "$othervar DOES NOT RETAIN last value from loop here\n"; select $othervar ( ) { } # variable in scope, or global print "$othervar RETAINS last value from loop here\n"; This difference in the treatment of variables is intentional, and appropriate. That's because the whole point of C loop's variable as C. Another deficiency of the Shell versions is that it's difficult for the programmer to differentiate between a C loop (see L<"Eof Detection">). =head1 IMPORTS AND OPTIONS =head2 Syntax use Shell::POSIX::Select ( '$Prompt', # to customize per-menu prompt '$Heading', # to customize per-menu heading '$Eof', # T/F for Eof detection # Variables must come first, then key/value options prompt => 'Enter number of choice:', # or 'whatever:' style => 'Bash', # or 'Korn' warnings => 1, # or 0 debug => 0, # or 1-9 logging => 0, # or 1 testmode => , # or 'make', or 'foreach' ); I The values shown for options are the defaults, except for C, which doesn't have one. =head2 Prompts There are two ways to customize the prompt used to solicit choices from C loop, or are content to use the same prompt for every loop. It allows a custom interactive prompt to be set in the B statement. The prompt string should not end in a whitespace character, because that doesn't look nice when the prompt is highlighted for display (usually in I). To offset the cursor from the prompt's end, I is inserted automatically after display highlighting has been turned off. If the environment variable C<$ENV{Shell_POSIX_Select_prompt}> is present, its value overrides the one in the B statement. The default prompt is "Enter number of choice:". To get the same prompt as provided by the Korn or Bash shell, use C<< prompt =>> Korn >> or C<< prompt => Bash >>. =head3 The $Prompt variable The programmer may also modify the prompt during execution, which may be desirable with nested loops that require different user instructions. This is accomplished by importing the $Prompt variable, and setting it to the desired prompt string before entering the loop. Note that imported variables have to be listed as the initial arguments to the C directive, and properly quoted. See L<"order.plx"> for an example. NOTE: If the program's input channel is not connected to a terminal, prompting is automatically disabled (since there's no point in soliciting input from a I!). =head2 $Heading The programmer has the option of binding a heading to each loop's menu, by importing C<$Heading> and setting it just before entering the associated loop. See L<"order.plx"> for an example. =head2 $Eof A common concern with the Shell's C loop. See L<"lc_filename.plx"> for a programming example. =head2 Styles The C