【发布时间】:2020-08-11 11:58:56
【问题描述】:
让我们举一个简单的例子:我希望在我的脚本中有以下输入:
[
{foo => date_1},
{foo2=> date_2},
{foo3=> undef}, # there was no input
... # probably more in the future
]
所以使用类似的东西:
use strict;
use warnings;
use utf8;
use Getopt::Long qw(GetOptions);
my @foos;
my @dates;
# INPUT
GetOptions(
'foo|f:s' => \@foos,
'date|d:s' => \@dates,
# 'help|h' => \&help,
) or die "Invalid options passed to $0\n";
我希望能够使用类似于以下内容的方式调用脚本:
perl script.pl --foo "Go with 1" --foo "Go with 2" --date "date1" --date "date2"
然后能够做类似的事情:
foreach my $i (0..scalar(@foos)){
print $foos[$i] . " " . $dates[$i] . "\n";
}
并获得:
Go with 1 date1
Go with 2 date2
我为什么要这样做?:我为一个函数循环了不确定数量的foos。在另一个函数中,我想再次循环遍历 foos 并打印与之关联的日期如果存在。
我想避免的事情:必须为 foos 的每个元素创建一个标志,如下所示:
GetOptions(
'foo|f:s' => \@foos,
'date-foo-1|df1:s' => \$dates_foo_1,
'date-foo-2|df2:s' => \$dates_foo_2,
# 'help|h' => \&help,
) or die "Invalid options passed to $0\n";
我想我能做的是将一个可选标志与另一个标志相关联,但我找不到任何相关的东西。
【问题讨论】:
标签: perl hash binding getopt-long