Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

perl $|=1; What is this?

Tags:

perl

People also ask

What does 1 mean in Perl?

1 at the end of a module means that the module returns true to use/require statements. It can be used to tell if module initialization is successful. Otherwise, use/require will fail. $somevar is a variable which is accessable only inside the block. It is used to simulate "static" variables.

What does =~ mean in Perl?

=~ is the Perl binding operator. It's generally used to apply a regular expression to a string; for instance, to test if a string matches a pattern: if ($string =~ m/pattern/) {

What is @$ in Perl?

It's a dereference. $tp is a reference to an array. @$tp says "dereference and give me the values", it could also be written as @{$tp} .


perlvar is your friend. It documents all these cryptic special variables.

$OUTPUT_AUTOFLUSH (aka $|):

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.

Mnemonic: when you want your pipes to be piping hot.

Happy coding.


For the other questions:

There is no reason that use strict; comes after $|, except by the programmers convention. $| and other special variables are not affected by strict in this way. The spacing is also not important -- just pick your convention and be consistent. (I prefer spaces.)


$| = 1; forces a flush after every write or print, so the output appears as soon as it's generated rather than being buffered.

See the perlvar documentation.

$| is the name of a special variable. You shouldn't introduce a space between the $ and the |.

Whether you use whitespace around the = or not doesn't matter to Perl. Personally I think using spaces makes the code more readable.

Why the use strict; comes after $| = 1; in your script I don't know, except that they're both the sort of thing you'd put right at the top, and you have to put them in one order or the other. I don't think it matters which comes first.


It does not matter where in your script you put a use statement, because they all get evaluated at compile time.

$| is the built-in variable for autoflush. I agree that in this case, it is ambiguous. However, a lone $ is not a valid statement in perl, so by process of elimination, we can say what it must mean.

use lib qw(.) seems like a silly thing to do, since "." is already in @INC by default. Perhaps it is due to the book being old. This statement tells perl to add "." to the @INC array, which is the "path environment" for perl, i.e. where it looks for modules and such.