The $select->can_read() does not block, despite what documentation says. It keeps reporting that there's something in STDIN, even if there's nothing there for sure

Define "nothing", sysread returning false? Then the handle is closed due to error (undef) or due to eof (zero). You need to handle those conditions. You are calling select (via can_read) on a handle that can't possibly ever return data.


By the way, the following is buggy:

my $rv = $handle->sysread($bufline, 4096); $line .= $bufline; while ($rv == 4096) { $rv = $handle->sysread($bufline, 4096); $line .= $bufline unless !$rv; }

It'll block if there's exactly 4096 bytes waiting. Remove the loop completely. You need to rely on your select (can_read) loop. What follows in the code should just restart the select loop unless it detects that it received a full message. This requires moving your handle's buffer ($line) outside of the loop, of course. In other words, the code should follow the following pattern:

- While there are handles from which to read, - Wait for data to arrive. - Read into the handle's buf. -> Don't forget to handle eof and error. - While the buf has a full command, - Remove the command from the buf. - Process the command.

By the way,

my $bufline = ""; my $rv = $handle->sysread($bufline, 4096); $line .= $bufline;

can be written as

my $rv = $handle->sysread($line, 4096, length($line));

In reply to Re: Non-blocking I/O woes by ikegami
in thread Non-blocking I/O woes by dwalin

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.