shobhit has asked for the wisdom of the Perl Monks concerning the following question:

Hi, In ksh we have a very useful command to build nice clean menu itmes.
The below ksh commands produce a menu where you can choose one of the listed options.
If you choose outside the menu or do not choose at all, then the select list is printed again.
My question is, do we have something to easily implement this in Perl?
I cannot use any GUI so Tk and others are ruled out. Need a command line solution
Something like:
select choice in "Option 1" "option 2"; do ... ... done

Replies are listed 'Best First'.
Re: ksh like menu in perl.
by cdarke (Prior) on Apr 12, 2007 at 12:20 UTC
Re: ksh like menu in perl.
by Moron (Curate) on Apr 12, 2007 at 12:42 UTC
    ^P It shouldn't be too difficult to handroll this one (update: although now seeing it, I prefer the cdarke solution above actually).

    ^C something like (untested):

    # cls => 1 # option to clear tty screen # opt => [ letter, text, coderef, ... ] # menu choices # to make a code ref assign sub { your-code } to the array element # in this way assign handling code to each choice letter sub GetChoice { my %opt = @_ or die; # using hash-style parameters $opt{ cls } ||= 0 and system 'cls'; my $maxchoice = $#{ $opt{ opt } } - 2; my %coderef = {}; my $done = 0; do { for ( my $choice = 0; $choice <= $maxchoice; $choice += 3 ) { ( ref( $opt{ opt }[ $choice + 2 ] eq 'CODE' ) or die "not a code reference"; $coderef{ $opt{ opt }[ $choice ] } = $opt{ opt }[ $choice + 2 +]; my $text = $opt{ opt }[ $choice + 1 ]; chomp $text; $text .= "\n\n"; print $opt{ opt }[ $choice ] . "\t$text"; } print "Enter option code: "; my $input = <>; if ( $input ~= /^(\w)/ ) { if ( defined( $coderef { $1 } ) ) { $coderef -> { $1 }(); $done = 1; } } unless( $done ) { $opt { cls } && system( 'cls' ); warn "Invalid option code $input"; } } until ( $done ); }
    __________________________________________________________________________________

    ^M Free your mind!

    Key to hats: ^I=white ^B=black ^P=yellow ^E=red ^C=green ^M=blue - see Moron's scratchpad for fuller explanation.

Re: ksh like menu in perl.
by shobhit (Sexton) on Apr 12, 2007 at 14:21 UTC
    Thanks for your response everyone. Unfortunately I do not have permission to install the new module you mention. However, immediately after i posed this question. I googl'd for it, and found the answer in another post. http://www.perlmonks.org/index.pl?node_id=186913 The select.pl mentioned there, works for me :-). regards, shobhit

      If that script does the trick, more power to you, but you shouldn't limit yourself from using CPAN modules just because you don't have administrative access to install the modules formally. You can always just install them locally in your own directory and:

      use lib '/my/script/directory'; use That::CPAN::Module;
      Here's the trick to the manual install, although I usually just run make and copy the files from the blib/lib directory created.
      perl -e 'split//,q{john hurl, pest caretaker}and(map{print @_[$_]}(joi +n(q{},map{sprintf(qq{%010u},$_)}(2**2*307*4993,5*101*641*5261,7*59*79 +*36997,13*17*71*45131,3**2*67*89*167*181))=~/\d{2}/g));'