Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Getopt::Long question

by Cody Pendant (Prior)
on Mar 02, 2009 at 23:55 UTC ( #747578=perlquestion: print w/replies, xml ) Need Help??

Cody Pendant has asked for the wisdom of the Perl Monks concerning the following question:

I'm using Getopt::Long, like this:

my($dir, $year, $start, $delay); GetOptions ( 'dir=s' => \$dir, 'year=i' => \$year, 'start:i' => \$start, 'delay:i' => \$delay );

And when I call my script like this:

scriptname --dir foo --year 2007 --delay 30

everything is fine.

When I call it like this, with the options in a different order, it dies:

scriptname --dir foo --delay 30 --year 2007 Option year requires an argument

But if I add an equals sign, it's OK:

scriptname --dir foo --delay 30 --year=2007

I can't figure out what's going on by referring to the module's documentation on CPAN. TIA.

Nobody says perl looks like line-noise any more
kids today don't know what line-noise IS ...

Replies are listed 'Best First'.
Re: Getopt::Long question
by Tanktalus (Canon) on Mar 03, 2009 at 00:02 UTC

    You may want to look at your version of Getopt::Long. I apparently have 2.35 installed, and do not have the problem you're mentioning.

Re: Getopt::Long question
by graff (Chancellor) on Mar 03, 2009 at 03:55 UTC
    When you used this ordering of args on your command line:
    scriptname --dir foo --year 2007 --delay 30
    did you confirm that the "$delay" variable actually got the value "30"?

    I'm guessing it might not have, because there might be something else happening to @ARGV before you make the call to GetOptions() -- like maybe something pops off the last arg?

      You're right! I see what's gone wrong now, and I feel appropriately stupid. It wasn't actually "scriptname args" that I was running, it was "scriptalias args" where scriptalias is a doskey alias to the script, which didn't pass on all the args. Mea Culpa.

      Nobody says perl looks like line-noise any more
      kids today don't know what line-noise IS ...

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://747578]
Approved by Tanktalus
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2021-02-28 12:56 GMT
Find Nodes?
    Voting Booth?

    No recent polls found