What does this do?

Discussion in 'Perl Misc' started by Bob, Jul 8, 2004.

  1. Bob

    Bob Guest

    $|++;

    I see that in code but my searches have come up with nothing.

    Robert
    Bob, Jul 8, 2004
    #1
    1. Advertising

  2. > $|++;
    > I see that in code but my searches have come up with nothing.

    As $| is a boolean variable, I suspect the one who wrote this thought
    incrementing is faster than assigning, i.e. when $x is 0, $x++ is faster
    than $x = 1. Maybe I am wrong, this is just an assumption.
    Eugene Mikheyev, Jul 8, 2004
    #2
    1. Advertising

  3. Bob

    gnari Guest

    "Bob" <> wrote in message
    news:...
    > $|++;
    >
    > I see that in code but my searches have come up with nothing.


    $| is explained in
    perldoc perlvar

    the ++ is just a cute way to set it to a true value. if assumes
    the value is not -1

    gnari
    gnari, Jul 8, 2004
    #3
  4. (Bob) wrote:

    > $|++;
    >
    > I see that in code but my searches have come up with nothing.


    perldoc perlvar

    It is a slightly obscure way of disable output buffering. The
    recommended way is

    $| = 1;

    Peter

    --
    #!/local/bin/perl5 -wp -*- mode: cperl; coding: iso-8859-1; -*-
    # matlab comment stripper (strips comments from Matlab m-files)
    s/^((?:(?:[])}\w.]'+|[^'%])+|'[^'\n]*(?:''[^'\n]*)*')*).*/$1/x;
    Peter J. Acklam, Jul 8, 2004
    #4
  5. Bob wrote:

    > $|++;
    >
    > I see that in code but my searches have come up with nothing.


    If it helps, the default value of $| is undef, or zero in numeric context,
    so incrementing it with ++ makes it non-zero. The results of assigning a
    non-zero value to $| are documented in 'perldoc perlvar'.

    sherm--

    --
    Cocoa programming in Perl: http://camelbones.sourceforge.net
    Hire me! My resume: http://www.dot-app.org
    Sherm Pendley, Jul 8, 2004
    #5
  6. Bob

    Brad Baxter Guest

    On Thu, 8 Jul 2004, gnari wrote:

    > "Bob" <> wrote in message
    > news:...
    > > $|++;
    > >
    > > I see that in code but my searches have come up with nothing.

    >
    > $| is explained in
    > perldoc perlvar
    >
    > the ++ is just a cute way to set it to a true value. if assumes
    > the value is not -1
    >
    > gnari


    The value is NEVER -1. :)

    Regards,

    Brad
    Brad Baxter, Jul 8, 2004
    #6
  7. Abigail <> wrote:

    > There's no advantage of writing '$| ++' instead of '$| = 1', but
    > the latter is far less obscure.


    And the latter turns on buffering also in the extremely unlikely
    event that $| should ever be -1, in which case $|++ would turn it
    off.

    Peter

    --
    #!/local/bin/perl5 -wp -*- mode: cperl; coding: iso-8859-1; -*-
    # matlab comment stripper (strips comments from Matlab m-files)
    s/^((?:(?:[])}\w.]'+|[^'%])+|'[^'\n]*(?:''[^'\n]*)*')*).*/$1/x;
    Peter J. Acklam, Jul 8, 2004
    #7
  8. On 8 Jul 2004, Peter J. Acklam wrote:

    >Abigail <> wrote:
    >
    >> There's no advantage of writing '$| ++' instead of '$| = 1', but
    >> the latter is far less obscure.

    >
    >And the latter turns on buffering also in the extremely unlikely
    >event that $| should ever be -1, in which case $|++ would turn it
    >off.


    Except that $| can't ever be -1 (unless you've done *| = \-1, in which
    case $|++ will throw a "Modification of read-only" error).

    $|, as defined by the internal magic supporting it, is set to 0 when given
    a false value, and 1 when given ANY true value.

    --
    Jeff "japhy" Pinyan % How can we ever be the sold short or
    RPI Acacia Brother #734 % the cheated, we who for every service
    RPI Corporation Secretary % have long ago been overpaid?
    http://japhy.perlmonk.org/ %
    http://www.perlmonks.org/ % -- Meister Eckhart
    Jeff 'japhy' Pinyan, Jul 8, 2004
    #8
  9. Bob

    Sam Holden Guest

    On 08 Jul 2004 23:11:24 +0200, Peter J. Acklam <> wrote:
    > Abigail <> wrote:
    >
    >> There's no advantage of writing '$| ++' instead of '$| = 1', but
    >> the latter is far less obscure.

    >
    > And the latter turns on buffering also in the extremely unlikely
    > event that $| should ever be -1, in which case $|++ would turn it
    > off.


    $| is a special variable it can never have the value of -1 and hence that
    event is not possible at all.

    It is either 0 or 1.

    perl -le "$|=-1;print $|"

    --
    Sam Holden
    Sam Holden, Jul 8, 2004
    #9
  10. Bob

    gnari Guest

    "Jeff 'japhy' Pinyan" <> wrote in message
    news:p...
    >
    > $|, as defined by the internal magic supporting it, is set to 0 when given
    > a false value, and 1 when given ANY true value.


    in that case the $|++ does not even have the advantage
    of allowing us to 'localize' the value as in:

    # unknown $| state
    $|++;
    ... do stuff ...
    $|--;
    this will alway end by resetting $| to 0, even if it
    was originally 1

    on the other hand $|-- will always reverse the value.
    now I will have to find a use for that ...

    gnari
    gnari, Jul 9, 2004
    #10
  11. Bob

    Ala Qumsieh Guest

    gnari wrote:

    > on the other hand $|-- will always reverse the value.
    > now I will have to find a use for that ...


    Some refer to this as the flip-flop behaviour. It is an invaluable tool
    for perlgolf. You can use it to alternate between two array elements in
    a loop without the need for an explicit variable. Here are a couple of
    perlgolf "holes" with solutions using this behaviour:

    http://perlgolf.sourceforge.net/TPR/0/3/
    http://perlgolf.sourceforge.net/TPR/0/5a/

    --Ala
    Ala Qumsieh, Jul 9, 2004
    #11
  12. Jeff 'japhy' Pinyan <> wrote:

    > On 8 Jul 2004, Peter J. Acklam wrote:
    >
    > >And the latter turns on buffering also in the extremely unlikely
    > >event that $| should ever be -1, in which case $|++ would turn it
    > >off.

    >
    > Except that $| can't ever be -1 (unless you've done *| = \-1, in
    > which case $|++ will throw a "Modification of read-only" error).


    Darn, then someone has fooled me. What I wrote above is virtually
    a citation of something I read somewhere. No big deal, though,
    but now I wonder where I got it from.

    Peter

    --
    #!/local/bin/perl5 -wp -*- mode: cperl; coding: iso-8859-1; -*-
    # matlab comment stripper (strips comments from Matlab m-files)
    s/^((?:(?:[])}\w.]'+|[^'%])+|'[^'\n]*(?:''[^'\n]*)*')*).*/$1/x;
    Peter J. Acklam, Jul 9, 2004
    #12
  13. On Thu, 8 Jul 2004, gnari wrote:

    >"Jeff 'japhy' Pinyan" <> wrote in message
    >news:p...
    >>
    >> $|, as defined by the internal magic supporting it, is set to 0 when given
    >> a false value, and 1 when given ANY true value.

    >
    >in that case the $|++ does not even have the advantage
    >of allowing us to 'localize' the value as in:
    >
    > # unknown $| state
    > $|++;
    > ... do stuff ...
    > $|--;
    >this will alway end by resetting $| to 0, even if it
    >was originally 1


    Luckily we have 'local' which does exactly what you want.

    # unknown $| state
    {
    local $| = 1;
    # ...
    }
    # $| is restored here

    --
    Jeff "japhy" Pinyan % How can we ever be the sold short or
    RPI Acacia Brother #734 % the cheated, we who for every service
    RPI Corporation Secretary % have long ago been overpaid?
    http://japhy.perlmonk.org/ %
    http://www.perlmonks.org/ % -- Meister Eckhart
    Jeff 'japhy' Pinyan, Jul 9, 2004
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bill Johnson
    Replies:
    0
    Views:
    1,232
    Bill Johnson
    Jul 8, 2005
  2. Replies:
    3
    Views:
    480
    Ken Cox [Microsoft MVP]
    Oct 7, 2005
  3. Janet Collins
    Replies:
    0
    Views:
    1,144
    Janet Collins
    Jan 13, 2006
  4. Replies:
    1
    Views:
    840
    Juan T. Llibre
    Feb 8, 2006
  5. Alek Nazarian
    Replies:
    7
    Views:
    3,221
    Alek Nazarian
    Oct 22, 2003
Loading...

Share This Page