未处理 Perl 简单命令行参数

Perl simple command line args not being processed

考虑这个简单的 perl 程序:

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

现在运行它:

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 在所有情况下都工作得很好。

我考虑了一段时间 Visual Studio 中的 Perl 扩展代码无法正常工作。

我可以确认这是一个错误。 Reported.


你的测试用例很差,有很多不必要的和未消除的变量。所以我要进行自己的测试:

#!/usr/bin/perl -s
print "ARGV: @ARGV\n";
print "arg: $arg\n";
print @ARGV == 0 && $arg eq "test" ? "ok\n" : "XXX\n";
# 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