Thie following code is instructive:
use strict; use warnings; $| = 0; print $|++ . " " for (1..4); $| = 0; print "\n"; print $|-- . " " for (1..4);
Prints:
0 1 1 1 0 1 0 1
Why does that happen? Well, from the documentation snippet referred to in Perl Idioms Explained - $|++, you see that the only legal values for $| are 0 and 1. When you increment $| to 2 it is set back to 1, so the 1 'sticks'. However, when you decrement $| to -1 the -1 is changed to 1. Next time it decrements from 1 to 0 - back where you started. A cheap toggle, so long as you are not interested in the effects on buffering.
You might also consider:
use strict; use warnings; my $x = 1; print $x ^= 1 . " " for (1..4);
which prints:
0101
In reply to Re: $| pre and post increment and deincrement behavior
by GrandFather
in thread $| pre and post increment and deincrement behavior
by bioMan
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |