Getopt::Std problem

S

S

Hi,

I'm using Getopt::Std in a script that I'm writing. While testing
boundary conditions, I notice some strange behavior. Here's the code
snippet in question:

use vars qw /%option/;
getopts('avs:m', \%option) or die Usage();
die "You did not enter any options - run $0 -h to view options\n" if !
%option;

Now, if I run my script with the -s option, but no argument, it dies as
expected, except for the following:


1 at ./make-acct-ctl.pl line 14.

Can anyone help to explain/correct this?
 
W

Walter Roberson

:I'm using Getopt::Std in a script that I'm writing. While testing
:boundary conditions, I notice some strange behavior. Here's the code
:snippet in question:

:use vars qw /%option/;
:getopts('avs:m', \%option) or die Usage();
:die "You did not enter any options - run $0 -h to view options\n" if !
:%option;

:Now, if I run my script with the -s option, but no argument, it dies as
:expected, except for the following:

:1 at ./make-acct-ctl.pl line 14.

:Can anyone help to explain/correct this?

What exactly does Usage() compute? Is it possible that Usage() is
returning the 1 ? It should return a list, but perhaps along the
way you have an accidental scalar context that is converting a
list containing a single string into scalar(@thatlist) which
would be 1 ?
 
S

S

Walter said:
:I'm using Getopt::Std in a script that I'm writing. While testing
:boundary conditions, I notice some strange behavior. Here's the code
:snippet in question:

:use vars qw /%option/;
:getopts('avs:m', \%option) or die Usage();
:die "You did not enter any options - run $0 -h to view options\n" if !
:%option;

:Now, if I run my script with the -s option, but no argument, it dies as
:expected, except for the following:

:1 at ./make-acct-ctl.pl line 14.

:Can anyone help to explain/correct this?

What exactly does Usage() compute? Is it possible that Usage() is
returning the 1 ? It should return a list, but perhaps along the
way you have an accidental scalar context that is converting a
list containing a single string into scalar(@thatlist) which
would be 1 ?
sub Usage
{
print <<HELP;

$0 -s <server> | -v | -a | -m

HELP
}
 
B

Ben Morrow

S said:
sub Usage
{
print <<HELP;

$0 -s <server> | -v | -a | -m

HELP
}

Well, there you go then: print() returns 1 if the print succeeds. You
probably meant

return <<HELP;
....

Ben
 
U

Uri Guttman

S> sub Usage
S> {
S> print <<HELP;

S> $0 -s <server> | -v | -a | -m

S> HELP
S> }

see my comments in another current thread on how to do a better usage
sub. the usage sub should do the die/exit itself. and it should take an
error string argument. this makes usage calls much cleaner in the main code.

uri
 

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