if (defined sdf) { print "true"; }
它打印真实.
sdf可以是任何名称.
另外,如果定义了sdf函数并且它返回0,则它不会打印任何内容.
print(sdf);不打印sdf字符串但是
if (sdf eq "sdf") { print "true"; }
打印真实.
如果sdf是一个字符串,则相关问题仍然存在.什么不是印刷品?
解决方法
perl -Mstrict -e "print qq{defined\n} if defined sdf"
Bareword "sdf" not allowed while "strict subs" in use at -e line 1. Execution of -e aborted due to compilation errors.
为了更多的乐趣,试试吧
perl -Mstrict -e "print sdf => qq{\n}"
见Strictly speaking about use strict:
The subs aspect of use strict disables the interpretation of “bare words” as text strings. By default,a Perl identifier (a sequence of letters,digits,and underscores,not starting with a digit unless it is completely numeric) that is not otherwise a built-in keyword or prevIoUsly seen subroutine definition is treated as a quoted text string:
06003
However,this is considered to be a dangerous practice,because obscure bugs may result:
06004
Can you spot the bug? Yes,the 10th entry is not the string ‘oct’,but rather an invocation of the built-in
oct()
function,returning the numeric equivalent of the default$_
treated as an octal number.
更正:(谢谢@ysth)
E:\Home> perl -we "print sdf" Unquoted string "sdf" may clash with future reserved word at -e line 1. Name "main::sdf" used only once: possible typo at -e line 1. print() on unopened filehandle sdf at -e line 1.
如果提供裸字以在间接对象槽中打印,则将其作为要打印的文件句柄.由于没有提供其他参数,因此打印默认为将$_打印到filehandle sdf.由于sdf尚未打开,因此失败.如果在没有警告的情况下运行此操作,则看不到任何输出.另请注意:
E:\Home> perl -MO=Deparse -e "print sdf" print sdf $_;
作为对此观察的确认.另请注意:
E:\Home> perl -e "print asdfg,sadjkfsh" No comma allowed after filehandle at -e line 1.
E:\Home> perl -e "print asdfg => sadjkfsh" asdfgsadjkfsh
后者打印两个字符串,因为=>如果它们仅由’word’字符组成,则自动引用LHS上的字符串,删除第一个参数的文件句柄解释.
所有这些例子表明,使用裸字会带来许多惊喜.你应该使用严格来避免这种情况.