7 IO::Select - OO interface to the system select call
13 $s = IO::Select->new();
16 $s->add($some_handle);
18 @ready = $s->can_read($timeout);
20 @ready = IO::Select->new(@handles)->read(0);
24 The C<IO::Select> package implements an object approach to the system C<select>
25 function call. It allows the user to see what IO handles, see L<IO::Handle>,
26 are ready for reading, writing or have an error condition pending.
32 =item new ( [ HANDLES ] )
34 The constructor create a new object and optionally initialises it with a set
45 Add the list of handles to the C<IO::Select> object. It is these values that
46 will be returned when an event occurs. C<IO::Select> keeps these values in a
47 cache which is indexed by the C<fileno> of the handle, so if more than one
48 handle with the same C<fileno> is specified then only the last one is cached.
50 =item remove ( HANDLES )
52 Remove all the given handles from the object.
54 =item can_read ( [ TIMEOUT ] )
56 Return an array of handles that are ready for reading. C<TIMEOUT> is the maximum
57 amount of time to wait before returning an empty list. If C<TIMEOUT> is
58 not given then the call will block.
60 =item can_write ( [ TIMEOUT ] )
62 Same as C<can_read> except check for handles that can be written to.
64 =item has_error ( [ TIMEOUT ] )
66 Same as C<can_read> except check for handles that have an error condition, for
69 =item select ( READ, WRITE, ERROR [, TIMEOUT ] )
71 C<select> is a static method, that is you call it with the package name
72 like C<new>. C<READ>, C<WRITE> and C<ERROR> are either C<undef> or
73 C<IO::Select> objects. C<TIMEOUT> is optional and has the same effect as
76 The result will be an array of 3 elements, each a reference to an array
77 which will hold the handles that are ready for reading, writing and have
78 error conditions respectively. Upon error an empty array is returned.
84 Here is a short example which shows how C<IO::Select> could be used
85 to write a server which communicates with several sockets while also
86 listening for more connections on a listen socket
91 $lsn = new IO::Socket::INET(Listen => 1, LocalPort => 8080);
92 $sel = new IO::Select( $lsn );
94 while(@ready = $sel->can_read) {
95 foreach $fh (@ready) {
104 # Maybe we have finished with the socket
113 Graham Barr <Graham.Barr@tiuk.ti.com>
121 Copyright (c) 1995 Graham Barr. All rights reserved. This program is free
122 software; you can redistribute it and/or modify it under the same terms
128 use vars qw($VERSION @ISA);
131 $VERSION = sprintf("%d.%02d", q$Revision: 1.2 $ =~ /(\d+)\.(\d+)/);
133 @ISA = qw(Exporter); # This is only so we can do version checking
138 my $type = ref($self) || $self;
140 my $vec = bless [''], $type;
155 my $fn = $f =~ /^\d+$/ ? $f : fileno($f);
158 vec($vec->[0],$fn++,1) = 1;
170 my $fn = $f =~ /^\d+$/ ? $f : fileno($f);
173 vec($vec->[0],$fn++,1) = 0;
185 select($r,undef,undef,$timeout) > 0
197 select(undef,$w,undef,$timeout) > 0
209 select(undef,undef,$e,$timeout) > 0
229 if defined $_[0] && !ref($_[0]);
231 my($r,$w,$e,$t) = @_;
234 my $rb = defined $r ? $r->[0] : undef;
235 my $wb = defined $w ? $e->[0] : undef;
236 my $eb = defined $e ? $w->[0] : undef;
238 if(select($rb,$wb,$eb,$t) > 0)
243 my $i = _max(defined $r ? scalar(@$r) : 0,
244 defined $w ? scalar(@$w) : 0,
245 defined $e ? scalar(@$e) : 0);
247 for( ; $i > 0 ; $i--)
251 if defined $r->[$i] && vec($rb, $j, 1);
253 if defined $w->[$i] && vec($wb, $j, 1);
255 if defined $e->[$i] && vec($eb, $j, 1);
258 @result = (\@r, \@w, \@e);
270 for($i = scalar(@$vec) - 1 ; $i > 0 ; $i--)
272 next unless defined $vec->[$i];
274 if vec($bits,$i - 1,1);