Perl operator: $|++; dollar sign pipe plus plus
Asked Answered
G

4

19

I'm working on a new version of an already released code of perl, and found the line:

$|++;

AFAIK, $| is related with pipes, as explained in this link, and I understand this, but I cannot figure out what the ++ (plus plus) means here.

Thank you in advance.

EDIT: Found the answer in this link:

In short: It forces to print (flush) to your console before the next statement, in case the script is too fast.

Sometimes, if you put a print statement inside of a loop that runs really really quickly, you won’t see the output of your print statement until the program terminates. sometimes, you don’t even see the output at all. the solution to this problem is to “flush” the output buffer after each print statement; this can be performed in perl with the following command:

$|++;

[update] as has been pointed out by r. schwartz, i’ve misspoken; the above command causes print to flush the buffer preceding the next output.

Grenadier answered 10/1, 2012 at 13:44 Comment(0)
P
21

$| defaults to 0; doing $|++ thus increments it to 1. Setting it to nonzero enables autoflush on the currently-selected file handle, which is STDOUT by default, and is rarely changed.

So the effect is to ensure that print statements and the like output immediately. This is useful if you're outputting to a socket or the like.

Peseta answered 10/1, 2012 at 13:48 Comment(1)
Re "$|++ thus increments it to 1.", Not quite. $|++ sets it to 1, no matter what value it had before. It's a "shortcut" for $| = 1;Hodeida
S
16

$| is an abbreviation for $OUTPUT_AUTOFLUSH, as you had found out. The ++ increments this variable.

$| = 1 would be the clean way to do this (IMHO).

Sauls answered 10/1, 2012 at 13:47 Comment(14)
I agree that $| = 1; is clearer. After all, it's not like $|--; turns off autoflushing. (It toggles it.) $|++; is a stupid micro-optimisation. (Did you really need those 10 nanoseconds that bad?)Hodeida
@ikegami, why do you think reading the value, incrementing it and putting it back is 10 nanoseconds faster than simply putting 1 in? I think it's just that the author found this code better looking and he is entitled to have his own perception of beauty.Stripteaser
@Michael Krelin - hacker, Incrementing doesn't normally involve "putting back", but since this is a magical variable, there is some truth to that. So you're right; it might be an even more stupid optimisation that I previously realised as it might actually slow things down.Hodeida
@ikegami, heh, I already learned here in this question that it's not much of incrementing ($|++ will never go past 1). And yes, as optimization it is really stupid, but, like I said, it is not necessarily an optimization. It's just that there are many ways to say the same thing.Stripteaser
@Michael Krelin - hacker, If it's stupid as an optimisation, and it's bad for readability, then what's left?Hodeida
@ikegami, it's not bad for readability. Not universally even if you personally don't like it.Stripteaser
@Michael Krelin - hacker, if you tell Perl to do one thing (increment) and it does something else, that's the very definition of poor readability.Hodeida
@ikegami, readability is about reader's relationships with the source, not developer's relationships with perl.Stripteaser
@Michael Krelin - hacker, Readability is the lack of difference between the reader's expected result of the source and intended result of the source. If the reader understands the intended result, the code is readable. If the code hints it's going to do one thing, but does another, it's not readable.Hodeida
@ikegami, I suspect the intended reader understand the intended result in this case.Stripteaser
@ikegami, or better — only in this case I consider this code readable.Stripteaser
@Michael Krelin - hacker, It's funny you should be arguing that $|++ is more readable than $|=1 when you were fooled by what it does when you read it according to your since-edited post.Hodeida
@ikegami, I'm not arguing it's more readable. I am only saying that it may be easier and more esthetically pleasing to read for intended audience. Which, as you point out, I do not belong to. I'm just against putting a "stupid" tag.Stripteaser
Would you please continue this discussion elsewhere?Sauls
W
6

It's an old idiom, from the days before IO::Handle. In modern code this should be written as

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

Wellspoken answered 19/1, 2012 at 16:56 Comment(0)
S
5

It increments autoflush, which is most probably equivalent to turning it on.

Stripteaser answered 10/1, 2012 at 13:47 Comment(3)
I think you probably were kidding about wrapping around to zero, but it is interesting that $| is a boolean, try $|=3; print $|.Trapper
$| can only take on the values 0 and 1, so it can't wrap around. However, --$| does "wrap around", and can be used to toggle $| (instead of, say, $|=1-$| or $|=!$|).Mohan
No, I wasn't kidding, I was just wrong, assuming it's a number. Thanks for correction, I'll edit out the wraparound part.Stripteaser

© 2022 - 2024 — McMap. All rights reserved.