#!/usr/bin/perl -sw
use strict;
use warnings;
use Getopt::Long;
my $remote = 0;
my $test = 0;
GetOptions ('remote' => \$remote, 'test' => \$test);
print "$remote:$test\n";
perl test.pl-远程-测试
上面打印“0:0”。我是Perl的新手,所以我无法确定为什么它不起作用。
我还从http://perldoc.perl.org/Getopt/Long.html#Simple-options运行了“简单选项”部分,但也没有产生任何结果。
最佳答案
我相信,您在she-bang行中包含的-s
命令行选项正在对您造成伤害。根据perlrun documentation,-s
命令行选项:
enables rudimentary switch parsing for switches on the command line after the program name but before any filename arguments (or before an argument of --).
如果删除该选项,那么事情应该会按预期进行。我还建议您删除
-w
,因为您已经在使用use warnings
指令(use warnings
指令的功能更加强大,实际上是在替换-w
选项)。所以,长话短说,让您的第一行:
#!/usr/bin/perl
关于perl - 无法解析命令行的长选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12077207/