sub ksh_select(&@) {
my $cr = shift;
my $prompt = $ENV{'PS3'} || '#? ';
local *ARGV;
local $| = 1;
local $_;
while (1) {
for my $i ( 0 .. $#_ ) {
print STDOUT $i+1, ") $_[$i]\n";
}
print STDOUT $prompt;
$_ = <>;
defined $_ or return;
chomp;
$cr->( $_ );
}
}
####
ksh_select { print "You chose $_\n" } qw( foo bar quux );
##
##
# in this example, the user has choices to navigate around some structure.
my %dispatch = (
First => \&goto_first,
Prev => \&goto_prev,
Next => \&goto_next,
Last => \&goto_last,
);
my @menu = qw( First Prev Next Last );
ksh_select {
defined $menu[$_]
? $dispatch{$menu[$_]}->()
: warn "Selection out of range!\n";
} @menu;