Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^2: Reading non-blockingly / "awk has to be better for something."

by james2vegas (Chaplain)
on Jul 28, 2012 at 09:48 UTC ( #984176=note: print w/ replies, xml ) Need Help??


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


Comment on Re^2: Reading non-blockingly / "awk has to be better for something."
Select or Download Code
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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://984176]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (9)
As of 2014-09-22 08:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (185 votes), past polls