Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

what does $|++ do here?

by deep3101 (Acolyte)
on May 29, 2011 at 16:20 UTC ( [id://907225]=perlquestion: print w/replies, xml ) Need Help??

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

Friends i think my question might seem too kiddish to you people but i just wanted to know the function of $|++; statement in this code which caught my eye:
use POSIX qw(:sys_wait_h); $|++; defined(my $pid = fork) or die "Couldn't fork: $!"; if (!$pid) { # Child exec('long_running_command', @args) or die "Couldn't exec: $!"; } else { # Parent while (! waitpid($pid, WNOHANG)) { print "."; sleep 1; } print "\n"; }
also i would like to know about the WNOHANG passed as parameter to waidpid();

Replies are listed 'Best First'.
Re: what does $|++ do here?
by JavaFan (Canon) on May 29, 2011 at 16:37 UTC
    Cargo code.

    There's no reason to prefer $|++; over $| = 1;. What the latter does, please refer to the perlvar manual.

    As for WNOHANG, it makes the waitpid not blocking, that is, it returns immediately, even if the process waiting for hasn't terminated yet.

      thanks a lot.. that helped me out.
Re: what does $|++ do here?
by davido (Cardinal) on May 30, 2011 at 05:38 UTC

    Though it's considered bad style, it does work. The $| special variable is basically an on/off switch for output buffering. When it is false (in the Boolean sense - default), print output is buffered, and doesn't get flushed, usually, until after a '\n' newline, or until the buffer fills up. Set that to true (again in the Boolean sense), and buffering is turned off, so that the script outputs on each print call, not waiting for a newline or full buffer.

    This special variable also has a special characteristic: It flip-flops like a switch when you increment it. That's what's being done in your sample script. But most people who care about readability and style prefer $| = 1;. It would be a "best practice" if you also localized the effects of the change (so that they don't ripple into other parts of your script where the behavior is unnecessary, or worse, undesirable. To do that, try:

    #....... irrelevant code not included... } else { local $| = 1; while( ! waitpid( $pid, WNOHANG ) ) { # ......

    Instead of putting it at the top of the script. If the script grows larger, you're not going to have to worry about undesirable behavior finding its way into other places.


    Dave

      It flip-flops like a switch when you increment it.
      If only.

      It's worse. It acts like a flip-flop when you decrement it, but not when you increment.

      perl -wE 'say $|++ for 1 .. 5; say "--"; say $|-- for 1 .. 5' 0 1 1 1 1 -- 1 0 1 0 1
      Thanks a lot Dave i'll take that piece of advice.
Re: what does $|++ do here?
by anonymized user 468275 (Curate) on May 30, 2011 at 09:21 UTC
    Noting also the comment about it being a toggle, the ++ doesn't make sense to me either. But in that case, I would tend to wonder if the following is what is really meant:
    $| = !$|;
    Which is a good enough reason otherwise to support the above idea of just assigning one to it to avoid confusion.

    One world, one people

      It was said that --$| toggles $|. While $| = !$|; is clearer than --$|, noone said anything about wanting to toggle $|.
Re: what does $|++ do here?
by planetscape (Chancellor) on May 30, 2011 at 22:39 UTC
Re: what does $|++ do here?
by rovf (Priest) on May 30, 2011 at 12:15 UTC
      i needed immediate help on that thing thus i myself posted it on both sites... thanks for showing concern sir.
        The problem is not posting on two sides. The problem is posting on two sides without saying so in both posts. This is a quesion of politeness - and I can't imagine that your need was so immediate that you couldn't spend the couple of seconds needed to mention your crossposting....

        -- 
        Ronald Fischer <ynnor@mm.st>
        A reply falls below the community's threshold of quality. You may see it by logging in.
Re: what does $|++ do here?
by sundialsvc4 (Abbot) on Jun 01, 2011 at 13:18 UTC

    This is a classic example of where a single Perl comment preceding this cryptic statement would have been extraordinarily helpful to more-than-a-few someones.   When I read source-code, what I most want to get a glimpse of is:   the original writer’s intentions, whether the resulting code be clever or not.   I can always use perldoc or RTFM™ to figure out what you wrote, but what takes so much time is understanding why you wrote it.   (Even in the case of your own code, which, months or years later, has become the work of a stranger.)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://907225]
Approved by salva
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2024-04-24 06:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found