Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

buffering question

by Anonymous Monk
on Aug 29, 2001 at 01:35 UTC ( [id://108608] : perlquestion . print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

does it make a difference if i use $|=1; or ++$|; ? -curious newbie

Replies are listed 'Best First'.
Re: buffering question
by mitd (Curate) on Aug 29, 2001 at 02:06 UTC
    Both turn on AUTOFLUSH, so aside from the fact that the assignment is about 35-40% faster than increment, (but then you are not likely bury this in a loop :) ), they are equivalent.


    Benchmark: timing 1000000 iterations of ass, inc... ass: 1 wallclock secs ( 1.13 usr + 0.00 sys = 1.13 CPU) @ 88 +4955.75/s (n=1000000) inc: 2 wallclock secs ( 1.93 usr + 0.01 sys = 1.94 CPU) @ 51 +5463.92/s (n=1000000)

    Holy Mid-Life Crisis

    I think MitD is becoming pedantic.
    Oh my gosh.

    mitd-Made in the Dark
    'My favourite colour appears to be grey.'

Re: buffering question
by Rudif (Hermit) on Aug 29, 2001 at 02:04 UTC
    it makes no difference unless someone sneaked in  $|=-1; while you were not looking ;-)


      $|=-1;is the same as $|=1; or ++$|;
        I think Rudif was pointing out that $|++ and $|=1 are interchangable, unless the value of $| is already -1. In that (rather rare) case, the two behave quite differently.


Re: buffering question
by blakem (Monsignor) on Aug 29, 2001 at 01:45 UTC
    Nope, shouldn't make any difference at all, though $|++ is probably more common that ++$|.


Re: buffering question
by John M. Dlugosz (Monsignor) on Aug 29, 2001 at 08:24 UTC
    It makes a difference only when $| was -1 beforehand. If it was zero, ++ makes it 1 and that's the case you're expecting. If it was 1, ++ makes it 2, which is still true, so it has the same side effect as $|=1 would. If it was -42, than it becomes -41 which is also still true. Only if it was -1 then ++ makes it false, which is indeed different than $|=1.

    Therefore, they are not the same.

    Update: Nevermind.

      That's what I thought too.... It turns out that $| has some special magic associated with it. See chipmunks post about this.