sdf
is a bareword http://www.perlfoundation.org/perl5/index.cgi?bareword.
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}"
See 严格来说 use strict http://www.stonehenge.com/merlyn/UnixReview/col54.html:
use strict 的 subs 方面禁止将“裸词”解释为文本字符串。默认情况下,不是内置关键字或以前看到的子例程定义的 Perl 标识符(字母、数字和下划线的序列,除非完全是数字,否则不以数字开头)被视为带引号的文本字符串:
@daynames = (sun, mon, tue, wed, thu, fri, sat);
然而,这被认为是一种危险的做法,因为可能会导致以下错误:
@monthnames = (jan, feb, mar, apr, may, jun,
jul, aug, sep, oct, nov, dec);
你能发现这个错误吗?是的,第 10 个条目不是字符串“oct”,而是对内置函数的调用oct()
函数,返回默认值的等价数字$_
被视为八进制数。
更正:(感谢@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.
如果提供裸字print
在间接对象槽中,它被视为要打印的文件句柄。由于没有提供其他参数,print
默认打印$_
到文件句柄sdf
. Since 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
后者打印两个字符串,因为=>
如果字符串仅由“单词”字符组成,则会自动在 LHS 上引用字符串,从而删除第一个参数的文件句柄解释。
所有这些例子都表明,使用裸词会带来很多惊喜。你应该use strict
以避免此类情况的发生。