http://www.perlmonks.org?node_id=984176


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