我知道脚本可以检索通过 ARGV 传递给它的所有命令行参数,即:
# test.pl
print "$ARGV[0]\n";
print "$ARGV[1]\n";
print "$ARGV[2]\n";
## perl ./test.pl one two three
one
two
three
Run Code Online (Sandbox Code Playgroud)
在上面的例子中,传递给test.pl脚本的命令行参数是“一”、“二”和“三”。
现在,假设我运行以下命令:
## perl -d:DumpTrace test.pl one two three
or
## perl -c test.pl one two three
Run Code Online (Sandbox Code Playgroud)
如何从test.pl脚本的操作中判断选项-c或-d:DumpTrace已传递给 perl 解释器?
我正在寻找一种方法来确定在脚本执行期间选项何时传递给 perl 解释器:
if "-c" was used in the execution of `test.pl` script {
print "perl -c option was used in the execution of this script";
}
Run Code Online (Sandbox Code Playgroud)
您可以使用Devel::PL_origargv来访问传递给perl解释器的命令行参数。示例脚本p.pl:
use strict;
use warnings;
use Devel::PL_origargv;
my @PL_origargv = Devel::PL_origargv->get;
print Dumper({args => \@PL_origargv});
Run Code Online (Sandbox Code Playgroud)
然后像这样运行脚本,例如:
$ perl -MData::Dumper -I. p.pl
$VAR1 = {
'args' => [
'perl',
'-MData::Dumper',
'-I.',
'p.pl'
]
};
Run Code Online (Sandbox Code Playgroud)