Getopts::std
am 01.08.2006 22:56:13 von lnatz
I am using various switches in my script. I have the Getopts::std
module and I wrote the following line:
getopts('F:S:D:T:I:plen', \%OPTS);
How can I see if the switch, such as -F, was followed by an argument?
What I mean is that if in the prompt I write:
./example.pl -F
How can I stop the script and check if -F was actually followed by an
argument and what that argument is?
Re: Getopts::std
am 01.08.2006 23:17:45 von Paul Lalli
lnatz wrote:
> I am using various switches in my script. I have the Getopts::std
> module
This is the second time you've posted a message about a non-existing
module. I can only *assume* you meant the module Getopt::Std, and its
getopts() function. Spelling and capitalization both count. Please put
some care into your posts.
> and I wrote the following line:
> getopts('F:S:D:T:I:plen', \%OPTS);
>
> How can I see if the switch, such as -F, was followed by an argument?
> What I mean is that if in the prompt I write:
> ./example.pl -F
> How can I stop the script and check if -F was actually followed by an
> argument and what that argument is?
This is such an absurdly easy thing to check, I have to ask why you
didn't. I've never used this module in my life, but it took me all of
5 minutes to find it on CPAN (including account for your misspellings),
read the documenation enough to see this:
"For those of you who don't like additional global variables being
created, getopt() and getopts() will also accept a hash reference as an
optional second argument. Hash keys will be x (where x is the switch
name) with key values the value of the argument or 1 if no argument is
specified."
and then run a small test case to verify:
$ perl -MData::Dumper -MGetopt::Std -e'getopts(q{oif:}, \%opts); pri
nt Dumper \%opts' -- -o -f
$VAR1 = {
'f' => undef,
'o' => 1
};
It seems that options which take a value but are not provided a value
get the undefined value. Why didn't you put this much effort into your
own post?
Paul Lalli