Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello, monks.I was reading the IO::Select doc on perldoc.com , but I still can't grasp what this code is actually doing or its significance.
$rin = $win = $ein = ''; vec($rin,fileno(STDIN),1) = 1; $ein = $rin | $win; ($nfound,$timeleft) = select($rout=$rin, $wout=$win, $eout=$ein, $timeout);
help? in laymans terms any examples would greatly expedite my understanding ;)

Replies are listed 'Best First'.
Re: IO::Select question
by bschmer (Friar) on Aug 10, 2001 at 14:06 UTC
    The code that you posted is not actually using the IO::Select module, but instead the built-in select call.

    To answer your question about what the code does, the vec() call sets up a value where the bits indicate which filehandles should be watched for input or output. In essance, this vec() setup will turn on the bit in position fileno(STDIN) in $rin. Since STDIN is usually 0, $rin will be 1 after this call. If you then add a call that sets the bit for STDOUT (usually fileno 1) in addition to STDIN, $rin would be 3. This setup wouldn't make a lot of sense in the select call since you can't read from STDOUT.

    Once $rin and $win have been set up to watch the appropriate handles the vectors are combined to be used to detect handles with eexceptions.

    The select() call then:

    • Watches the handles in $rin to see if any of the handles have data available to be read.
    • Watches the handles in $win to see if writes to any of the handles would block.
    • Watches the handles in $ein for handles with exceptions.
    $timeout indicates how long the handles should be watched. If a handle state changes before $timeout expires, select returns. When select() returns, the number of handles that have data, can block and have exceptions are returned, along with the time that remained on the timeout (Windows versions of Perl return the timeout that was passed in). Due to the assignments in the call, $rout will contain the vector that indicates the filehandles that have data available, $wout will be a vector that indicates which handles would block, and $eout is a vector that indicates the handles that have exceptions. Using this method prevents you from having to re-set the vectors if you wanted to call select again. IO::Select puts a prettier face on the calls so that you don't have to do the vec() calls yourself.

    If you have access to a UNIX box, it may be helpful to look at the select() man page.