×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Questions about Getopt::Long

Questions about Getopt::Long

Questions about Getopt::Long

(OP)
Please take a look at the piece of code below:

CODE

#!/usr/bin/perl

use strict;
use warnings;
use Getopt::Long;

my ($debug, $emailAddr, $fussy, $fussier);
GetOptions( # This is line 8
  "d" => \$debug,
  "e=s" => \$emailAddr,
  "fussy" => \$fussy,
  "fussier" => \$fussier,
) || die;

if($fussy) {
  print "\$fussy = $fussy\n";
}
if($fussier) {
  print "\$fussier = $fussier\n";
}
if($debug) {
  print "\$debug = $debug\n";
}
if($emailAddr) {
  print "\$emailAddr = $emailAddr\n";
}
print "$0 exit...\n";
exit; 

And some sample runs and my questions are list below in red:

CODE

% ./tt.pl -d
$debug = 1
./tt.pl exit...

% ./tt.pl -e
Option e requires an argument
Died at ./tt.pl line 8.

% ./tt.pl -e -d
$emailAddr = -d <-- This is a bug. How to fix it?
./tt.pl exit...

% ./tt.pl -fussy
$fussy = 1
./tt.pl exit...

./tt.pl -ff
Unknown option: ff
Died at ./tt.pl line 8.

% ./tt.pl -fussier
$fussier = 1
./tt.pl exit...

% ./tt.pl -fussi
$fussier = 1 <-- How can this happen? I'd expect to see the error: Unknown option: fussi
./tt.pl exit... 

Many thanks!!

RE: Questions about Getopt::Long

(OP)
It seems that GetOptions() does sort of wild cards expansion thing.

CODE

#!/usr/bin/perl

use strict;
use warnings; 
use Getopt::Long;

my ($ab, $ac, $xyz);
GetOptions(
  "ab" => \$ab,
  "ac" => \$ab,
  "xyz" => \$xyz,
) || die;

if($ab) {
  print "\$ab = $ab\n";
}
if($ac) {
  print "\$ac = $ac\n";
}
if($xyz) {
  print "\$xyz = $xyz\n";
}
print "$0 exit...\n";
exit; 

Test runs:

CODE

% ./tt.pl -a
Option a is ambiguous (ab, ac)
Died at ./tt.pl line 8.

% ./tt.pl -x
$xyz = 1
./tt.pl exit... 

Not sure if there is a way to disable GetOptions()'s wild cards expansion functionality?

RE: Questions about Getopt::Long

Did you read the perdocs on Getopt::Long? There's a lot in there, but the bit below was near the top.

In the "Getting Started with Getopt::Long" section, the approximately fourth paragraph reads:

Quote (perldoc Getopt::Long)

In the default configuration, options names may be abbreviated to uniqueness, case does not matter, and a single dash is sufficient, even for long option names. Also, options may be placed between non-option arguments. See "Configuring Getopt::Long" for more details on how to configure Getopt::Long.

The auto_abbrev option looks like it handles the specific behavior you were noticing.

RE: Questions about Getopt::Long

(OP)
To rharsh,

Thank you for pointing out the perldoc. I just read it:

Quote:


auto_abbrev
Allow option names to be abbreviated to uniqueness. Default is enabled unless environment variable POSIXLY_CORRECT has been set, in which case auto_abbrev is disabled.

But when I set the environment in the code, I don't see the difference:

$ENV{POSIXLY_CORRECT} = 1;

Am I setting the $ENV{POSIXLY_CORRECT} correctly syntax-wise? OMG, I feel so retarded.

CODE

#!/usr/bin/perl

use strict;
use warnings;
use Getopt::Long;

$ENV{POSIXLY_CORRECT} = 1;
my ($ab, $ac, $xyz);
GetOptions(
  "ab" => \$ab,
  "ac" => \$ab,
  "xyz" => \$xyz,
) || die;

if($ab) {
  print "\$ab = $ab\n";
}
if($ac) {
  print "\$ac = $ac\n";
}
if($xyz) {
  print "\$xyz = $xyz\n";
}
print "$0 exit...\n";
exit; 

Test runs:

CODE

% ./t1.pl -a
Option a is ambiguous (ab, ac)
Died at ./t1.pl line 9.

% ./t1.pl -x
$xyz = 1
./t1.pl exit... 

RE: Questions about Getopt::Long

This is probably going to be easier... change

CODE

use Getopt::Long; 
to this

CODE

use Getopt::Long qw(:config no_auto_abbrev); 

RE: Questions about Getopt::Long

(OP)
Thank you, rharsh. Your solution works very well.

Now, how to solve the problem below:

Sample Code:

CODE

#!/usr/bin/perl

use strict;
use warnings;
use Getopt::Long qw(:config no_auto_abbrev);

my ($email, $debug);
GetOptions(
  "d" => \$debug,
  "e=s" => \$email,
) || die;

if($email) {
  print "\$email = $email\n";
}
if($debug) {
  print "Debug mode\n";
}
print "$0 exit...\n";
exit; 

And test runs:

CODE

% ./t1.pl  -e
Option e requires an argument
Died at ./t1.pl line 8.

% ./t1.pl  -e -d
$email = -d //It'd be nice to post the same error message as the above.
./t1.pl exit... 

Thanks!

RE: Questions about Getopt::Long

If you're just going to be using single letter switches, I'd suggest using GetOpt::Std.

If you're going to stick with GetOpt::Long, the module is working like it should. Your 'e' option requires a string as an argument, it got one: '-d'. So -d isn't being read as a switch/option, but as an argument for -e.

You'll probably need to do some error checking to make sure whatever is in $email actually looks like an email address. Admittedly, I haven't used getopt::long all that much, but the behavior, while not what you're looking for, seems reasonable. You can enter references to code blocks rather than a scalar to validate the variable that's being read -- and that could be used to validate the email address and then store it. There is information about this in the perldoc.

Also, this post on perlmonks might be helpful.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close