【发布时间】:2019-09-26 22:27:10
【问题描述】:
我正在尝试在命令行中将数组传递给 Perl。
我正在阅读来自https://perldoc.perl.org/Getopt/Long.html的说明
我的脚本是
#!/usr/bin/env perl
use strict;
use warnings FATAL => 'all';
use feature 'say';
use autodie ':all';
use Getopt::Long 'GetOptions';
my @array;
GetOptions ("a=s@" => \@array);#'file' indicates string on command line, '=s' means it must be a string
if (defined $array[0]) {#http://perldoc.perl.org/Getopt/Long.html#Options-with-values
my @z = split(/,/,join(',',@array));
say 'The array is ' . join (', ', @z);
}
使用命令行输出
con@VB:~/Scripts$ perl array_getopt.pl -a v c d
The array is v
这是不正确的,因为它错过了c 和d
我也试过
GetOptions ("a=s" => \@array); 有同样的错误。
我在该页面上看到一些内容说我必须一遍又一遍地重复相同的标签,例如 perl array_getopt.pl -a v -a c -a d,最终用户不会喜欢。
如何将信息传递到命令行,以便-a v c d 将传递到数组?
【问题讨论】:
标签: perl getopt-long