[Israel.pm] Getopt::Long usage question

Mikhael Goikhman migo at homemail.com
Sun Nov 14 02:41:51 PST 2004


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. Anyway, I suggest to always
explicitly define what to do with every option, so there are no
surprises if someone passes some unusual options. I.e.:

	# set default option values
	my $verbosity_level = 1;
	my $output_file = "-";
	my $debug = 0;

	GetOptions(
		"help|h|?" => \&show_help,
		"version"  => \&show_version,
		"output=s" => \$output_file,
		"debug:s"  => \$debug,
		"verbose+" => \$verbosity_level,
		"quiet"    => sub { verbosity_level = 0 },
	) or show_help();

Of course, you may place all option values in the hash if you need to
pass them somewhere. I.e. here is the same thing, just using the hash:

	# set default option values
	my %options = (
		verbosity_level => 1,
		output_file => "-",
		debug => 0,
	)l

	GetOptions(
		"help|h|?" => \&show_help,
		"version"  => \&show_version,
		"output=s" => \$options{output_file},
		"debug:s"  => \$options{debug},
		"verbose+" => \$options{verbosity_level},
		"quiet"    => sub { options{verbosity_level} = 0 },
	) or die;

Regards,
Mikhael.

-- 
perl -e 'print+chr(64+hex)for+split//,d9b815c07f9b8d1e'



More information about the Perl mailing list