perl – 点击“打印”声明的开头?
发布时间:2020-12-16 06:10:18 所属栏目:大数据 来源:网络整理
导读:我在使用Perl脚本时遇到了一些奇怪的事情.这是关于使用一个点给出不同的结果. perlop 没有任何改变,或者我只是吹过它.我在看 Operator Precedence and Associativity print "I'd expect to see 11x twice,but I only see it once.n";print (1 . 1) . "3";pr
我在使用Perl脚本时遇到了一些奇怪的事情.这是关于使用一个点给出不同的结果.
print "I'd expect to see 11x twice,but I only see it once.n"; print (1 . 1) . "3"; print "n"; print "" . (1 . 1) . "3n"; print "Pluses: I expect to see 14 in both cases,and not 113,because plus works on numbers.n"; print (1 . 1) + "3"; print "n"; print "" + (1 . 1) + "3n"; 在一开始就引用引号是一个可以接受的解决方法,以获得我想要的东西,但是这里发生的事情是我缺少的操作顺序?有什么规则可以学习? 解决方法
当您将第一个参数放在括号中打印时,Perl将其视为函数调用语法.
所以这: print (1 . 1) . "3"; 被解析为: print(1 . 1) . "3"; 或者,等效地: (print 1 . 1) . "3"; 因此,Perl打印“11”,然后获取该打印调用的返回值(如果成功则为1),将3连接到它,并且 – 因为整个表达式在void上下文中 – 对结果13完全没有任何作用. 如果在启用警告的情况下运行代码(通过命令行上的-w或使用警告; pragma),您将收到以下警告以识别错误: $perl -w foo.pl print (...) interpreted as function at foo.pl line 2. print (...) interpreted as function at foo.pl line 6. Useless use of concatenation (.) or string in void context at foo.pl line 2. Useless use of addition (+) in void context at foo.pl line 6. 正如Borodin在下面的评论中指出的那样,你不应该依赖-w(或代码内等价的$^ W);生产代码应始终使用警告pragma,最好使用警告qw(all);.虽然在这个特定的例子中无关紧要,但你也应该使用strict;尽管通过useversion请求现代功能;对于Perl版本的5.11或更高版本,自动打开也是严格的. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |