Kamikaze
Kamikaze

Reputation: 854

Perl Getopt::Long Dynamic Parameters

With GetOpt::Long, is it possible to create a dynamic list of parameters?

myprog.pl --dir /tmp --force --releaes 1.2.3

 
my %options = (); 
my @options = qw(dir force release );
#note dir and release take argument, and force is a flag my $result = GetOptions(\%options, \@optons); #or something like that print "dir $options{dir} \n"; #produces say /tmp print "force $options{force} \n"; # produces 1 or 0 print "release $options{release} \n"; # and so on

Thanks

Upvotes: 1

Views: 757

Answers (2)

ikegami
ikegami

Reputation: 385657

Subs take a list of scalars for arguments. That list can be generated from any expression*, including an array.

my @options;
if (condition()) { # Dynamic
   @options = qw( dir=s force release=s );
} else {
   @options = ...;
}

GetOptions(\%options, @optons);

* — Prototypes can alter what expressions are allowed and how the expression is evaluated.

Upvotes: 0

Oleksandr Tymoshenko
Oleksandr Tymoshenko

Reputation: 1340

This should do the trick:

my @options = qw(dir=s force release=s); 
...
my $result = GetOptions(\%options, @options);

Upvotes: 3

Related Questions