select (ready file descriptors)

selectRBITS,WBITS,EBITS,TIMEOUT
The four-argument select
operator is totally unrelated to the previously described select operator. This operator is used to
discover which (if any) of your file descriptors are ready to do input
or output, or to report an exceptional condition. (This helps you avoid
having to do polling.) It calls the select(2)
syscall with the bit masks you’ve specified, which you can construct
using fileno and vec, like this:
$rin = $win = $ein = ""; vec($rin, fileno(STDIN), 1) = 1; vec($win, fileno(STDOUT), 1) = 1; $ein = $rin | $win;
If you want to select on many
filehandles, you might wish to write a subroutine:
sub fhbits {
my @fhlist = @_;
my $bits;
for my $fh (@fhlist) {
vec($bits, fileno($fh), 1) = 1;
}
return $bits;
}
$rin = fhbits(*STDIN, *TTY, *MYSOCK);Notice we passed in the filehandles using their typeglobs, because passing them in as strings is a bad idea. If you are using autovivified filehandles, you don’t have to do this.
If you wish to use the same bit masks repeatedly (and it’s more efficient if you do), the usual idiom is:
($nfound, $timeleft) =
select($rout=$rin, $wout=$win, $eout=$ein, $timeout);Or to block until any file descriptor becomes ready:
$nfound = select($rout=$rin, $wout=$win, $eout=$ein, undef);
As you ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access