in reply to Re: cgi buffer flush
in thread cgi buffer flush

Isn't "$|++" the same as "$|=1"?


Replies are listed 'Best First'.
Re: Re: Re: cgi buffer flush
by dorko (Prior) on Mar 09, 2002 at 18:29 UTC
    Isn't "$|++" the same as "$|=1"?

    A lot of people seem to think it is, but it's not.

    • $|++ adds one to the current value of $|.
    • $| = 1 sets the value of $| equal to one.
    If for some reason $| = -1, doing a $|++ will make $| = 0, and usually that is not what $|++ is meant to do.

    $| = 1 is much safer and more straight forward (less ambiguous?).

    Update: Whoops. I was way wrong. A thousand lashes to me for misleading my fellows due to my neglectful ignorance. Thanks to dvergin for the correction and for setting me straight..



    -- Yeah, I'm a Delt.

            If for some reason $| = -1,...

      No. Please don't make assertions like that when you haven't tested them.

      $| is magic. It always equals either zero or one. Nothing else. Behold:

      print "$|\n"; # 0 $| = -1; print "$|\n"; # 1 $|++; print "$|\n"; # 1 $|++; print "$|\n"; # 1 $|--; print "$|\n"; # 0 $|--; print "$|\n"; # 1 $|--; print "$|\n"; # 0
      As you can see, $|++ always sets $| to one (no matter what it was) and $|-- always toggles it.

      I agree that $| = 1; is more clear to newer programmers. But rationalle for that usage has to do with the learning curve and not with the behavior of $|.

      "Perl is a mess and that's good because the
      problem space is also a mess.
      " - Larry Wall

      Makes perfect sense, but under what circumstances would $|=-1, unless those where you specifically defined it as such? I can see where it would be a much better idea to use $|=1 so you don't mistakenly define $| to 2 (or higher).

      OTOH, why haven't more folks discussed the use of the FileHandle or IO modules that support the autoflush method? Wouldn't that negate this argument altogether?

              so you don't mistakenly define $| to 2...

        That won't happen. See my response to dorko for a demonstration of the magical character of $|.