Perl 如何使用GetOptions获取默认参数?

Perl 如何使用GetOptions获取默认参数?,perl,Perl,我已经看了好几年的医生了,但是我似乎找不到我需要的。。。(也许我是瞎子) 我想做的是像这样解析命令行 myperlscript.pl -mode [sth] [inputfile] 我可以使用-mode部分,但我不确定如何获取[inputfile]。如有任何建议,将不胜感激 不以-开头的命令行参数仍将在@ARGV中,不是吗?此任务不使用GetOptionsGetOptions将为您简单地解析选项,并将所有非选项保留在@ARGV中。因此,在调用GetOptions后,只需查看@ARGV以查找在命

我已经看了好几年的医生了,但是我似乎找不到我需要的。。。(也许我是瞎子)

我想做的是像这样解析命令行

myperlscript.pl -mode [sth] [inputfile]

我可以使用
-mode
部分,但我不确定如何获取[inputfile]。如有任何建议,将不胜感激

不以
-
开头的命令行参数仍将在
@ARGV
中,不是吗?

此任务不使用
GetOptions
GetOptions
将为您简单地解析选项,并将所有非选项保留在
@ARGV
中。因此,在调用
GetOptions
后,只需查看
@ARGV
以查找在命令行上传递的任何文件名。

任何未由GetOptions处理的内容都将保留在
@ARGV
中。所以你可能想要像这样的东西

use Getopt::Long;
my %opt
my $inputfile = 'default';
GetOptions(\%opt, 'mode=s');
$inputfile = $ARGV[0] if defined $ARGV[0];

GetOptions
将在
@ARGV
变量中保留它未分析的任何参数。因此,您可以在
@ARGV
变量上循环

use Getopt::Long;
my %opt;
GetOptions(
  \%opt,
  'mode=s'
);

for my $filename (@ARGV){
  parse( $filename, \%opt );
}
还有另一个选项,您可以使用特殊的
参数回调选项

use Getopt::Long qw'permute';
our %opt;
GetOptions(
  \%opt,
  'mode=s',
  '<>' => sub{
    my($filename) = @_;
    parse( $filename, \%opt );
  }
);
此示例将
$opt{mode}
设置为
s
,然后它将调用
parse
,并将
file1
作为参数。然后它将调用
parse
,并将
file2
作为参数。然后将
$opt{mode}
更改为
t
,并使用
file3
调用
parse
,作为参数

perl test.pl -mode s file1 file2 -mode t file3