Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

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

by Anonymous Monk
on Jul 28, 2012 at 12:39 UTC ( #984191=note: print w/ replies, xml ) Need Help??


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

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


Comment on Re^3: Reading non-blockingly / "awk has to be better for something."
Download Code
Re^4: Reading non-blockingly / "awk has to be better for something."
by Anonymous Monk on Jul 28, 2012 at 13:02 UTC

    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://984191]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2014-07-14 05:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (255 votes), past polls