3 # Copyright (c) 1997-8 Graham Barr <gbarr@pobox.com>. All rights reserved.
4 # This program is free software; you can redistribute it and/or
5 # modify it under the same terms as Perl itself.
10 use warnings::register;
11 use vars qw($VERSION @ISA);
16 @ISA = qw(Exporter); # This is only so we can do version checking
25 my $type = ref($self) || $self;
27 my $vec = bless [undef,0], $type;
37 shift->_update('add', @_);
43 shift->_update('remove', @_);
50 my $fno = $vec->_fileno(shift);
51 return undef unless defined $fno;
52 $vec->[$fno + FIRST_FD];
59 return unless defined $f;
60 $f = $f->[0] if ref($f) eq 'ARRAY';
61 ($f =~ /^\d+$/) ? $f : fileno($f);
67 my $add = shift eq 'add';
69 my $bits = $vec->[VEC_BITS];
70 $bits = '' unless defined $bits;
76 my $fn = $vec->_fileno($f);
78 next unless defined $fn;
79 my $i = $fn + FIRST_FD;
80 if (defined $vec->[$i]) {
81 $vec->[$i] = $f; # if array rest might be different, so we update
85 vec($bits, $fn, 1) = 1;
88 if ( ! defined $fn ) { # remove if fileno undef'd
89 defined($_) && $_ == $f and do { $vec->[FD_COUNT]--; $_ = undef; }
90 for @{$vec}[FIRST_FD .. $#$vec];
93 my $i = $fn + FIRST_FD;
94 next unless defined $vec->[$i];
96 vec($bits, $fn, 1) = 0;
101 $vec->[VEC_BITS] = $vec->[FD_COUNT] ? $bits : undef;
109 my $r = $vec->[VEC_BITS];
111 defined($r) && (select($r,undef,undef,$timeout) > 0)
120 my $w = $vec->[VEC_BITS];
122 defined($w) && (select(undef,$w,undef,$timeout) > 0)
131 my $e = $vec->[VEC_BITS];
133 defined($e) && (select(undef,undef,$e,$timeout) > 0)
140 warnings::warn("Call to deprecated method 'has_error', use 'has_exception'")
141 if warnings::enabled();
157 sub as_string # for debugging
160 my $str = ref($vec) . ": ";
161 my $bits = $vec->bits;
162 my $count = $vec->count;
163 $str .= defined($bits) ? unpack("b*", $bits) : "undef";
166 splice(@handles, 0, FIRST_FD);
168 $str .= " " . (defined($_) ? "$_" : "-");
188 if defined $_[0] && !ref($_[0]);
190 my($r,$w,$e,$t) = @_;
193 my $rb = defined $r ? $r->[VEC_BITS] : undef;
194 my $wb = defined $w ? $w->[VEC_BITS] : undef;
195 my $eb = defined $e ? $e->[VEC_BITS] : undef;
197 if(select($rb,$wb,$eb,$t) > 0)
202 my $i = _max(defined $r ? scalar(@$r)-1 : 0,
203 defined $w ? scalar(@$w)-1 : 0,
204 defined $e ? scalar(@$e)-1 : 0);
206 for( ; $i >= FIRST_FD ; $i--)
208 my $j = $i - FIRST_FD;
210 if defined $rb && defined $r->[$i] && vec($rb, $j, 1);
212 if defined $wb && defined $w->[$i] && vec($wb, $j, 1);
214 if defined $eb && defined $e->[$i] && vec($eb, $j, 1);
217 @result = (\@r, \@w, \@e);
229 my $max = scalar(@$vec) - 1;
231 for ($i = FIRST_FD; $i <= $max; $i++)
233 next unless defined $vec->[$i];
235 if !defined($bits) || vec($bits, $i - FIRST_FD, 1);
246 IO::Select - OO interface to the select system call
252 $s = IO::Select->new();
255 $s->add($some_handle);
257 @ready = $s->can_read($timeout);
259 @ready = IO::Select->new(@handles)->can_read(0);
263 The C<IO::Select> package implements an object approach to the system C<select>
264 function call. It allows the user to see what IO handles, see L<IO::Handle>,
265 are ready for reading, writing or have an exception pending.
271 =item new ( [ HANDLES ] )
273 The constructor creates a new object and optionally initialises it with a set
282 =item add ( HANDLES )
284 Add the list of handles to the C<IO::Select> object. It is these values that
285 will be returned when an event occurs. C<IO::Select> keeps these values in a
286 cache which is indexed by the C<fileno> of the handle, so if more than one
287 handle with the same C<fileno> is specified then only the last one is cached.
289 Each handle can be an C<IO::Handle> object, an integer or an array
290 reference where the first element is an C<IO::Handle> or an integer.
292 =item remove ( HANDLES )
294 Remove all the given handles from the object. This method also works
295 by the C<fileno> of the handles. So the exact handles that were added
296 need not be passed, just handles that have an equivalent C<fileno>
298 =item exists ( HANDLE )
300 Returns a true value (actually the handle itself) if it is present.
301 Returns undef otherwise.
305 Return an array of all registered handles.
307 =item can_read ( [ TIMEOUT ] )
309 Return an array of handles that are ready for reading. C<TIMEOUT> is
310 the maximum amount of time to wait before returning an empty list, in
311 seconds, possibly fractional. If C<TIMEOUT> is not given and any
312 handles are registered then the call will block.
314 =item can_write ( [ TIMEOUT ] )
316 Same as C<can_read> except check for handles that can be written to.
318 =item has_exception ( [ TIMEOUT ] )
320 Same as C<can_read> except check for handles that have an exception
321 condition, for example pending out-of-band data.
325 Returns the number of handles that the object will check for when
326 one of the C<can_> methods is called or the object is passed to
327 the C<select> static method.
331 Return the bit string suitable as argument to the core select() call.
333 =item select ( READ, WRITE, EXCEPTION [, TIMEOUT ] )
335 C<select> is a static method, that is you call it with the package name
336 like C<new>. C<READ>, C<WRITE> and C<EXCEPTION> are either C<undef> or
337 C<IO::Select> objects. C<TIMEOUT> is optional and has the same effect as
338 for the core select call.
340 The result will be an array of 3 elements, each a reference to an array
341 which will hold the handles that are ready for reading, writing and have
342 exceptions respectively. Upon error an empty list is returned.
348 Here is a short example which shows how C<IO::Select> could be used
349 to write a server which communicates with several sockets while also
350 listening for more connections on a listen socket
355 $lsn = new IO::Socket::INET(Listen => 1, LocalPort => 8080);
356 $sel = new IO::Select( $lsn );
358 while(@ready = $sel->can_read) {
359 foreach $fh (@ready) {
361 # Create a new socket
368 # Maybe we have finished with the socket
377 Graham Barr. Currently maintained by the Perl Porters. Please report all
378 bugs to <perl5-porters@perl.org>.
382 Copyright (c) 1997-8 Graham Barr <gbarr@pobox.com>. All rights reserved.
383 This program is free software; you can redistribute it and/or
384 modify it under the same terms as Perl itself.