LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   $| in perl (https://www.linuxquestions.org/questions/programming-9/%24%7C-in-perl-4175440285/)

sharky 12-06-2012 09:50 AM

$| in perl
 
I'm trying to decipher some perl code and there is a statement that I don't understand.

Code:

$|=1;
I've never seen "dollar/pipe" before and google doesn't even seem to understand the search.

Any perl gurus know what this variable in perl represents or how it is used?

Thanks,

druuna 12-06-2012 09:54 AM

This from the Programming Perl book:
Quote:

autoflush HANDLE EXPR
$OUTPUT_AUTOFLUSH
$AUTOFLUSH
$|

[FHA] If set to true, forces a buffer flush after every print, printf, and write on the currently selected output handle. (We call this command buffering. Contrary to popular belief, setting this variable does not turn off buffering.) The default is false, which on many systems means that STDOUT will be line buffered if output is to the terminal, and block buffered otherwise, even on pipes and sockets. Setting this variable is useful when you are outputting to a pipe, such as when you are running a Perl script under rsh(1) and want to see the output as it's happening. If you have pending, unflushed data in the currently selected filehandle's output buffer when this variable is set to true, that buffer will be immediately flushed as a side-effect of assignment. See the one-argument form of select for examples of controlling buffering on filehandles other than STDOUT. (Mnemonic: when you want your pipes to be piping hot.)
Simply said: Setting $| makes sure that the output is "seen" immediately.

david1941 12-06-2012 10:07 AM

Here's an example out of one of my scripts:
Code:

open STUCKLOG, ">$SessionLog"
        or die "can't open $SessionLog";                #  Set a log file
select(STUCKLOG); $|=1;                                #  autoflush on


sharky 12-06-2012 10:59 AM

That answers my question.

Thanks,

linosaurusroot 12-06-2012 11:05 AM

man perlvar


All times are GMT -5. The time now is 10:33 AM.