【问题标题】:Perl simple command line args not being processedPerl 简单的命令行参数没有被处理
【发布时间】:2021-08-08 01:49:22
【问题描述】:

考虑这个简单的 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 Code 中的 Perl 扩展无法正常工作。

【问题讨论】:

  • 我可以用perl -I . -d test.pl -arg works 复制这种行为,但不能用perl -I. -d test.pl -arg works 复制(注意-I 和它的参数之间没有空格)。奇数。

标签: perl


【解决方案1】:

我可以确认这是一个错误。 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

【讨论】:

    猜你喜欢
    • 2011-06-03
    • 2018-05-24
    • 2013-06-04
    • 2015-05-01
    • 1970-01-01
    • 2020-01-23
    • 1970-01-01
    • 2013-05-08
    相关资源
    最近更新 更多