我正在编写的脚本在 2 小时前运行良好,将结果打印到文件中。
矿工更改后,包括添加循环,脚本停止打印到文件。
Env:Windows 10 命令行; Perl 版本 v5.32.1.
一些代码:
if($resultFile ne ''){
print "Opening $resultFile for output\n";
open(OUT,">$resultFile") || die "Could not open output file, $resultFile;$!\n";
print OUT "test 0\n";
}
# test code
my $list = 'foo';
my $newline = "bar\n";
open FH, '>', "$list.tmp" or die "$list == $!";
print FH $newline or die "Error print: $!";
#close FH;
print $newline;
## begin CASES loop
my $i=0;
while(\<CASES\>) {
my $case = $_;
chomp($case);
print "$case\n";
print OUT "$case\n";
my @data1;
[snip : a lot of processing]
print "@data1\n";
print OUT "@data1\n";
print FH "test 6\n";
## end of CASES loop
}
输出:(短时间打印后)
输入 test_results3.txt
''
输入 foo.tmp
''
如果我让脚本运行更长时间,输出将显示在 test_results3.txt 中,但不会显示在 foo.tmp 中。
我在脚本顶部设置了“$| = 1”。
所以我的问题是:为什么数据没有在每个 print 语句之后立即转储——就像
$|=1
应该做的那样?
是否有一些最小量的缓冲正在进行?
$|=1
应该关闭输出缓冲。
这是怎么回事?
$| = 1;
只影响当前 select
ed 句柄(默认情况下 print
写入的句柄),最初是 STDOUT。
要禁用任意句柄的缓冲,您可以使用以下命令:
$handle->autoflush(1);
这是
的缩写my $old_fh = select( $handle );
$| = 1;
select( $old_fh );