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. This method also works
53 by the C<fileno> of the handles. So the exact handles that were added
54 need not be passed, just handles that have an equivalent C<fileno>
56 =item can_read ( [ TIMEOUT ] )
58 Return an array of handles that are ready for reading. C<TIMEOUT> is the maximum
59 amount of time to wait before returning an empty list. If C<TIMEOUT> is
60 not given then the call will block.
62 =item can_write ( [ TIMEOUT ] )
64 Same as C<can_read> except check for handles that can be written to.
66 =item has_error ( [ TIMEOUT ] )
68 Same as C<can_read> except check for handles that have an error condition, for
73 Returns the number of handles that the object will check for when
74 one of the C<can_> methods is called or the object is passed to
75 the C<select> static method.
77 =item select ( READ, WRITE, ERROR [, TIMEOUT ] )
79 C<select> is a static method, that is you call it with the package name
80 like C<new>. C<READ>, C<WRITE> and C<ERROR> are either C<undef> or
81 C<IO::Select> objects. C<TIMEOUT> is optional and has the same effect as
84 The result will be an array of 3 elements, each a reference to an array
85 which will hold the handles that are ready for reading, writing and have
86 error conditions respectively. Upon error an empty array is returned.
92 Here is a short example which shows how C<IO::Select> could be used
93 to write a server which communicates with several sockets while also
94 listening for more connections on a listen socket
99 $lsn = new IO::Socket::INET(Listen => 1, LocalPort => 8080);
100 $sel = new IO::Select( $lsn );
102 while(@ready = $sel->can_read) {
103 foreach $fh (@ready) {
105 # Create a new socket
112 # Maybe we have finished with the socket
121 Graham Barr <Graham.Barr@tiuk.ti.com>
129 Copyright (c) 1995 Graham Barr. All rights reserved. This program is free
130 software; you can redistribute it and/or modify it under the same terms
136 use vars qw($VERSION @ISA);
139 $VERSION = sprintf("%d.%02d", q$Revision: 1.2 $ =~ /(\d+)\.(\d+)/);
141 @ISA = qw(Exporter); # This is only so we can do version checking
150 my $type = ref($self) || $self;
152 my $vec = bless [undef,0], $type;
165 $vec->[VEC_BITS] = '' unless defined $vec->[VEC_BITS];
169 my $fn = $f =~ /^\d+$/ ? $f : fileno($f);
172 vec($vec->[VEC_BITS],$fn,1) = 1;
173 $vec->[FD_COUNT] += 1
174 unless defined $vec->[$fn+FIRST_FD];
175 $vec->[$fn+FIRST_FD] = $f;
177 $vec->[VEC_BITS] = undef unless $vec->count;
187 my $fn = $f =~ /^\d+$/ ? $f : fileno($f);
190 vec($vec->[VEC_BITS],$fn,1) = 0;
191 $vec->[$fn+FIRST_FD] = undef;
192 $vec->[FD_COUNT] -= 1;
194 $vec->[VEC_BITS] = undef unless $vec->count;
202 my $r = $vec->[VEC_BITS] or return ();
204 select($r,undef,undef,$timeout) > 0
214 my $w = $vec->[VEC_BITS] or return ();
216 select(undef,$w,undef,$timeout) > 0
226 my $e = $vec->[VEC_BITS] or return ();
228 select(undef,undef,$e,$timeout) > 0
254 if defined $_[0] && !ref($_[0]);
256 my($r,$w,$e,$t) = @_;
259 my $rb = defined $r ? $r->[VEC_BITS] : undef;
260 my $wb = defined $w ? $e->[VEC_BITS] : undef;
261 my $eb = defined $e ? $w->[VEC_BITS] : undef;
263 if(select($rb,$wb,$eb,$t) > 0)
268 my $i = _max(defined $r ? scalar(@$r)-1 : 0,
269 defined $w ? scalar(@$w)-1 : 0,
270 defined $e ? scalar(@$e)-1 : 0);
272 for( ; $i >= FIRST_FD ; $i--)
274 my $j = $i - FIRST_FD;
276 if defined $rb && defined $r->[$i] && vec($rb, $j, 1);
278 if defined $wb && defined $w->[$i] && vec($wb, $j, 1);
280 if defined $eb && defined $e->[$i] && vec($eb, $j, 1);
283 @result = (\@r, \@w, \@e);
295 for($i = scalar(@$vec) - 1 ; $i >= FIRST_FD ; $i--)
297 next unless defined $vec->[$i];
299 if vec($bits,$i - FIRST_FD,1);