从Perl中的管道读取无缓冲数据

Bra*_*rad 7 linux perl perl-io

我试图从Perl中的管道读取unbufferd数据.例如,在下面的程序中:

open FILE,"-|","iostat -dx 10 5";
$old=select FILE;
$|=1;
select $old;
$|=1;

foreach $i (<FILE>) {
  print "GOT: $i\n";
}
Run Code Online (Sandbox Code Playgroud)

iostat每隔10秒(5次)吐出数据.你会期望这个程序也这样做.然而,相反它似乎挂起50秒(即10x5),之后它会吐出所有数据.

我怎样才能返回任何可用的数据(以无缓冲的方式),而无需等待所有EOF?

PS我在Windows下看到了很多对此的引用 - 我在Linux下这样做.

tch*_*ist 5

#!/usr/bin/env perl

use strict;
use warnings;



open(PIPE, "iostat -dx 10 1 |")       || die "couldn't start pipe: $!";

while (my $line = <PIPE>) {
    print "Got line number $. from pipe: $line";
}

close(PIPE)                           || die "couldn't close pipe: $! $?";
Run Code Online (Sandbox Code Playgroud)