RICHARD FERNANDEZ wrote:
> Hi Folks,

Hello,

> I've written a little mytest.pl using Getopt::Std:
> 
> <script>
> #!/usr/bin/perl
> use warnings;
> use strict;
> use Getopt::Std;
> 
> getopt('hl');
> our ($opt_h, $opt_l);
> 
> my $hostname = $opt_h ? $opt_h : undef;
> my $file         = $opt_l ? $opt_l : undef;
> my @volgroups   = @ARGV;
> 
> 
> print "hostname = $hostname\n";
> print "file = $file\n";
> print "volgroups = ", join("\t", @volgroups), "\n";
> 
> </script>
> 
> When I run this, I get the following output:
> 
> $ ./mytest.pl -h localhost -l file foo bar            
> hostname = localhost
> file = file
> volgroups = foo bar
> 
> This is what I expect. If I then add "--volgroups" before the "foo",
> I get this:
> 
> $ ./mytest.pl -h localhost -l file --volgroups foo bar
> hostname = localhost
> file = groups
> volgroups = foo bar
> 
> I would have expected the second output to be the same as the first.
> Specifically, I can't see why file now equals "groups".
> 
> The man page says that:
> "To allow programs to process arguments that look like
>  switches, but aren't, both functions will stop processing
>  switches when they see the argument "--".  The "--" will
>  be removed from @ARGV."
> 
> Interestingly enough, if I say --volgroup instead of --volgroups, then
> the output says "file = group" instead of "file = groups"!
> 
> Can anyone shed some light?

Getopt::Std only works with single hyphen switches, the only exceptions being
'--', '--help' and '--version'.

perldoc Getopt::Std


Getopt::Std also processes swithes in clusters so "-abcd filename" is the same
as "-a -b -c -d filename".


With your command line:

./mytest.pl -h localhost -l file --volgroups foo bar

First the '-h' switch is processed and 'localhost' is assigned to $opt_h.
Next the '-l' switch is processed and 'file' is assigned to $opt_l.  Next the
cluster '--volgroups' is processed as the switches '-"-"', '-v', '-o' and '-l'
and since '-l' takes a value the remaining string 'groups' is assigned to
$opt_l overwriting the previous value in $opt_l.




John
-- 
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order.       -- Larry Wall

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to