Perl简单命令行参数未被处理



考虑这个简单的perl程序:

#!/usr/bin/perl -s
print "ARGV: @ARGVn";
print "arg: $argn";

现在运行它:

chmod u+x test.pl
./test.pl -arg=works

输出为:

ARGV: 
arg: works

在调试器中运行程序也可以:

perl -d test.pl -arg=works
Loading DB routines from perl5db.pl version 1.53
Editor support available.
Enter h or 'h h' for help, or 'man perldebug' for more help.
main::(./test.pl:3):    print $arg, "n";
DB<1> c
ARGV:      <-- SEE HERE
arg: works <-- SEE HERE
Debugged program terminated.  Use q to quit or R to restart,
use o inhibit_exit to avoid stopping after program termination,
h q, h R or h o to get additional info.
DB<1> q

但是,如果添加了包含路径,则该参数将不再被解析:

perl -I . -d test.pl -arg works
Loading DB routines from perl5db.pl version 1.53
Editor support available.
Enter h or 'h h' for help, or 'man perldebug' for more help.
main::(./test.pl:3):    print $arg, "n";
DB<1> c
ARGV: test.pl -arg=works <-- OOPS
arg:                     <-- OOPS
Debugged program terminated.  Use q to quit or R to restart,
use o inhibit_exit to avoid stopping after program termination,
h q, h R or h o to get additional info.
DB<1> q

当使用-I时,@ARGV似乎不会以相同的方式处理。这就像-s选项不再工作,因为它应该消耗开关,如-opt=value。为什么呢?

Getopt::Long和Getopt::std在所有情况下都可以正常工作。

我认为有一段时间Perl扩展在Visual Studio Code不能正常工作。

我可以确认这是一个bug。报道。


您的测试用例非常差,有许多不必要的和未消除的变量。所以我要自己测试一下:

#!/usr/bin/perl -s
print "ARGV: @ARGVn";
print "arg: $argn";
print @ARGV == 0 && $arg eq "test" ? "okn" : "XXXn";
# Baseline
$ perl a.pl -arg=test
ARGV:
arg: test
ok
# With -I .
$ perl -I . a.pl -arg=test
ARGV: a.pl -arg=test
arg:
XXX
# The problem isn't the use of both -I and -s.
$ perl -I . -s a.pl -arg=test
ARGV:
arg: test
ok
# What about -w?
$ perl -w a.pl -arg=test
ARGV:
arg: test
ok
# Or -0?
$ perl -0777 a.pl -arg=test
ARGV:
arg: test
ok
# It's just -I
$ perl -CSDA a.pl -arg=test
ARGV:
arg: test
ok
$ perl -v | grep This
This is perl 5, version 34, subversion 0 (v5.34.0) built for x86_64-linux-thread-multi

这是一个变通方法:

$ perl -Mlib=. a.pl -arg=test
ARGV:
arg: test
ok

最新更新