Using regular buffered reads, you don't really have much control over it. Most likely, it will block even if you sent exactly one line, because at the time of the actual (hidden) sysread, it doesn't know that there's a full line available -- it just reads a fixed number of bytes.

So you have two options. One is to do a select and a sysread for every individual byte. This is probably easier but very inefficient.

Another is to change STDIN to be nonblocking, and try to sysread large chunks at a time. Keep trying until sysread returns undef, and then check $!{EWOULDBLOCK}. Completely untested:

sub read_available { my ($fh) = @_; my $data = ''; while (1) { my $buf; my $nread = sysread($fh, $buf, 4096); if (defined $nread) { if ($nread == 0) { # End of file, so we have everything we'll ever get return ($data, 1); } else { $data .= $buf; } } else { if ($!{EWOULDBLOCK}) { return ($data, 0); } else { return; # Error } } } } . . . # Set STDIN nonblocking IO::Handle->new_from_fd(fileno(STDIN), "r")->blocking(0); . . . # If select says that STDIN is readable... my ($data, $eof) = read_available(*STDIN) or die "Error reading stdin: $!";

I really should test this once, but hopefully it gives the right idea. Seems like a correct version of this routine ought to be in a FAQ somewhere. Or maybe it is; I didn't check.


In reply to Re^3: forked kid can't read from IO::Select->can_read by sfink
in thread forked kid can't read from IO::Select->can_read by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.