Click here to get back home

Is this bug in Getopt::Std?

 HomeNewsGroups | Search | About
 comp.lang.perl.modules    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Is this bug in Getopt::Std? Hemant Shah 10-20-2006
Posted by Hemant Shah on October 20, 2006, 3:00 pm
Please log in for more thread options


Folks,

Here is the sample perl script:


#!/usr/bin/perl

use Getopt::Std;

my %Options;

getopts('C:', \%Options);

print "Comment = $Options\n";
print "ARGV[0] = $ARGV[0]\n" if ($ARGV[0]);




When I run the script as follows:

./tstgetopts.pl -a"Comment"
Unknown option: a
Comment = omment



Why does getopts set value for $Options?
It should be a left in %ARGV.

If I use space in between then it works as expected.

./tstgetopts.pl -a "Comment"
Unknown option: a
Comment =
ARGV[0] = Comment


Is this a bug or am I missing something?




--
Hemant Shah /"\ ASCII ribbon campaign
E-mail: NoJunkMailshah@xnet.com \ / ---------------------
X against HTML mail
TO REPLY, REMOVE NoJunkMail / \ and postings
FROM MY E-MAIL ADDRESS.
-----------------[DO NOT SEND UNSOLICITED BULK E-MAIL]------------------
I haven't lost my mind, Above opinions are mine only.
it's backed up on tape somewhere. Others can have their own.

Posted by Paul Lalli on October 20, 2006, 3:59 pm
Please log in for more thread options


Hemant Shah wrote:
> Here is the sample perl script:
>
>
> #!/usr/bin/perl
>
> use Getopt::Std;
>
> my %Options;
>
> getopts('C:', \%Options);
>
> print "Comment = $Options\n";
> print "ARGV[0] = $ARGV[0]\n" if ($ARGV[0]);

>
> When I run the script as follows:
>
> ./tstgetopts.pl -a"Comment"
> Unknown option: a
> Comment = omment
>
> Why does getopts set value for $Options?
> It should be a left in %ARGV.
>
> If I use space in between then it works as expected.
>
> ./tstgetopts.pl -a "Comment"
> Unknown option: a
> Comment =
> ARGV[0] = Comment
>
> Is this a bug or am I missing something?

You're missing something.

Quotes are not a shell argument separator. This has nothing to do with
Perl, btw. Saying
./tstgetopts.pl -a"Comment"
is exactly the same as
./tstgetopts.pl -aComment

Getopts assumes that one switch is a cluster of single switches, until
it gets to C, which takes a value....

Take a look at this example, and see if that helps you understand:
perl -le'print for @ARGV' foo bar foo "bar" foo"bar"
foo
bar
foo
bar
foobar


Hope this helps,
Paul Lalli


Posted by Joe Smith on October 21, 2006, 5:38 am
Please log in for more thread options


Hemant Shah wrote:

> getopts('C:', \%Options);

The example given in 'perldoc Getopt::Std' has
getopts('oif:'); # -o & -i are boolean flags, -f takes an argument

That means it would accept
        -o -i -fFile
and
        -oi -f File
and
        -oifFile
since getopts allows option bundling. Individual option letters can
be specified each with its own hyphen, or several of the letters
can be bunched up together behind a single hyphen.

> ./tstgetopts.pl -a"Comment"
> Unknown option: a
> Comment = omment

Those double quotes make no difference when specified to a Unix
shell. What you typed is the same as
./tstgetopts.pl -aComment
and is interpreted as
./tstgetopts.pl -a -Comment
which has an unknown option "-a" and "-C" with argument "omment".

> Is this a bug or am I missing something?

I'd say it is a problem with the documentation.
        -Joe

Posted by Dr.Ruud on October 21, 2006, 7:52 am
Please log in for more thread options


Joe Smith schreef:

> ./tstgetopts.pl -aComment
> and is interpreted as
> ./tstgetopts.pl -a -Comment

And further as "./tstgetopts.pl -a -C -o -m -m -e -n -t":

#!/usr/bin/perl
use strict ;
use warnings ;

use Getopt::Std ;

my %opts ;

{ local ($\, $", $,) = ("\n", '', "\t") ;

getopts("@{['a'..'z', 'A'..'Z']}", \%opts) ;

print $_, $opts for (sort keys %opts) ;
}
__END__

$ perl test.pl -a"Comments"
C 1
a 1
e 1
m 1
n 1
o 1
s 1
t 1

$ perl test.pl -a "Comments"
a 1

--
Affijn, Ruud

"Gewoon is een tijger."

Posted by anno4000 on October 22, 2006, 6:06 am
Please log in for more thread options


> Folks,
>
> Here is the sample perl script:
>
>
> #!/usr/bin/perl
>
> use Getopt::Std;
>
> my %Options;
>
> getopts('C:', \%Options);
>
> print "Comment = $Options\n";
> print "ARGV[0] = $ARGV[0]\n" if ($ARGV[0]);
>
>
>
>
> When I run the script as follows:
>
> ./tstgetopts.pl -a"Comment"
> Unknown option: a
> Comment = omment
>
>
>
> Why does getopts set value for $Options?
> It should be a left in %ARGV.
>
> If I use space in between then it works as expected.
>
> ./tstgetopts.pl -a "Comment"
> Unknown option: a
> Comment =
> ARGV[0] = Comment
>
>
> Is this a bug or am I missing something?

What version of Getopt::Std are you using? 1.05 works as expected:

Unknown option: a
Use of uninitialized value in concatenation (.) or string at ./ttt line 10.
Comment =

Anno

Similar ThreadsPosted
Getopt::Simple V 1.47 July 29, 2004, 7:18 am
Getopt::Std question September 7, 2005, 9:42 am
[Fwd: Re: What happened to Getopt::Std ?] June 13, 2007, 6:34 am
Version? of getopt::Long October 4, 2004, 2:48 pm
Announce: Getopt::Simple V 1.48 November 16, 2004, 10:29 am
Getopt::Long install problems October 4, 2004, 6:33 pm
Parsing function args in Getopt::Long style January 11, 2008, 4:11 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap