What's the best way to test existence of arguments

P

pgodfrin

Greetings,

Is there a better way to test for existence of arguments besides
$#ARGV ? I have a little program that uses -s via the getopts module,
and if that option is used I want to force an argument. I suppose I
could use getopts(s:) - but I wondered if there is another "clean" way
to do that?

pg
 
J

Joost Diepenmaat

pgodfrin said:
Greetings,

Is there a better way to test for existence of arguments besides
$#ARGV ? I have a little program that uses -s via the getopts module,
and if that option is used I want to force an argument. I suppose I
could use getopts(s:) - but I wondered if there is another "clean" way
to do that?

GetOpt::Long should give you all the control you'd need, including
default values, types and real named options and readable variable
names. It's a bit more verbose than GetOpt, but IMO it's also more
readable and definitely more flexible.

Joost.
 
J

Jürgen Exner

pgodfrin said:
Is there a better way to test for existence of arguments besides
$#ARGV ?

What about using @ARGV in scalar context?
if (! @ARGV) {
#I got no command line arguments
}

jue
 
B

Ben Morrow

Quoth pgodfrin said:
Is there a better way to test for existence of arguments besides
$#ARGV ?

I would use @ARGV in scalar context rather than $#ARGV, but what's wrong
with that?

@ARGV or die "You need an argument!\n";

seems pretty clear to me.
I have a little program that uses -s via the getopts module,
and if that option is used I want to force an argument. I suppose I
could use getopts(s:)

....or indeed that?

Ben
 
M

Michele Dondi

Is there a better way to test for existence of arguments besides
$#ARGV ? I have a little program that uses -s via the getopts module,

@ARGV


Michele
 
I

Ilya Zakharevich

[A complimentary Cc of this posting was sent to
Joost Diepenmaat
GetOpt::Long should give you all the control you'd need, including
default values, types and real named options and readable variable
names. It's a bit more verbose than GetOpt, but IMO it's also more
readable and definitely more flexible.

Do not know as now, but a couple of years ago ::Long had some
limitations which ::Std did not. (I started a rewrite, but did not
finish it.)

Hope this helps,
Ilya
 

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,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top