Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2024-03-29 13:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found