# perl -wle'print $|; $|--; print $|';
0
1
# perl -wle'print $|; $| -= 1; print $|';
0
1
See also
$ perl -wle'print $|++ for 1.. 10'
0
1
1
1
1
1
1
1
1
1
$| only has two values, 0 and 1. I thought this was documented in perlvar but it doesn't seem to be. Perhaps it should be.
Re:Feature
cwest on 2002-08-20T13:16:05
Sure it is, "... $| tells you only whether you've asked Perl explicitly to flush after each write..."
To me that sounds like it will return the moral equivalent of true or false (zero and one in this case) based on what the state of $| is.
Re:Feature
KM on 2002-08-20T13:28:55
Yes, that's how it works. It is a switch, on (1) or off (0). But, why would, in the case if this variable, substracting from 0 makes it 1? If that is a 'feature', I wouldn't mind knowing what the feature is:) Actually, this also occurs with $^W:
# perl -le 'print $^W; $^W--; print $^W';
0
1
Re:Feature
KM on 2002-08-20T13:20:05
Yes, it has a range of [0,1], so I would expect it to stay at 1 when continually incremented. I don't, however, expect it to be incremented to 1 when it is 0 and decremented. I expect:0 -> 1 when $|++
1 -> 1 when $|++
1 -> 0 when $|--
0 -> 0 when $|--I think the range should be documented, but is it going from a value of 0 to 1 when decremented a bug, or is it a documentation issue? (Of course, once you document a bug is a feature, it isn't a bug anymore
:) Re:Feature
davorg on 2002-08-20T13:41:54
I suppose that as it's a boolean value, then the -1 gets mapped to "true" - or 1. Not sure how intuitive that is tho'.
Of course, if you ever needed a flip-flop variable then you could make use of this behaviour
:) $ perl -wle'print $|-- for 1.. 10'
0
1
0
1
0
1
0
1
0
1Re:Feature
KM on 2002-08-20T13:56:51
Yeah, I don't think it is intuitive. I guess it should just be documented somewhere that this is the case with Perl's special boolean vars. I don't like it though, it isn't DWIM.
The one true way is $| = 1;
, even if you're re-enabling buffering.
That's perl's new built-in boolean type. The pumpkings have been slipping in Perl 6 features when you weren't looking.
I think what happens is you decrement 0 to get -1, but $| only stores the truth or falsehood of its value, so it stores and outputs 1.
Neat!
On another note, I like how everyone in this thread is actually using -w for a one liner.