What is the difference between "$ | ++" and "$ | = 1"

Can anyone help clarify? Also, please indicate if there is another representation of "$ |".

Thanks in advance.

+4
source share
4 answers

There is no practical difference that I know of; $| stores only a boolean value (0 or 1), so incrementing it will never lead to any value other than 1. Micromicro optimizers can tell you that ++ is faster.

Reducing it, on the other hand, acts like a switch, but I can not think of any reason for this in the production code: either you want to turn it on or off.

+10
source

$| is supermagical, so $|++ does the same as $| = 1; $| = 1; But why rely on magic when you can just do what you mean ( $| = 1; )?

+3
source

value of $ | starts at 0, so $ | ++ increases from 0 to 1, and $ | = 1 sets it to 1. note, however, that its value can never be set above 1 using an increment or assignment, for example. $ | = 2 is still evaluated at 1.

+2
source

You almost certainly don't want to play with low-level details like $| in modern code. It is much better and much more readable, written as

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

All Articles