Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Check to See if there is Values in Getopt::Long

by Anonymous Monk
on Aug 02, 2011 at 21:46 UTC ( #918141=perlquestion: print w/replies, xml ) Need Help??

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

Here's a snippet of my code.
#!/usr/bin/perl use strict; use Getopt::Long; GetOptions( 'a=s' => \my $opt_a, 'b=s' => \my $opt_b, ... 'z=s' => \my $opt_z, );
I have about 26 options and I would want all of the options checked if the user has entered something in the option. I am hoping to do this in a for-loop statement so that it would look cleaner. Something like this.
for my $i (<loop through all the options>) { die if ( ! $i ); }
Any suggestions?

Replies are listed 'Best First'.
Re: Check to See if there is Values in Getopt::Long
by ikegami (Pope) on Aug 02, 2011 at 21:58 UTC

    Sounds like a horrible interface!

    Anyway, if they're all strings,

    my @opts = qw( a b ... z ); GetOptions( \my %opts, ( map { "$_=s" } @opts ), ) or die("...\n"); for my $opt (@opts) { die("...\n") if !exists($opts{$opt}); }

    If they're not,

    my %opt_defs = ( ( map { $_ => "$_=s" } qw( a b ... ) ), ( map { $_ => "$_=i" } qw( i j ... ) ), ); GetOptions( \my %opts, values(%opt_defs), ) or die("...\n"); for my $opt (keys(%opt_defs)) { die("...\n") if !exists($opts{$opt}); }
Re: Check to See if there is Values in Getopt::Long
by pileofrogs (Priest) on Aug 03, 2011 at 00:10 UTC

    First of all, anything ikegami says is smarter than anything I've ever said.

    You're going to die if the user doesn't enter every letter in the alphabet as an option? Seriously?

    Maybe you want to read in a line from stdin with all the values or put the values in a file or something like that? Maybe a comma-separated-value (CSV) file? There are a zillion modules out there to parse CSV files. (see CSV for some of them)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://918141]
Approved by ikegami
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2020-07-04 12:30 GMT
Find Nodes?
    Voting Booth?

    No recent polls found