Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Here's a tutorial I started on using select (primarily because I saw a query from tachyon on it and I figured it was a good thing to do). This is just a first cut, so be kind and send me your comments. :-)

Also, is there a way for to automagically handle POD format?

=head1 NAME perlselecttut - a tutorial on using select() and IO::Select =head1 DESCRIPTION This document attempts to explain how to use the 4-arg version of C<select()> and the module C<IO::Select> so that they are easier to understand. If anything is unclear, the author of this document would appreciate hearing about it. =head2 What are C<select()>/C<IO::Select> and why would you use them? Without going into too much detail (none at all in fact) C<select()> a +nd C<IO::Select> are useful for when you want to "watch" several filehandles at once to see if they are ready for reading or writing or if any of them have an exception condition. Now, when I say "filehandles" I really mean anything that can be read or written to as if it were a file: sockets, serial devices, pipes, fifos, etc. An example application that would use C<select> is a chat server. Each client that is connected to the server occupies it's own socket (filehandle), so the server would use C<select> to see what each clien +t says having to wait on any particular client. Caveat lector! This document is mostly written from a unix-centric point of view, but similar ideas apply on other operating systems. =head2 All about C<select()> Here's the canonical C<select()> example that you will find in the documentation: ($nfound,$timeleft) = select($rout=$rin, $wout=$win, $eout=$ein, $timeout); But what does it all mean? Why the assignments? Why those particular variable names? The first 3 arguments to C<select()> tell it which filehandles to watc +h for reading, writing, and exceptions respectively (thus the leading r, w, and e on each variable). These arguments are each a special bitvector where each "on" bit corresponds to the filedescriptor number of the filehandle that is being watched. The fourth argument to C<select()> is how long to watch the filehandles before giving up. Th +e C<select()> call returns a count of how many filehandles have triggere +d one of the reading/writing/exception conditions (C<$nfound>) and the amount of time left in the timeout specified (C<$timeleft>). =head3 What are filedescriptors? Each time you open a file, the operating system makes an entry in something called a file descriptor table so that it can keep track of the file (things like the current position, whether or not it's buffered, the contents of the buffer, etc.) By default the entry at index 0 is STDIN, at index 1 is STDOUT and STDERR is at index 2. The index into the filedescriptor table is called the file descriptor numb +er or L<fileno> for short. It's these indexes that are used as offsets in +to the bitvector to identify to C<select()> which filehandles you wish to watch. =head3 Using C<vec()> to build bitvectors A bitvector is an odd thing for perl because it's so low level. Having to fiddle with bitvectors shows how close C<select()> is to its C heri +tage. But luckily perl remembers its heritage through a function called C<vec()>. To create a bitvector for use with C<select()> with the proper bit turned on for STDIN you would write: vec($vector,fileno(STDIN),1) = 1; C<fileno()> is a routine that returns the index into the filedescriptor table for a given filehandle (aka. the fileno). Since we know STDIN is at index 0, we could have written vec($vector,0,1) = 1; But that would be I<extremely> unportable. If, for some reason, we happened to run on a system that used 0 for some other filehandle out program wouldn't work as expected. This I<could> happen if we closed STDIN, then opened another file. The operating system may use the firs +t available free filedescriptor when you open a new file and since we closed STDIN, descriptor 0 would be free. Anyway, the call to C<vec()> means: treat $vector is a bitvector and access the bits starting at the bit position corresponding to the the index into the filedescriptor table for the filehandle STDIN for 1 bit and assign that bit the value 1. So, to watch several filehandles you would do this: vec($vector,fileno(FOO),1) = 1; vec($vector,fileno(BAR),1) = 1; vec($vector,fileno(BAZ),1) = 1; and then use C<$vector> in the call to C<select()> to watch the FOO, BAR, and BAX filehandles. See C<perldoc -f vec> for more information on C<vec()> =head3 Why are we doing assignments in the C<select()> call? The reason you typically see assignments in the first three positions +is that C<select()> modifies its first 3 arguments to tell you I<which> filehandles have data ready for reading or writing or exceptions and y +ou usually want to continually watch the same filehandles over and over again. Since the following select($rout=$rin, $wout=$win, $eout=$ein, $timeout); functions the same as $rout = $rin; $wout = $win; $eout = $ein; select($rout, $wout, $eout, $timeout); The assignments allow $rin,$win, and $ein to keep their original value +s so that you can call it in a loop without having to continually build the bit vectors. Contrast these two functionally equivalent snippets: # Example 1, the usual idiom vec($rin,fileno(FOO),1) = 1; vec($rin,fileno(BAR),1) = 1; vec($rin,fileno(BAZ),1) = 1; while (1) { ($found) = select($rout=$rin,undef,undef,$timeout); next unless $found; # Check $rout to see which handles are ready for reading } # Example 2, building the vectors each time while (1) { vec($rinout,fileno(FOO),1) = 1; vec($rinout,fileno(BAR),1) = 1; vec($rinout,fileno(BAZ),1) = 1; ($found) = select($rinout,undef,undef,$timeout); next unless $found; # Check $rinout to see which handles are ready for reading } Oh, btw, you'll notice that I used C<undef> for the second and third argument to C<select()>. If you don't care to check any filehandles for reading, writing, or exceptions you can pass C<undef> in the respective position and C<select()> won't bother paying attention to that condition. You can also pass c<undef> for the timeout and C<select()> will wait forever for a filehandle to trigger the appropriate condition. =head3 Checking which filehandles are ready After C<select()> returns, the three bitvectors will have changed to reflect the actual filehandles that triggered the particular condition you were waiting for. One way to check which filehandle is ready is to just use C<vec()> again to see if the particular bit is 1. For example: vec($rin,fileno(FH1),1) = 1; vec($rin,fileno(FH2),1) = 1; vec($rin,fileno(FH3),1) = 1; while (1) { ($found) = select($rout=$rin,undef,undef,$timeout); next unless $found; if (vec($rout,fileno(FH1),1) == 1) { # There is data waiting to be read on FH1 } if (vec($rout,fileno(FH2),1) == 1) { # There is data waiting to be read on FH2 } # and so on ... } Another method would be to use C<select()> again. vec($fh1,fileno(FH1),1) = 1; vec($fh2,fileno(FH2),1) = 1; vec($fh3,fileno(FH3),1) = 1; $rin = $fh1 | $fh2 | $fh3; while (1) { ($found) = select($rout=$rin,undef,undef,$timeout); next unless $found; if (select($fh1,undef,undef,$timeout)) { # There is data waiting to be read on FH1 } if (select($fh2,undef,undef,$timeout)) { # There is data waiting to be read on FH2 } # and so on ... } By building individual bitvectors for each filehandle and then combini +ng them together using a bit-wise OR, we can check whether I<any> of the filehandles are ready with the combined bitvector or whether an individual filahandle is ready with the individual bitvectors using C<select()>. Note that several filehandles may be ready at once, so it would be prudent to service as many of the filehandles that you can before calling C<select()> again. =head3 I know which filehandles are ready. Now what? After you have setup C<select()> and determined which filehandles Are ready, you'll want to read from those filehandles that are ready for reading and write to those filehandles that are ready for writing and +do whatever is necessary to those filehandles that have an exception condition. [ To be honest, I've never used the ability to check for exception conditions on filehandles and I have little understanding of what it may be for. The only reference I have handy at the moment, Stevens' U<Advanced Programming in the Unix Environment>, says "... an exception condition corresponds to (a) the arrival of out-of-band data on a network connection, or (b) certain conditions occuring on a psued +o terminal that has been placed into packet mode" ] But you must be careful how you read or write data to the filehandle. Buffered I/O lik +e C<readline()> (aka, the diamond operator or <>) or C<read()>, won't wo +rk quite right, so you need to use C<sysread()> and C<syswrite()> to read/write from/to the appropriate filehandles. =head2 IO::Select As you can tell by now, C<select()> isn't the friendliest of routines to use. Luckily you have another option: C<IO::Select>. C<IO::Select +> is an object oriented interface that sits on top of the basic C<select()> routine such that you never have to see bitvectors and strange assignments. You deal only with C<IO::Select> objects and the filehandles themselves. Here's a simple example: use IO::Select; my $sel = IO::Select=>new; $sel->add(\*FOO); $sel->add(\*BAR); $sel->add(\*BAZ); if (@fh = $sel->can_read($timeout)) { # Each filehandle in @fh is ready to be read from } The basic usage is simple: you create a IO::Select object (possibly initializing it with filehandles), then add new file handles to the object using the C<add()> method, and when you're ready to "watch" the filehandles you call one of the C<can_read()>, C<can_write()>, or C<has_exception()> methods on the object. Each of these methods returns an array of filehandles such that you can read/write from/to them. =head1 AUTHOR Jonathan Scott Duff

In reply to Using select and IO::Select by duff

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, 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, 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.
  • Log In?

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

    How do I use this? | Other CB clients
    Other Users?
    Others drinking their drinks and smoking their pipes about the Monastery: (7)
    As of 2021-04-21 16:24 GMT
    Find Nodes?
      Voting Booth?

      No recent polls found