Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

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

by Anonymous Monk
on Jul 28, 2012 at 08:59 UTC ( [id://984164]=note: print w/replies, xml ) Need Help??


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

Throwing in more detail, the I/O poller module is Gtk2::Helper, whose documentation warns: "you should not use Perl's builtin read and write functions here because these operate always with buffered I/O. Use low level sysread() and syswrite() instead."

I'm not really sure what to construe of that. sysread() needs a size -- should I just read one byte at a time as Grimy suggested above? Am I doing this all wrong by using Perl's buffered I/O? (The code behaves as expected even if I use my current style.)

  • Comment on Re: Reading non-blockingly / "awk has to be better for something."

Replies are listed 'Best First'.
Re^2: Reading non-blockingly / "awk has to be better for something."
by james2vegas (Chaplain) on Jul 28, 2012 at 09:48 UTC
    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) }

      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

Re^2: Reading non-blockingly / "awk has to be better for something."
by Anonymous Monk on Jul 28, 2012 at 09:00 UTC

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2024-04-24 07:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found