IO::Select - OO interface to the select system call
use IO::Select;$s = IO::Select->new();$s->add(\*STDIN);$s->add($some_handle);@ready = $s->can_read($timeout);@ready = IO::Select->new(@handles)->read(0);
TheIO::Select
package implements an object approach to the systemselect
function call. It allows the user to see what IO handles, seeIO::Handle, are ready for reading, writing or have an error condition pending.
The constructor creates a new object and optionally initialises it with a set of handles.
Add the list of handles to theIO::Select
object. It is these values that will be returned when an event occurs.IO::Select
keeps these values in a cache which is indexed by thefileno
of the handle, so if more than one handle with the samefileno
is specified then only the last one is cached.
Each handle can be anIO::Handle
object, an integer or an array reference where the first element is anIO::Handle
or an integer.
Remove all the given handles from the object. This method also works by thefileno
of the handles. So the exact handles that were added need not be passed, just handles that have an equivalentfileno
Returns a true value (actually the handle itself) if it is present. Returns undef otherwise.
Return an array of all registered handles.
Return an array of handles that are ready for reading.TIMEOUT
is the maximum amount of time to wait before returning an empty list, in seconds, possibly fractional. IfTIMEOUT
is not given and any handles are registered then the call will block.
Same ascan_read
except check for handles that can be written to.
Same ascan_read
except check for handles that have an exception condition, for example pending out-of-band data.
Returns the number of handles that the object will check for when one of thecan_
methods is called or the object is passed to theselect
static method.
Return the bit string suitable as argument to the core select() call.
select
is a static method, that is you call it with the package name likenew
.READ
,WRITE
andERROR
are eitherundef
orIO::Select
objects.TIMEOUT
is optional and has the same effect as for the core select call.
The result will be an array of 3 elements, each a reference to an array which will hold the handles that are ready for reading, writing and have error conditions respectively. Upon error an empty array is returned.
Here is a short example which shows howIO::Select
could be used to write a server which communicates with several sockets while also listening for more connections on a listen socket
use IO::Select;use IO::Socket;$lsn = new IO::Socket::INET(Listen => 1, LocalPort => 8080);$sel = new IO::Select( $lsn );while(@ready = $sel->can_read) { foreach $fh (@ready) { if($fh == $lsn) { # Create a new socket $new = $lsn->accept; $sel->add($new); } else { # Process socket # Maybe we have finished with the socket $sel->remove($fh); $fh->close; } }}
Graham Barr. Currently maintained by the Perl Porters. Please report all bugs to <perl5-porters@perl.org>.
Copyright (c) 1997-8 Graham Barr <gbarr@pobox.com>. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Perldoc Browser is maintained by Dan Book (DBOOK). Please contact him via theGitHub issue tracker oremail regarding any issues with the site itself, search, or rendering of documentation.
The Perl documentation is maintained by the Perl 5 Porters in the development of Perl. Please contact them via thePerl issue tracker, themailing list, orIRC to report any issues with the contents or format of the documentation.