加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

为什么在这个Perl示例中定义的sdf返回true?

发布时间:2020-12-16 06:11:17 所属栏目:大数据 来源:网络整理
导读:我在Perl中尝试过这个例子.有人可以解释为什么这是真的吗? if (defined sdf) { print "true"; } 它打印真实. sdf可以是任何名称. 另外,如果定义了sdf函数并且它返回0,则它不会打印任何内容. print(sdf);不打印sdf字符串但是 if (sdf eq "sdf"){ print "true
我在Perl中尝试过这个例子.有人可以解释为什么这是真的吗?

if (defined sdf)  {   print "true";  }

它打印真实.

sdf可以是任何名称.

另外,如果定义了sdf函数并且它返回0,则它不会打印任何内容.

print(sdf);不打印sdf字符串但是

if (sdf eq "sdf")
{
 print "true";
}

打印真实.

如果sdf是一个字符串,则相关问题仍然存在.什么不是印刷品?

解决方法

sdf是 bareword.

perl -Mstrict -e "print qq{definedn} 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上的字符串,删除第一个参数的文件句柄解释.

所有这些例子表明,使用裸字会带来许多惊喜.你应该使用严格来避免这种情况.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读