Perl是$ | 设置影响系统命令?

Arv*_*ind 4 perl buffering

我正在查看Perl中的一些旧代码,其中作者$| = 1在第一行写 了一个.

但是代码没有任何print语句,它使用system命令调用C++二进制文件.现在我看到$|每次打印后都会强制冲洗.它是否以任何方式影响系统命令的输出,或者我可以安全地删除该行.

谢谢Arvind

Xet*_*ius 7

我不相信.$ | 将影响Perl运行的方式,而不是任何外部可执行文件.

你可以安全地删除它.

perldoc - perlvar:States" 如果设置为非零,则在当前选定的输出通道上每次写入或打印后立即强制刷新. " 我认为重要的是" 当前选择的输出通道 ".外部应用程序将拥有自己的输出通道.


Cha*_*ens 5

对于这样的问题,通常很容易编写一个显示行为的简单程序:

#!/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";
}
Run Code Online (Sandbox Code Playgroud)

从中可以看出,设置$|对正在运行的程序没有影响system.


bri*_*foy 5

这是你可以轻松检查自己的东西.创建一个缓冲很重要的程序,比如打印一系列点.自输出缓冲后,您应该在十秒后立即看到输出:

#!perl

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

print "\n";

现在,尝试设置$|并调用它system:

 % perl -e "$|++; system( qq|$^X test.pl| )";
Run Code Online (Sandbox Code Playgroud)

对于我的测试用例,$ | 值不会影响子进程中的缓冲.