简体   繁体   中英

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.

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; But why rely on magic when you can just do what you mean ( $| = 1; )?

the value of $| starts as 0 so $|++ increments from 0 to 1 while $| = 1 sets it to 1. note however that its value can never be set to higher than 1 via incrementation or assignment eg $| = 2 still evaluates to 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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