What's the best way to test existence of arguments

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
pgodfrin [ Mo, 21 Januar 2008 18:11 ] [ ID #1913458 ]

Re: What's the best way to test existence of arguments

pgodfrin <pgodfrin [at] gmail.com> writes:

> 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.
Joost Diepenmaat [ Mo, 21 Januar 2008 18:24 ] [ ID #1913459 ]

Re: What's the best way to test existence of arguments

pgodfrin <pgodfrin [at] gmail.com> wrote:
>Is there a better way to test for existence of arguments besides
>$#ARGV ?

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

jue
jurgenex [ Mo, 21 Januar 2008 18:32 ] [ ID #1913460 ]

Re: What's the best way to test existence of arguments

On Jan 21, 11:32 am, Jürgen Exner <jurge... [at] hotmail.com> wrote:
> pgodfrin <pgodf... [at] gmail.com> wrote:
> >Is there a better way to test for existence of arguments besides
> >$#ARGV ?
>
> What about using [at] ARGV in scalar context?
> if (! [at] ARGV) {
> #I got no command line arguments
> }
>
> jue

doy - I'm a nitwit...
thanks,
pg
pgodfrin [ Mo, 21 Januar 2008 18:42 ] [ ID #1913462 ]

Re: What's the best way to test existence of arguments

On Mon, 21 Jan 2008 09:11:33 -0800 (PST), pgodfrin
<pgodfrin [at] gmail.com> wrote:

>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,

[at] ARGV


Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
Michele Dondi [ Mo, 21 Januar 2008 21:47 ] [ ID #1913470 ]

Re: What's the best way to test existence of arguments

Quoth pgodfrin <pgodfrin [at] gmail.com>:
>
> Is there a better way to test for existence of arguments besides
> $#ARGV ?

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

[at] 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
Ben Morrow [ Mo, 21 Januar 2008 21:32 ] [ ID #1913473 ]

Re: What's the best way to test existence of arguments

[A complimentary Cc of this posting was sent to
Joost Diepenmaat
<joost [at] zeekat.nl>], who wrote in article <87d4rvhz1t.fsf [at] zeekat.nl>:
> 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
Ilya Zakharevich [ Di, 22 Januar 2008 01:02 ] [ ID #1914053 ]
Perl » comp.lang.perl.misc » What's the best way to test existence of arguments

Vorheriges Thema: Looking for explanation
Nächstes Thema: FAQ 8.17 How can I measure time under a second?