Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Another Getopt::Long questions

by dragonchild (Archbishop)
on Apr 09, 2003 at 23:21 UTC ( [id://249471]=perlquestion: print w/replies, xml ) Need Help??

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

I've got the following call:
my %options; GetOptions( \%options, 'length|height=i', );
As that stands, the option will be put into either $options{length} or $options{height}. Is there some way I can force Getopt::Long to always put it into $options{length}?

(And, yes, I've RTFM'ed. It talks about synonyms and it talks about hashrefs, but it doesn't talk about synonyms and hashrefs together.)

------
We are the carpenters and bricklayers of the Information Age.

Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Replies are listed 'Best First'.
Re: Another Getopt::Long questions
by Enlil (Parson) on Apr 09, 2003 at 23:46 UTC
    I believe your code does what you want. Using the following code:
    use strict; use warnings; use Getopt::Long; use Data::Dumper; my %options; GetOptions( \%options, 'length|height=i', ); print Dumper \%options;
    I get the following:
    E:\>go.pl --height=1 $VAR1 = { 'length' => 1 }; E:\>go.pl --length=1 $VAR1 = { 'length' => 1 };

    -enlil

      I don't remember if it's mentioned specifically in the POD, but from my usage, the first option is always the name of the key it will use. This is handy when you want to use one character options or bundling. As in:

      use Getopt::Long; Getopt::Long::Configure('bundling'); my %options; GetOptions( \%options, 'LongName|L=s' ); if ($options{LongName}) { # do something with $options{LongName} }
      And the user can specify that option either as:
      prog.pl --longname something
      or
      prog.pl -L something

      kelan


      Perl6 Grammar Student

Re: Another Getopt::Long questions
by Ovid (Cardinal) on Apr 09, 2003 at 23:46 UTC

    Update: Hmmm... I guess was thinking about Getopt::Standard. enlil's answer appears correct and my reference below is superfluous.

    Try this:

    #!/usr/bin/perl -w use strict; use Getopt::Long; use Data::Dumper; my %options; GetOptions( \%options, 'length|height=i' => \$options{length} ); print Dumper \%options;

    Cheers,
    Ovid

    New address of my CGI Course.
    Silence is Evil (feel free to copy and distribute widely - note copyright text)

Re: Another Getopt::Long questions
by dragonchild (Archbishop) on Apr 10, 2003 at 14:12 UTC
    *blushes* Never post a question after 9.5 hours after two 12 hour days. Always post it in the morning. It's amazing what changes ... "I don't know why, but it works now!" is a really embarassing thing to say!

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://249471]
Approved by Paladin
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-04-18 03:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found