If you're willing to get down and dirty, you can use select() like the rest of them. Earlier, reyjrar was asking about doing the same sort of thing on a socket. The same principles apply since file-handles and socket handles are really quite the same. That post is here.

I've extracted the relevant bits of that code and posted it here as an example on how to check up on a filehandle without blocking:
my ($block_size) = 10_000_000; # 10MB block size my ($timeout) = 0; # Don't wait. my ($rfd) = ''; # Initialize with string only # SYNOPSIS: open ($filehandle, "FILE or PIPE"); CheckHandle ($filehandle); sub CheckHandle { my ($filehandle) = @_; vec ($rfd, fileno($filehandle), 1) = 1; # Wait for something to happen, and make sure # that it happened to the right filehandle. if (select ($rfd, undef, undef, $timeout) >= 0 && vec($rfd, fileno($filehandle), 1)) { # Something came in! my ($buffer); read ($filehandle, $buffer, $block_size); return $buffer; } return; }
Explanation:
POSIX calls such as select() use a slightly different view of the world when compared with Perl. Most of the time Perl will convert for you automatically, but there are a few cases where you have to help out.

select() polls a number of file-handles (or sockets) and can report if they are ready to read, write, or if they have experienced an error. The documentation on select() is a little thin, and it will take a bit of experimentation to get a good handle on it. Note that select() can be configured to block or to be non-blocking, the trick is in the "$timeout" parameter. A zero timeout will put it into non-blocking mode.

In order to tell select which filehandles you want to monitor, vec() is used to create a bit-mask with bit number zero representing filehandle 0, bit 1 for filehandle 1, etc. These filehandles are a bit different from Perl filehandles, so a quick conversion with fileno() will get things on track.

This code uses "scalar"-style filehandles here because they are easier to pass between functions, and they work the same in most circumstances.

In reply to Re: Non blocking read on a filehandle by tadman
in thread Non blocking read on a filehandle by odie

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.