[Israel.pm] Getopt::Long usage question
Mikhael Goikhman
migo at homemail.com
Sun Nov 14 05:36:09 PST 2004
On 14 Nov 2004 14:03:32 +0200, Offer Kaye wrote:
>
> When using GetOptions, you can place options in seperate variables, or
> in a hash. Using your example, the way to use a hash as explained in
> the documentation is to pass a reference to a hash as the first
> parameter to GetOptions. There is then no need to manually place each
> option is the hash - it will automatically be populated:
> my %options;
> GetOptions(
> \%options,
> "help|h|?",
> "version",
> "output=s",
> "debug:s",
> "verbose+",
> "quiet",
> ) or die;
Ok, now I see what you mean. True, Getopt::Long supports such syntax too,
but I think it is mainly provided for users who are used to Getopt::Std,
this is not the Getopt::Long way. You may use such syntax though if you
add this code:
show_help() if $options{help};
show_version() if $options{version};
$options{verbose} = 0 if $options{quiet};
> But now I don't know how to use a subroutine to handle an option, like
> you did for "version" or "quiet":
>
> > "version" => \&show_version,
> > "quiet" => sub { $options{verbosity_level} = 0 },
>
> So is there a way to do this, or is your way (manually setting each
> option to a place in the hash) the only way to do this? I don't really
> like your workaround, because it means more typing... ;-)
As you see, if you insist on the first-hashref-parameter syntax, and add
the hash initialization lines plus the lines above that handle user
defined actions, you may actually get more typing.
Personally, I always like to initialize the option default values, and
explicitelly define what to do with every option, so I only use the
syntax posted in my parent message. I find it pretty elegant and short.
It also gives the way to use different names for variables and options.
Regards,
Mikhael.
--
perl -e 'print+chr(64+hex)for+split//,d9b815c07f9b8d1e'
More information about the Perl
mailing list