简体   繁体   English

“$ | ++”和“$ | = 1”有什么区别

[英]What is difference between “$|++” and “$|=1”

Can someone please help to clarify? 有人可以帮忙澄清一下吗? Also, please mention if there are other representation of "$|". 另外,请提及是否有“$ |”的其他表示。

Thanks in advance. 提前致谢。

There is no practical difference that I know of; 我所知道的没有实际的区别; $| only stores a boolean (0 or 1), so incrementing it will never result in any value other than 1. The micro-micro-optimizers might tell you that ++ is faster. 只存储一个布尔值(0或1),因此递增它将永远不会产生除1之外的任何值。微型微优化器可能会告诉你++更快。

Decrementing it, on the other hand, acts as a toggle, but I can't think of any good reason to do that in production code: either you want it on or off. 另一方面,减少它可以作为一个切换,但我不能想到在生产代码中这样做的任何好理由:要么你想要它打开还是关闭。

$| is super magical, so that $|++ does the same as $| = 1; 超级神奇,所以$|++$| = 1;相同 $| = 1; But why rely on magic when you can just do what you mean ( $| = 1; )? 但是,当你可以做你的意思时,为什么要依靠魔法( $| = 1; )?

the value of $| $ |的值 starts as 0 so $|++ increments from 0 to 1 while $| 从0开始,因此$ | ++从0增加到1而$ | = 1 sets it to 1. note however that its value can never be set to higher than 1 via incrementation or assignment eg $| = 1将其设置为1.请注意,它的值永远不能通过递增或赋值设置为高于1,例如$ | = 2 still evaluates to 1. = 2仍然评估为1。

You almost certainly don't want to be fiddling with lowlevel details like $| 你几乎肯定不想摆弄像$|这样的低级细节 in modern code. 在现代代码中。 This is far better and much more obviously-readable written as 这是更好,更明显可读的写作

use IO::Handle;
STDOUT->autoflush(1);

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

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