FAQ
Hi folks

I have been using shell script for my admin work and recently decided to use
perl fo all my automation. So proud I did that.

Quick question: I have been using "Select" function in shell to present
menu to users. Do we have a "select" equivalent in perl? Or if I have a list
of names in an array what is the best way to present them in a menu and
prompt the user to select one from the list?

Thanks

Mr. B

Search Discussions

  • Jim Gibson at Apr 7, 2011 at 6:26 pm
    On 4/7/11 Thu Apr 7, 2011 10:48 AM, "Balaji krishnan"
    <balaji.r.krishnan@gmail.com> scribbled:
    Hi folks

    I have been using shell script for my admin work and recently decided to use
    perl fo all my automation. So proud I did that.

    Quick question: I have been using "Select" function in shell to present
    menu to users. Do we have a "select" equivalent in perl? Or if I have a list
    of names in an array what is the best way to present them in a menu and
    prompt the user to select one from the list?
    I use the print statement to present the list of items with a number, then
    print a prompt (without a newline). Then use statements of the type

    my $ans = <STDIN>
    chomp($ans);

    to get a response from the user. You can then test that response and act
    accordingly. Simplest approach is to number your choices and get a number
    from the user. A blank line or zero can be do nothing (it's always a good
    idea to have a no operation choice).
  • Tim Maher at Apr 9, 2011 at 6:42 pm

    On Thu, Apr 07, 2011 at 11:26:10AM -0700, Jim Gibson wrote:
    On 4/7/11 Thu Apr 7, 2011 10:48 AM, "Balaji krishnan"
    <balaji.r.krishnan@gmail.com> scribbled:
    Hi folks

    I have been using shell script for my admin work and recently decided to use
    perl fo all my automation. So proud I did that.

    Quick question: I have been using "Select" function in shell to present
    menu to users. Do we have a "select" equivalent in perl?
    Yes we do!

    I uploaded a version to CPAN many years ago, which I (for one) use all
    the time.

    Nowadays the CPAN installation doesn't work on some systems, so I recommend
    installing the module (called Shell::POSIX::Select) from the Debian repository,
    from the package called "libshell-posix-select-perl".

    Tim
    *----------------------------------------------------------------------*
    Tim Maher, PhD (206) 781-UNIX http://www.consultix-inc.com |
    tim at ( TeachMePerl, TeachMeLinux, or TeachMeUnix ) dot Com |
    Intermediate Perl: 4/19-4/21 UNIX/Linux Fundamentals: 5/9-5/12 |
    *-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
    "Minimal Perl for UNIX People" has been an Amazon Best Seller! < |
    * Download chapters, read reviews, and order at: MinimalPerl.com * |
    *----------------------------------------------------------------------*
  • Alan Haggai Alavi at Apr 7, 2011 at 6:58 pm
    Hello Balaji,
    I have been using shell script for my admin work and recently decided to use
    perl fo all my automation. So proud I did that.
    Nice to know that you have decided to use Perl.
    Quick question: I have been using "Select" function in shell to present
    menu to users. Do we have a "select" equivalent in perl? Or if I have a list
    of names in an array what is the best way to present them in a menu and
    prompt the user to select one from the list?
    The task is very easy to implement using IO::Prompt:

    =pod code

    use IO::Prompt;

    my @names = qw( foo bar baz qux );
    my $selected_name = prompt 'Please select one name: ', -menu => \@names;

    print "Selected name: $selected_name\n";

    =cut code

    Regards,
    Alan Haggai Alavi.
    --
    The difference makes the difference

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedApr 7, '11 at 5:48p
activeApr 9, '11 at 6:42p
posts4
users4
websiteperl.org

People

Translate

site design / logo © 2021 Grokbase