in reply to What the heck does $|++; do?

It's an idiomatic way of turning off output buffering, as controlled by the $| variable. See. perlvar and Perl Idioms Explained - $|++ for more info.


update: s/on/off/, thanks to halley for spotting that

Replies are listed 'Best First'.
Re: What the heck does $|++; do?
by Abigail-II (Bishop) on Apr 13, 2004 at 17:05 UTC
    Idiomatic? Needlessly obfuscated, I'd say, without even a gain in keystrokes. $|=1 takes four keystrokes, $|++ as well, but has more shifted keys. And it gives the false impression that you can turn buffering off by doing $| -- as often as you've given $| ++.
    $| = 0; $| ++; $| ++; $| --; $| --; print $|; # prints 1, not 0!

    If I were to code review production code, someone writing $| ++ instead of $| = 1 better have a pretty damn good reason for doing so.


      Idiomatic? Needlessly obfuscated, I'd say,
      I would say both. Part of me dislikes the peculiar ++ behaviour, but in it's defence, it is idiomatic.

      Come to think of it, this is another of those cargo cults, isn't it? People doing $|++ without thinking of the consequences, such as non-nestability. If you know that there will not be any nested $| accesses for the same handle, then it's ok.

      A minor point, ++ is easier to type than =1 in this case, as you already have your finger on the shift key from the |, so it requires two less actions (release Shift, move finger across keyboard to 1).

        ...move finger across keyboard to 1

        It is already there, you pressed 4 for the $ with your other hand, so your pinky is right above 1 ;-)

        He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.

        Chady |