A C*_*nge 10 perl command-line-arguments
所以我正在尝试运行此代码......
my $filePath = $ARGV['0'];
if ($filePath eq ""){
print "Missing argument!";
}
Run Code Online (Sandbox Code Playgroud)
它应检查第一个命令行参数,并告诉我它是否为空,但它返回此错误,我无法弄清楚原因:
Use of uninitialized value $filePath in string eq at program.pl line 19.
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
Cat*_*ham 18
只需检查$ ARGV [0]是否已定义
#!/usr/bin/perl
use strict;
use warnings;
if(!defined $ARGV[0]){
print "No FilePath Specified!\n";
}
Run Code Online (Sandbox Code Playgroud)
如果没有通过命令行,这将打印"No FilePath Specified!\n".
您遇到的问题是,您是将$ filePath设置为未定义的值.警告是抱怨的,因为您已经尝试将未定义的值与""进行比较.警告认为值得告诉你.
我用我的例子来展示一种检查某些东西是否已定义的简洁方法,但从技术上讲,你也可以这样做:
if(!@ARGV){
print "No FilePath Specified!\n";
}
Run Code Online (Sandbox Code Playgroud)
空和未初始化不是一回事.您可以检查是否使用defined运算符初始化变量,例如:
if ((!defined $filePath) || ($filePath eq "")) {
# $filePath is either not initialized, or initialized but empty
...
}
Run Code Online (Sandbox Code Playgroud)
我很确定你的意思是:
my $filePath = $ARGV[0];
Run Code Online (Sandbox Code Playgroud)
(没有报价)