in reply to Re: Reading non-blockingly / "awk has to be better for something."
in thread Reading non-blockingly / "awk has to be better for something."

When using Gtk2 I normally use the AnyEvent wrapper over that, especially for its AnyEvent::Handle's methods which let you define reading by line as you wish. But keeping with plain Gtk2 you can just, with your $fh set non-blocking, call sysread with the maximum expected length of your data, like this:

use IO::Handle; my $fh = *STDIN; $fh->blocking(0); while (1) { # this block would go in your callback, not in a loop lik +e this: $fh->sysread( my $data, 255 ); print "$data"; # split $data into lines on CR|LF put the first lin +e on the end of the last line of the previous block (use an array per +haps) }

Replies are listed 'Best First'.
Re^3: Reading non-blockingly / "awk has to be better for something."
by Anonymous Monk on Jul 28, 2012 at 12:39 UTC

    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; });

      Here's a self-contained version of that code.

      It goes into EOF-loop if it says last unless $buf, and into slurp mode if it says return FALSE unless $buf