Perl $ | Does setting affect system commands?

I look at some old Perl code where the author writes $| = 1 $| = 1 in the first line.

But there are no print statements in the code; it calls the binary C ++ code using the system command. Now I read that $| will be forcibly erased after each print. It also affects the output of the system command in any way, or I'm sure to delete this line.

Thanks Ervind

+4
source share
3 answers

I don’t believe that. $ | will affect how Perl works, not the external executable.

You must be safe to remove it.

perldoc-perlvar : States "If a nonzero value is set, it immediately makes it flush after each recording or printing on the currently selected output channel.". I think the β€œ selected output channel ” is important here. The external application will have its own output channel.

+7
source

With questions like this, it is often easy to write a trivial program that shows what the behavior is:

 #!/usr/bin/perl use strict; use warnings; if (@ARGV) { output(); exit; } print "in the first program without \$|:\n"; output(); $| = 1; print "in the first program with \$|:\n"; output(); print "in system with \$|\n"; system($^X, $0, 1) == 0 or die "could not run '$^X $0 1' failed\n"; $| = 0; print "in system without \$|\n"; system($^X, $0, 1) == 0 or die "could not run '$^X $0 1' failed\n"; sub output { for my $i (1 .. 4) { print $i; sleep 1; } print "\n"; } 

This shows that the installation of $| does not affect programs running through system .

+5
source

This is something you can easily check for yourself. Create a program in which buffering is important, such as printing multiple dots. You should see the output all immediately after ten seconds from the moment of buffering the output:

  #! perl

 foreach (1 .. 10)
     {
     print ".";
     sleep 1;
     }

 print "\ n";

Now try installing $| and name it with system :

  % perl -e "$|++; system( qq|$^X test.pl| )"; 

For my test case, $ | the value did not affect buffering in the child process.

+5
source

Source: https://habr.com/ru/post/1286562/


All Articles