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. | [reply] [d/l] [select] |
|
thanks a lot.. that helped me out.
| [reply] |
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.
| [reply] [d/l] [select] |
|
perl -wE 'say $|++ for 1 .. 5; say "--"; say $|-- for 1 .. 5'
0
1
1
1
1
--
1
0
1
0
1
| [reply] [d/l] |
|
Thanks a lot Dave i'll take that piece of advice.
| [reply] |
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.
| [reply] [d/l] |
|
It was said that --$| toggles $|. While $| = !$|; is clearer than --$|, noone said anything about wanting to toggle $|.
| [reply] [d/l] [select] |
Re: what does $|++ do here?
by planetscape (Chancellor) on May 30, 2011 at 22:39 UTC
|
| [reply] |
Re: what does $|++ do here?
by rovf (Priest) on May 30, 2011 at 12:15 UTC
|
| [reply] [d/l] |
|
i needed immediate help on that thing thus i myself posted it on both sites... thanks for showing concern sir.
| [reply] |
|
| [reply] [d/l] |
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.)
| |