Reputation: 7681
I am generalizing a pre-existing perl script... i.e. making it work on numerous instances simultaneously. Basically it is a parameter scan feature of a modelling script. The scan does a single parameter and I want it to do them all. The pre-existing script (called scan_var.pl from bionetgen if your interested) has a bunch of arguments - some of them optional.
The following is successful in implementing the parameter scan with the default settings
#execute the parameter scan with each variable individually
foreach $var_name (@var_names){
my $param = shift @var_names;
system ("perl", $scan_var_location, $model, $param, $min_value, $max_value, $NPTS);
}
But now I want to use the GetOpt::Long
module to parse in the optional arguments. My code so far is:
# some default parameters
my $log = 0;
my $t_end = 20;
my $n_steps = 1;
my $steady_state = 0;
my $method = "\"ode\"";
my $verbose = 0;
my $prefix;
my $options = GetOptions (
'verbose' => \$verbose, #boolean
'log' => \$log, #boolean
'n_steps:i' => \$n_steps, #integer
'steady_state' => \$steady_state, #boolean
'method:s' => \$method, #string
't_end:i' => \$t_end, #integer
'prefix:s' => \$prefix string
);
#execute the parameter scan with each variable individually
foreach $var_name (@var_names){ #iterates through a list stored in $var_names(not shown for concise-ness)
my $param = shift @var_names;
system ("perl", $scan_var_location, #required value, directory
$options, #optional command line arguments - corresponds to the list above
$model, #required command line value (directory)
$param, #list iterated over
$min_value, #requierd integer
$max_value, #required integer
$NPTS #required integer
);
}
This however is somehow incorrect. Does anybody have any suggestions for corrections?
Cheers
Upvotes: 0
Views: 1004
Reputation: 35208
The following is a tighter version of your code:
GetOptions (
'verbose' => \(my $verbose = 0), #boolean
'log' => \(my $log = 0), #boolean
'n_steps:i' => \(my $n_steps = 1), #integer
'steady_state' => \(my $steady_state = 0), #boolean
'method:s' => \(my $method = q{"ode"}), #string
't_end:i' => \(my $t_end = 20), #integer
'prefix:s' => \my $prefix, #string
);
#execute the parameter scan with each variable individually
#iterates through a list stored in $var_names(not shown for concise-ness)
foreach $var_name (@var_names) {
system("perl",
$scan_var_location, #required value, directory
$model, #required command line value (directory)
$var_name, #list iterated over
$min_value, #requierd integer
$max_value, #required integer
$NPTS, #required integer
);
}
Upvotes: 0
Reputation: 37146
GetOptions()
supports the storage of values in a hash. This allows you to reduce variable clutter:
use Getopt::Long;
my %options;
GetOptions( \%options,
'verbose',
'log',
'n_steps:i',
'steady_state',
'method:s',
't_end:i',
'prefix:s',
);
my $stringified_options = join ' ', map "-$_ $options{$_}", keys %options;
foreach my $var_name ( @var_name ) {
system ("perl", $scan_var_location,
$stringified_options,
$model,
$param,
$min_value,
$max_value,
$NPTS
);
}
Upvotes: 1
Reputation: 62154
One problem is that you probably don't want to use the return value of GetOptions
in your system
call. $options
has the return value:
GetOptions returns true to indicate success. It returns false when the function detected one or more errors during option parsing.
Upvotes: 0