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);
77 next unless defined $fn;
78 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 next unless defined $vec->[$i];
90 vec($bits, $fn, 1) = 0;
95 $vec->[VEC_BITS
] = $vec->[FD_COUNT
] ?
$bits : undef;
103 my $r = $vec->[VEC_BITS
];
105 defined($r) && (select($r,undef,undef,$timeout) > 0)
114 my $w = $vec->[VEC_BITS
];
116 defined($w) && (select(undef,$w,undef,$timeout) > 0)
125 my $e = $vec->[VEC_BITS
];
127 defined($e) && (select(undef,undef,$e,$timeout) > 0)
134 warnings
::warn("Call to depreciated method 'has_error', use 'has_exception'")
135 if warnings
::enabled
();
151 sub as_string
# for debugging
154 my $str = ref($vec) . ": ";
155 my $bits = $vec->bits;
156 my $count = $vec->count;
157 $str .= defined($bits) ?
unpack("b*", $bits) : "undef";
160 splice(@handles, 0, FIRST_FD
);
162 $str .= " " . (defined($_) ?
"$_" : "-");
182 if defined $_[0] && !ref($_[0]);
184 my($r,$w,$e,$t) = @_;
187 my $rb = defined $r ?
$r->[VEC_BITS
] : undef;
188 my $wb = defined $w ?
$w->[VEC_BITS
] : undef;
189 my $eb = defined $e ?
$e->[VEC_BITS
] : undef;
191 if(select($rb,$wb,$eb,$t) > 0)
196 my $i = _max
(defined $r ?
scalar(@
$r)-1 : 0,
197 defined $w ?
scalar(@
$w)-1 : 0,
198 defined $e ?
scalar(@
$e)-1 : 0);
200 for( ; $i >= FIRST_FD
; $i--)
202 my $j = $i - FIRST_FD
;
204 if defined $rb && defined $r->[$i] && vec($rb, $j, 1);
206 if defined $wb && defined $w->[$i] && vec($wb, $j, 1);
208 if defined $eb && defined $e->[$i] && vec($eb, $j, 1);
211 @result = (\
@r, \
@w, \
@e);
223 my $max = scalar(@
$vec) - 1;
225 for ($i = FIRST_FD
; $i <= $max; $i++)
227 next unless defined $vec->[$i];
229 if !defined($bits) || vec($bits, $i - FIRST_FD
, 1);
240 IO::Select - OO interface to the select system call
246 $s = IO::Select->new();
249 $s->add($some_handle);
251 @ready = $s->can_read($timeout);
253 @ready = IO::Select->new(@handles)->read(0);
257 The C<IO::Select> package implements an object approach to the system C<select>
258 function call. It allows the user to see what IO handles, see L<IO::Handle>,
259 are ready for reading, writing or have an error condition pending.
265 =item new ( [ HANDLES ] )
267 The constructor creates a new object and optionally initialises it with a set
276 =item add ( HANDLES )
278 Add the list of handles to the C<IO::Select> object. It is these values that
279 will be returned when an event occurs. C<IO::Select> keeps these values in a
280 cache which is indexed by the C<fileno> of the handle, so if more than one
281 handle with the same C<fileno> is specified then only the last one is cached.
283 Each handle can be an C<IO::Handle> object, an integer or an array
284 reference where the first element is a C<IO::Handle> or an integer.
286 =item remove ( HANDLES )
288 Remove all the given handles from the object. This method also works
289 by the C<fileno> of the handles. So the exact handles that were added
290 need not be passed, just handles that have an equivalent C<fileno>
292 =item exists ( HANDLE )
294 Returns a true value (actually the handle itself) if it is present.
295 Returns undef otherwise.
299 Return an array of all registered handles.
301 =item can_read ( [ TIMEOUT ] )
303 Return an array of handles that are ready for reading. C<TIMEOUT> is
304 the maximum amount of time to wait before returning an empty list, in
305 seconds, possibly fractional. If C<TIMEOUT> is not given and any
306 handles are registered then the call will block.
308 =item can_write ( [ TIMEOUT ] )
310 Same as C<can_read> except check for handles that can be written to.
312 =item has_exception ( [ TIMEOUT ] )
314 Same as C<can_read> except check for handles that have an exception
315 condition, for example pending out-of-band data.
319 Returns the number of handles that the object will check for when
320 one of the C<can_> methods is called or the object is passed to
321 the C<select> static method.
325 Return the bit string suitable as argument to the core select() call.
327 =item select ( READ, WRITE, ERROR [, TIMEOUT ] )
329 C<select> is a static method, that is you call it with the package
330 name like C<new>. C<READ>, C<WRITE> and C<ERROR> are either C<undef>
331 or C<IO::Select> objects. C<TIMEOUT> is optional and has the same
332 effect as for the core select call.
334 The result will be an array of 3 elements, each a reference to an array
335 which will hold the handles that are ready for reading, writing and have
336 error conditions respectively. Upon error an empty array is returned.
342 Here is a short example which shows how C<IO::Select> could be used
343 to write a server which communicates with several sockets while also
344 listening for more connections on a listen socket
349 $lsn = new IO::Socket::INET(Listen => 1, LocalPort => 8080);
350 $sel = new IO::Select( $lsn );
352 while(@ready = $sel->can_read) {
353 foreach $fh (@ready) {
355 # Create a new socket
362 # Maybe we have finished with the socket
371 Graham Barr. Currently maintained by the Perl Porters. Please report all
372 bugs to <perl5-porters@perl.org>.
376 Copyright (c) 1997-8 Graham Barr <gbarr@pobox.com>. All rights reserved.
377 This program is free software; you can redistribute it and/or
378 modify it under the same terms as Perl itself.