Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

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
[Corion]: choroba: Yeah - I basically have the same for regenerating README and README.mkdown already, except that I do that in Makefile.PL, but I guess one or the other thing should somehow work ;)
[ambrus]: was this bug: https://rt.cpan. org/Public/Bug/ Display.html?id= 59814
[Corion]: ambrus: Oh - that one would be much harder to automate... The SYNOPSIS section should mostly be a runnable program IMO, but I write only small snippets in my documentation for single functions/methods, and creating the appropriate environment for ...
[Corion]: ... those in an automated fashion seems somewhat hard to me. Although it should do wonders for the test coverage ;)
[haukex]: Corion: I once wrote an automated thingy for that here

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (10)
As of 2017-02-27 12:16 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (384 votes). Check out past polls.