在Perl脚本中获取一些我不太了解的奇怪行为.我试图将我的一些魔术字符串文字变成可以更容易修改的变量.
我的子程序中有一个参数叫做$in_feature.现在它的价值很简单"in_feature".我可以把它打印出来看起来很好.到现在为止还挺好...
但是这段代码失败了:
if ($in_feature != "" && !$blockModel->is_field($in_feature))
{
print "ERROR: was expecting to find a variable in the block model called $in_feature.\n";
return;
}
Run Code Online (Sandbox Code Playgroud)
如果我删除字符串比较并将方法调用更改回字符串文字,它按预期工作.
if (!$blockModel->is_field("in_feature"))
{
print "ERROR: was expecting to find a variable in the block model called $in_feature.\n";
return;
}
Run Code Online (Sandbox Code Playgroud)
所以变量是某种字符串,等于空字符串,不能用来代替字符串?!?这是为什么?
perl中的字符串比较使用ne和eq而不是!=和==
替换$in_feature != ""为$in_feature ne "",它可能会解决您的问题.