Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在perl的系统函数中使用命令行中的可选参数_Perl_Command Line_System - Fatal编程技术网

在perl的系统函数中使用命令行中的可选参数

在perl的系统函数中使用命令行中的可选参数,perl,command-line,system,Perl,Command Line,System,我正在泛化一个预先存在的perl脚本。。。i、 e.使其同时适用于多个实例。基本上,它是建模脚本的参数扫描功能。扫描只执行一个参数,我希望它完成所有参数。预先存在的脚本(如果您感兴趣,可以从bionetgen调用scan_var.pl)有一系列参数,其中一些参数是可选的 以下是使用默认设置成功执行参数扫描的步骤 #execute the parameter scan with each variable individually foreach $var_name (@var_names){

我正在泛化一个预先存在的perl脚本。。。i、 e.使其同时适用于多个实例。基本上,它是建模脚本的参数扫描功能。扫描只执行一个参数,我希望它完成所有参数。预先存在的脚本(如果您感兴趣,可以从bionetgen调用scan_var.pl)有一系列参数,其中一些参数是可选的

以下是使用默认设置成功执行参数扫描的步骤

#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);
}
但是现在我想使用
GetOpt::Long
模块来解析可选参数。到目前为止,我的代码是:

# 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
           );
}
然而,这在某种程度上是不正确的。有人有什么改正的建议吗


干杯

一个问题是您可能不想在
系统
呼叫中使用
GetOptions
返回值
$options
具有返回值:

GetOptions返回true表示成功。当 函数在选项解析期间检测到一个或多个错误

支持在散列中存储值。这允许您减少可变杂波:

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

以下是代码的更严格版本:

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

始终
严格使用;使用警告!请更详细地描述您的问题。如果您将
GetOptions
的每个参数对放在单独的一行上,您的代码将更易于阅读和调试。目前,你有
'n\u步骤:i'=>\\$n\u步骤,
并且额外的反斜杠是多余的。Biffen-使用严格和警告是打开的,这只是我脚本的底部,因为它太长了,无法要求人们全部阅读(但无论如何感谢你的建议)。至于更多细节,我已经尝试过这样做,如果我使用可选参数,我的新脚本将在系统函数中自动使用它们。我想问题是有些是布尔型的,而有些是数值型的。。。我在想,在这个上下文中,括号前面的反斜杠是什么意思?我使用它们只是因为我在其他例子中看到了它是如何完成的,但我不明白为什么。它不能是转义字符,对吗?反斜杠是将对标量的引用传递给
GetOptions
。这就是
GetOptions
函数用来设置词汇变量值的机制。