Getopt::Std argument list mistake?

D

Davy

Hi all,

I have write a small program to test Getopt::Std.

I found when I enterit work OK: output: minus_result is -50

But when I enterIt output the help message?

Why -v must be write at the end of the argument list??

#-------------------------
use warnings;
use strict;
use Getopt::Std;

my $help = <<"EOH";
--------------------------------------------
$0: first_vector minus second_vector

Options:
-a first_vector
-b second_vector
-h print help
-v verbose
--------------------------------------------
EOH

# set out command-line options,
# requirements, and defaults.
my %options;
getopt("a:b:hv", \%options);

die $help if exists $options{h};
die $help unless $options{a};
die $help unless $options{b};

my $first_vector = $options{a};
my $second_vector = $options{b};

my $minus_result = $first_vector - $second_vector;

if (exists $options{v}) {
print "minus_result is $minus_result\n";
}
else {
print "$minus_result\n";
}
#-------------------------------------

Thanks!
Davy
 
D

Davy

Davy said:
Hi all,

I have write a small program to test Getopt::Std.

I found when I enter
it work OK: output: minus_result is -50

But when I enter
It output the help message?

Why -v must be write at the end of the argument list??

#-------------------------
use warnings;
use strict;
use Getopt::Std;

my $help = <<"EOH";
--------------------------------------------
$0: first_vector minus second_vector

Options:
-a first_vector
-b second_vector
-h print help
-v verbose
--------------------------------------------
EOH

# set out command-line options,
# requirements, and defaults.
my %options;
getopt("a:b:hv", \%options);
[snip]

Hi all,

I change getopt() to getopts() and all OK;

Thanks!
Davy
 
P

Paul Lalli

Davy said:
getopt("a:b:hv", \%options);

You are confusing getopt() with getopts(). getopt() takes a list of
options that take values. Colons don't mean anything special.
getopts() takes a list of options, and those followed by colons take a
value. So when you put -v before -b, it assumed that -v's value was
'-b'.

Paul Lalli
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top