[Israel.pm] Getopt::Long usage question

Offer Kaye offer.kaye at gmail.com
Sun Nov 14 04:03:32 PST 2004


On Sun, 14 Nov 2004 10:41:51 +0000, Mikhael Goikhman wrote:
> On 14 Nov 2004 11:33:38 +0200, Offer Kaye wrote:
> >
> > How can I use a user-defined subroutine to handle an option, when
> > stroring the options in a hash?
> 
> This question seems to be incomplete, it is hard to understand what
> exactly do you mean without an example. 

Sorry. I will explain.
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;

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... ;-)

-- 
Offer Kaye



More information about the Perl mailing list