NAME
IO::Select - OO interface to the system select call
SYNOPSYS
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);
DESCRIPTION
The IO::Select package implements an object approach to the system select function call. It allows the user to see what IO handles, see IO::Handle, are ready for reading, writing or have an error condition pending.
CONSTRUCTOR
- new ( [ HANDLES ] )
-
The constructor create a new object and optionally initialises it with a set of handles.
METHODS
- add ( HANDLES )
-
Add the list of handles to the
IO::Selectobject. It is these values that will be returned when an event occurs.IO::Selectkeeps these values in a cache which is indexed by thefilenoof the handle, so if more than one handle with the samefilenois specified then only the last one is cached. - remove ( HANDLES )
-
Remove all the given handles from the object.
- can_read ( [ TIMEOUT ] )
-
Return an array of handles that are ready for reading.
TIMEOUTis the maximum amount of time to wait before returning an empty list. IfTIMEOUTis not given then the call will block. - can_write ( [ TIMEOUT ] )
-
Same as
can_readexcept check for handles that can be written to. - has_error ( [ TIMEOUT ] )
-
Same as
can_readexcept check for handles that have an error condition, for example EOF. - select ( READ, WRITE, ERROR [, TIMEOUT ] )
-
selectis a static method, that is you call it with the package name likenew.READ,WRITEandERRORare eitherundeforIO::Selectobjects.TIMEOUTis optional and has the same effect as before.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.
EXAMPLE
Here is a short example which shows how IO::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;
}
}
}
AUTHOR
Graham Barr <Graham.Barr@tiuk.ti.com>
REVISION
$Revision: 1.2 $
COPYRIGHT
Copyright (c) 1995 Graham Barr. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.