- 论坛徽章:
- 0
|
如题,请教各位大神,我经常看到一些Perl的书籍中有例子设置$|=1,这个意思我查询了资料就是$|值在非零时对当前选定的文件执行写或者打印操作后强制清除缓冲区。
我看过两种情况使用这个东东:我的问题是我们为什么要这么做,另外还有多少种情况下我们需要这么做,希望有大神给出一个比较全的回答,多谢了!
1、在磁盘坏块时需要手动递归检查数据是否被损坏。
2、在文件句柄中打开过滤器时,比如
#!/usr/bin/perl
$|=1;
$tmpfile="temp";
open(DB,"data.txt") or die "Can't open the file: $!\n";
open(SAVED,">&STDOUT") or die "$!\n";
open(STDOUT,">$tmpfile") or die "Can't open: $!\n";
open(SORT,"|sort +1") or die;
print SORT while(<DB>);
close SORT;
open(STDOUT,">&SAVED") or die "Can't open: $!\n";
print "Here wo are printing to the screen again.\n";
rename("temp","data.txt"); |
|