1753435 Members
4777 Online
108794 Solutions
New Discussion юеВ

Perl Question

 
Sutapa Dey
Frequent Advisor

Perl Question

Hi,

Please let me know what does this syntax "$|++" mean in perl?

Thanks in advance
4 REPLIES 4
H.Merijn Brand (procura
Honored Contributor

Re: Perl Question

Short answer:

Unbuffered output on the current file handle

Longer answer:

$ man perlvar
:
HANDLE->autoflush(EXPR)
$OUTPUT_AUTOFLUSH
$| If set to nonzero, forces a flush right away and after every
write or print on the currently selected output channel.
Default is 0 (regardless of whether the channel is really
buffered by the system or not; $| tells you only whether you've
asked Perl explicitly to flush after each write). STDOUT will
typically be line buffered if output is to the terminal and
block buffered otherwise. Setting this variable is useful
primarily when you are outputting to a pipe or socket, such as
when you are running a Perl program under rsh and want to see
the output as it's happening. This has no effect on input
buffering. See "getc" in perlfunc for that. See "select" in
perldoc on how to select the output channel. See also
IO::Handle. (Mnemonic: when you want your pipes to be piping
hot.)

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
James R. Ferguson
Acclaimed Contributor

Re: Perl Question

Hi Sutapa:

To add, the post-increment applied to the '$|' variable is a very common Perl idiom used to set the autoflush variable on.

You could also simply do, and some folks consider this "better":

$| = 1;

You might find this discussion interesting:

http://www.perlmonks.org/?node_id=280025

Regards!

...JRF...

James R. Ferguson
Acclaimed Contributor

Re: Perl Question

Hi (again):

One last comment: You might also profit by reading this:

http://forums12.itrc.hp.com/service/forums/helptips.do?#28

Regards!

...JRF...
H.Merijn Brand (procura
Honored Contributor

Re: Perl Question

Beware of the possible performance hit of File::Handle when using the verbose HANDLE->autoflush (1) instead of the terse $|++. The performance hit isn't that big, but people like me think: why load a 600+ line module for such a simple operation.

On modern systems you won't notice, but hardcore perl programmers will use the cryptic but very fast way of unbuffering a filehandle like this:

select ((select (HANDLE), $| = 1)[0]);

which switches to HANDLE, sets unbuffered, and switches back to the current handle.

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn