Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

(jeffa) Re: Ksh

by jeffa (Bishop)
on Aug 01, 2002 at 20:48 UTC ( [id://186931]=note: print w/replies, xml ) Need Help??


in reply to Ksh style select menus in perl

I really don't know. I am sure that a CPAN module could be built that would offer the same functionality, but the interface would most likely be different (OOP based?). To elaborate on what select does, here is some code for those interested to play with:
#!/usr/bin/ksh PS3="Enter your choice :" select menu_list in English francais do case $menu_list in English) print "Thank you";; francais) print "Merci";; *) print "???"; break;; esac done

That is pretty slick. :) Here are some guidelines from the Kornshell '93 manual to go by for anyone wishing to do a little porting:

select vname in word  . . .  ] ;do list  ;done
    A select command prints on standard error (file descriptor 2) the set of words, each preceded by a number. If in word  <NULL>. . . is omitted, then the positional parameters starting from 1 are used instead. The PS3 prompt is printed and a line is read from the standard input. If this line consists of the number of one of the listed words, then the value of the variable vname  is set to the word  corresponding to this number. If this line is empty, the selection list is printed again. Otherwise the value of the variable vname  is set to null. The contents of the line read from standard input is saved in the variable REPLY. The list  is executed for each selection until a break  or end-of-file  is encountered. If the REPLY variable is set to null  by the execution of list, then the selection list is printed before displaying the PS3 prompt for the next selection.

UPDATE:
Here is my go at it - pure evil:

no strict; use constant PS3 => 'Enter your choice :'; my %menu = ( English => sub { print "Thank you\n" }, fancais => sub { print "Merci\n" }, none => sub { print "???\n"; exit }, ); while (1) { &select(menu_list => in => qw(English fancais)); $menu{$menu_list}->(); } sub select { my ($var,$in,@list) = @_; unless ($i) { printf STDERR "%d) %s\n", ++$i, $_ for @list; } push @list,undef; print STDERR PS3; chomp($ans = <>); unless ($ans) { $i = pop @list; &select($var,undef,@list); } $$var = $list[$ans-1] || 'none'; }

Yes, i am actually doing a Bad Thing and turning off strict. Why? Because i wanted to use menu_list as symbolic var - not really a good thing, but it remains close to the syntax of ksh's select. I opted to use a hash (%menu) instead of a case - much nicer. pushing an undef value onto @list inside select() is a trick to handle the user select anything other than a positive integer. Also, you must prefix the call to select() with an ampersand, else Perl will execute the built-in select. I almost got the REPLY being null behavior to work - see if you can find the bug. ;)

I don't recommend using this code, this is just for fun. :)

jeffa

Hadn't touched Kornshell since 1996

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://186931]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (4)
As of 2024-04-16 10:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found