Bill
Bill

Reputation: 2633

Perl, what does $|++ do?

I'm re-factoring some perl code, and as seems to be the case, Perl has some weird constructs that are a pain to look up.

In this case I encountered the following...

$|++;

This is on a line by itself just after the "use" statements.

What does this command do?

Upvotes: 3

Views: 468

Answers (5)

LeoNerd
LeoNerd

Reputation: 8532

As others have pointed out, it enables autoflush on the selected output filehandle (which is likely STDOUT). What nobody else has said, though, is that while you're generally refactoring and neatening up code, you really ought to replace it with the equivalent but much more obvious

STDOUT->autoflush(1);

Upvotes: 1

OrangeDog
OrangeDog

Reputation: 38797

From perldoc perlvar:

$|

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 for that. See select on how to select the output channel. See also IO::Handle.

Therefore, as it always starts as 0, this increments it to 1, forcing a flush after every write/print.

You can replace it with the following to be much clearer.

use English '-no_match_vars';
$OUTPUT_AUTOFLUSH = 1;

Upvotes: 5

tjd
tjd

Reputation: 4104

If Google is your only source of information, I can understand how looking up special variables in Perl could cause consternation. Fortunately there is perldoc! Every machine with perl on it should also have perldoc. Use it without command line parameters to get a list of all the Core documentation that comes with your version of Perl.

  • To look up all special variables: perldoc perlvar

  • To look up a specific special variable:perldoc -v '$|' ( on *nix, use double quotes on Windows)

  • To look up perl's list of functions: perldoc perlfunc

  • To look up a specific function: perldoc -f sprintf

  • To look up the operators (including precedence): perldoc perlop

Armed with that information, you'll know what happens when you post-increment the Output Autoflush variable.

As a special bonus, perldoc.perl.org can manage all of these jobs with the exception of the -v search...

Upvotes: 1

Lee Duhem
Lee Duhem

Reputation: 15121

$| is one of Perl's special variables.

According to perlvar:

If set to nonzero, forces a flush right away and after every write or print on the currently selected output channel.

Upvotes: 1

Oesor
Oesor

Reputation: 6642

Looking up variables is best done with perlvar (perldoc perlvar, or http://perldoc.perl.org/perlvar.html)

From that:

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 for that. See select on how to select the output channel. See also IO::Handle.

++ is the increment operator, which adds one to the variable.

So $|++ sets autoflush true (default 0 + 1 = 1, which boolean evals as true), which forces writes to stdout to not be buffered.

Upvotes: 3

Related Questions