I'm having a bit of trouble going the plain Gtk2 route. I'm not sure what's wrong since I mucked around with the code and changed its behaviour a few times already, but it either 1) works until EOF, after which it goes to an infinite loop, or 2) sysreads the whole output on one go.
my $cmd = [perl => -e => '$|++; for my $i (0..4) { $sum+= $i; print
+"Line $i: sum = $sum\r\n"; sleep 1;}'];
my $cb = sub {print "CB: >>" . shift() . "<<\n";}
open($fh, '-|', @$cmd) or die "failed to launch external command: $!
+";
$fh->blocking(0);
$tag = Gtk2::Helper->add_watch($fh->fileno, in => sub {
if ($fh->eof) {
print "pipe EOF\n";
Gtk2::Helper->remove_watch($tag);
close $fh;
return;
}
while (1) {
my $buf;
$fh->sysread($buf, 4095);
print "READ: >>$buf<<\n";
last unless $buf;
for (split(/[\r\n]/, $buf)) {
$cb->($_);
}
}
# keep watch active
return TRUE;
});
|