Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^3: getopts and -?

by kejohm (Hermit)
on May 10, 2011 at 02:28 UTC ( #903890=note: print w/ replies, xml ) Need Help??


in reply to Re^2: getopts and -?
in thread getopts and -?

The parameters passed to the Getopt::Long::GetOptions() function are different to the Getopt::Std::getopts() function. One way to call GetOptions() is to pass a hash with the keys specifying each option, and the values specifying a reference to a variable that will be set. Here is an example based on your code:

our ( $opt_d,$opt_h,$opt_l,$opt_L,$opt_i,$opt_o,$opt_P,$opt_p, $opt_q,$opt_r,$opt_v,$opt_c,$opt_help ); GetOptions( 'd' => \$opt_d, 'h' => \$opt_h, 'l' => \$opt_l, 'L' => \$opt_L, 'i' => \$opt_i, 'o=s' => \$opt_s, 'P=s' => \$opt_P, 'p' => \$opt_p, 'q' => \$opt_q, 'r' => \$opt_r, 'v=s' => \$opt_v, 'c' => \$opt_c, 'help|?' => \$opt_help, );

Since your code has a lot of options, you may want to look at storing the options in a hash, rather than individual variables. Check out the Getopt::Long manpage for more info.


Comment on Re^3: getopts and -?
Select or Download Code
Replies are listed 'Best First'.
Re^4: getopts and -?
by JavaFan (Canon) on May 10, 2011 at 11:12 UTC
    Why package variables? my works as well, and you can even write:
    GetOptions( d => \my $opt_d, h => \my $opt_h, l => \my $opt_l, ... ) or die "Failed to parse options";

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://903890]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (7)
As of 2015-07-28 07:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (252 votes), past polls