简体   繁体   English

点击“打印”声明的开头?

[英]Dot at the beginning of a “print” statement?

I've come across something odd while using a Perl script. 我在使用Perl脚本时遇到了一些奇怪的事情。 It's about using a dot giving different results. 这是关于使用一个点给出不同的结果。

perlop didn't turn anything up, or perhaps I just blew past it. perlop没有改变任何东西,或者我只是吹过它。 I was looking at Operator Precedence and Associativity 我在看运算符优先级和关联性

print "I'd expect to see 11x twice, but I only see it once.\n";
print (1 . 1) . "3";
print "\n";
print "" . (1 . 1) . "3\n";
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) + "3\n";

Putting quotes at the start is an acceptable workaround to get what I want, but what is happening here with the order of operations that I'm missing? 在一开始就引用引号是一个可以接受的解决方法,以获得我想要的东西,但是这里发生的事情是我缺少的操作顺序? What rules are there to be learned? 有什么规则可以学习?

When you put the first argument to print in parentheses, Perl sees it as function call syntax. 当您将第一个参数放在括号中print时,Perl将其视为函数调用语法。

So this: 所以这:

print (1 . 1) . "3";

is parsed as this: 被解析为:

print(1 . 1)  . "3";

or, equivalently: 或者,等效地:

(print 1 . 1) . "3";

Therefore, Perl prints "11", then takes the return value of that print call (which is 1 if it succeeded), concatenates 3 to it, and - since the whole expression is in void context - does absolutely nothing with the resulting 13 . 因此,Perl打印“11”,然后获取该print调用的返回值(如果成功则为1 ),将3连接到它,并且 - 因为整个表达式在void上下文中 - 对结果13完全没有任何作用。

If you run your code with warnings enabled (via -w on the command line or the use warnings; pragma), you will get these warnings identifying your error: 如果在启用警告的情况下运行代码(通过命令行上的-wuse warnings; 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.

As Borodin points out in the comment below, you shouldn't rely on -w (or the in-code equivalent $^W ); 正如Borodin在下面的评论中指出的那样,你不应该依赖-w (或代码内等价的$^W ); production code should always make use of the warnings pragma, preferably with use warnings qw(all); 生产代码应始终使用warnings pragma,最好use warnings qw(all); . While it wouldn't matter in this particular instance, you should also use strict; 虽然在这个特定情况下无关紧要,但你也应该use strict; , although requesting modern features via use version ; ,虽然通过use 版本请求现代功能; for a Perl version of 5.11 or higher automatically turns on strict as well. 对于Perl版本的5.11或更高版本,自动打开也是strict

If a named operator (or a sub call) is followed by parens, those parens delimit the operands (or arguments). 如果命名运算符(或子调用)后跟parens,那些parens将分隔操作数(或参数)。

print (1 . 1) . "3";       ≡  ( print(1 . 1) ) . "3";
print "" . (1 . 1) . "3";  ≡  print("" . (1 . 1) . "3");

Note that Perl would have alerted you of your problems had you been using ( use strict; and) use warnings qw( all ); 请注意,Perl会提醒您使用过的问题( use strict;并且) use warnings qw( all ); as you should. 你应该的。

print (...) interpreted as function at a.pl line 2.
print (...) interpreted as function at a.pl line 6.
Useless use of concatenation (.) or string in void context at a.pl line 2.
Useless use of addition (+) in void context at a.pl line 6.
I'd expect to see 11x twice, but I only see it once.
11
113
Pluses: I expect to see 14 in both cases, and not 113, because plus works on numbers.
11
Argument "" isn't numeric in addition (+) at a.pl line 8.
14

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM