Select equivalent in perl

--e0cb4e8873e7652fea04a057b781
Content-Type: text/plain; charset=ISO-8859-1

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

--e0cb4e8873e7652fea04a057b781--
Balaji krishnan [ Do, 07 April 2011 19:48 ] [ ID #2057785 ]

Re: Select equivalent in perl

On 4/7/11 Thu Apr 7, 2011 10:48 AM, "Balaji krishnan"
<balaji.r.krishnan [at] 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).



--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Jim Gibson [ Do, 07 April 2011 20:26 ] [ ID #2057786 ]

Re: Select equivalent in perl

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 [at] names = qw( foo bar baz qux );
my $selected_name = prompt 'Please select one name: ', -menu => \ [at] names;

print "Selected name: $selected_name\n";

=cut code

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

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Alan Haggai Alavi [ Do, 07 April 2011 20:57 ] [ ID #2057787 ]

Re: Select equivalent in perl

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 [at] 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 * |
*----------------------------------------------------------- -----------*

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Tim Maher [ Sa, 09 April 2011 20:42 ] [ ID #2057868 ]
Perl » gmane.comp.lang.perl.beginners » Select equivalent in perl

Vorheriges Thema: regular expression for email id and IP address
Nächstes Thema: REGEX Explanation