# Copyright (c) 1997-8 Graham Barr <gbarr@pobox.com>. All rights reserved.
# This program is free software; you can redistribute it and/or
# modify it under the same terms as Perl itself.
use vars
qw($VERSION @ISA);
@ISA = qw(Exporter); # This is only so we can do version checking
my $type = ref($self) || $self;
my $vec = bless [undef,0], $type;
shift->_update('add', @_);
shift->_update('remove', @_);
my $fno = $vec->_fileno(shift);
return undef unless defined $fno;
return unless defined $f;
$f = $f->[0] if ref($f) eq 'ARRAY';
($f =~ /^\d+$/) ?
$f : fileno($f);
my $add = shift eq 'add';
my $bits = $vec->[VEC_BITS
];
$bits = '' unless defined $bits;
my $fn = $vec->_fileno($f);
if (defined $vec->[$i]) {
$vec->[$i] = $f; # if array rest might be different, so we update
next unless defined $vec->[$i];
$vec->[VEC_BITS
] = $vec->[FD_COUNT
] ?
$bits : undef;
my $r = $vec->[VEC_BITS
];
defined($r) && (select($r,undef,undef,$timeout) > 0)
my $w = $vec->[VEC_BITS
];
defined($w) && (select(undef,$w,undef,$timeout) > 0)
my $e = $vec->[VEC_BITS
];
defined($e) && (select(undef,undef,$e,$timeout) > 0)
warnings
::warn("Call to deprecated method 'has_error', use 'has_exception'")
sub as_string
# for debugging
my $str = ref($vec) . ": ";
$str .= defined($bits) ?
unpack("b*", $bits) : "undef";
splice(@handles, 0, FIRST_FD
);
$str .= " " . (defined($_) ?
"$_" : "-");
if defined $_[0] && !ref($_[0]);
my $rb = defined $r ?
$r->[VEC_BITS
] : undef;
my $wb = defined $w ?
$w->[VEC_BITS
] : undef;
my $eb = defined $e ?
$e->[VEC_BITS
] : undef;
if(select($rb,$wb,$eb,$t) > 0)
my $i = _max
(defined $r ?
scalar(@
$r)-1 : 0,
defined $w ?
scalar(@
$w)-1 : 0,
defined $e ?
scalar(@
$e)-1 : 0);
for( ; $i >= FIRST_FD
; $i--)
if defined $rb && defined $r->[$i] && vec($rb, $j, 1);
if defined $wb && defined $w->[$i] && vec($wb, $j, 1);
if defined $eb && defined $e->[$i] && vec($eb, $j, 1);
@result = (\
@r, \
@w, \
@e);
my $max = scalar(@
$vec) - 1;
for ($i = FIRST_FD
; $i <= $max; $i++)
next unless defined $vec->[$i];
if !defined($bits) || vec($bits, $i - FIRST_FD
, 1);
IO::Select - OO interface to the select system call
@ready = $s->can_read($timeout);
@ready = IO::Select->new(@handles)->read(0);
The C<IO::Select> package implements an object approach to the system C<select>
function call. It allows the user to see what IO handles, see L<IO::Handle>,
are ready for reading, writing or have an error condition pending.
=item new ( [ HANDLES ] )
The constructor creates a new object and optionally initialises it with a set
Add the list of handles to the C<IO::Select> object. It is these values that
will be returned when an event occurs. C<IO::Select> keeps these values in a
cache which is indexed by the C<fileno> of the handle, so if more than one
handle with the same C<fileno> is specified then only the last one is cached.
Each handle can be an C<IO::Handle> object, an integer or an array
reference where the first element is an C<IO::Handle> or an integer.
Remove all the given handles from the object. This method also works
by the C<fileno> of the handles. So the exact handles that were added
need not be passed, just handles that have an equivalent C<fileno>
Returns a true value (actually the handle itself) if it is present.
Return an array of all registered handles.
=item can_read ( [ TIMEOUT ] )
Return an array of handles that are ready for reading. C<TIMEOUT> is
the maximum amount of time to wait before returning an empty list, in
seconds, possibly fractional. If C<TIMEOUT> is not given and any
handles are registered then the call will block.
=item can_write ( [ TIMEOUT ] )
Same as C<can_read> except check for handles that can be written to.
=item has_exception ( [ TIMEOUT ] )
Same as C<can_read> except check for handles that have an exception
condition, for example pending out-of-band data.
Returns the number of handles that the object will check for when
one of the C<can_> methods is called or the object is passed to
the C<select> static method.
Return the bit string suitable as argument to the core select() call.
=item select ( READ, WRITE, ERROR [, TIMEOUT ] )
C<select> is a static method, that is you call it with the package
name like C<new>. C<READ>, C<WRITE> and C<ERROR> are either C<undef>
or C<IO::Select> objects. C<TIMEOUT> is optional and has the same
effect as for the core select call.
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.
Here is a short example which shows how C<IO::Select> could be used
to write a server which communicates with several sockets while also
listening for more connections on a listen socket
$lsn = new IO::Socket::INET(Listen => 1, LocalPort => 8080);
$sel = new IO::Select( $lsn );
while(@ready = $sel->can_read) {
# Maybe we have finished with the socket
Graham Barr. Currently maintained by the Perl Porters. Please report all
bugs to <perl5-porters@perl.org>.
Copyright (c) 1997-8 Graham Barr <gbarr@pobox.com>. All rights reserved.
This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.