I am trying to read unbufferd data from a pipe in Perl. For example in the program below:
open FILE,"-|","iostat -dx 10 5";
$old=select FILE;
$|=1;
select $old;
$|=1;
foreach $i (<FILE>) {
print "GOT: $i\n";
}
iostat spits out data every 10 seconds (five times). You would expect this program to do the same. However, instead it appears to hang for 50 seconds (i.e. 10x5), after which it spits out all the data.
How can I get the to return whatever data is available (in an unbuffered manner), without waiting all the way for EOF?
P.S. I have seen numerous references to this under Windows - I am doing this under Linux.
while
not aforeach
. And your output buffering is immaterial, given that it’s an input handle not an output handle. – Mattheiforeach my $i (<FILE>) { ... }
, the file read is done in list context, that is, the whole file is read before the lines are processed in theforeach
loop. Inwhile (my $i = <FILE>) { ... }
, the read is done in scalar context, that is, each line is read and then processed in the while block, before the next line is read. – Longsighted