Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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

In reply to (jeffa) Re: Ksh by jeffa
in thread Ksh style select menus in perl by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-19 14:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found