IO::Select(3pm)
hey bestie π welcome to IO::Select(3pm) from the Perl Programmers Reference Guide β yes, the one and only OO glow-up for select β¨
πΈ NAME πΈ
IO::Select β an OO interface to the select system call π«Ά
β¨ SYNOPSIS β¨
Okay so hereβs the quick-start group chat:
use IO::Select;
my $s = IO::Select->new();
$s->add(\*STDIN);
$s->add($some_handle);
my @ready1 = $s->can_read($timeout);
my @ready2 = IO::Select->new(@handles)->can_read(0);
π¦ DESCRIPTION π¦
The IO::Select package implements an object approach to the system select function call. It lets you see which IO handles β see IO::Handle β are ready for reading, ready for writing, or have an exception pending. Thatβs the vibe check: read-ready, write-ready, and exception-pending handles. Iconic, honestly π
π CONSTRUCTOR π
new ( [ HANDLES ] )
This constructor creates a brand-new object and optionally initialises it with a set of handles. Main character energy from the jump π«Ά
π METHODS π
add ( HANDLES )
Adds the list of handles to the IO::Select object. These are the values that will be returned when an event occurs. IO::Select keeps these values in a cache indexed by the fileno of the handle, so if more than one handle with the same fileno is specified, then only the last one is cached.
Each handle can be an IO::Handle object, an integer, or an array reference where the first element is an IO::Handle or an integer. No cap, thatβs the whole menu π
remove ( HANDLES )
Removes all the given handles from the object. This method also works by the fileno of the handles. So the exact handles that were added need not be passed β just handles that have an equivalent fileno.
exists ( HANDLE )
Returns a true value (actually the handle itself) if it is present. Returns undef otherwise.
handles
Returns an array of all registered handles. The full roster, bestie π
can_read ( [ TIMEOUT ] )
Returns an array of handles that are ready for reading. TIMEOUT is the maximum amount of time to wait before returning an empty list (with $! unchanged), in seconds, possibly fractional. If TIMEOUT is not given and any handles are registered, then the call will block indefinitely. Upon error, an empty list is returned, with $! set to indicate the error. To distinguish between timeout and error, set $! to zero before calling this method, and check it after an empty list is returned.
lowkey important: timeout gives you an empty list with$!unchanged, error gives you an empty list with$!set. Set$!to zero first if you need to tell them apart π
can_write ( [ TIMEOUT ] )
Same as can_read except check for handles that can be written to.
has_exception ( [ TIMEOUT ] )
Same as can_read except check for handles that have an exception condition, for example pending out-of-band data.
count ()
Returns the number of handles that the object will check for when one of the can_ methods is called or the object is passed to the select static method.
bits()
Returns the bit string suitable as argument to the core select() call. Very βgive me the raw materialβ of it all β¨
select ( READ, WRITE, EXCEPTION [, TIMEOUT ] )
select is a static method, that is you call it with the package name like new. READ, WRITE and EXCEPTION are either undef or IO::Select objects. TIMEOUT is optional and has the same effect as for the core select call.
If at least one handle is ready for the specified kind of operation, 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 exceptions respectively. Upon timeout, an empty list is returned, with $! unchanged. Upon error, an empty list is returned, with $! set to indicate the error. To distinguish between timeout and error, set $! to zero before calling this method, and check it after an empty list is returned.
π EXAMPLE π
Hereβs a short example which shows how IO::Select could be used to write a server which communicates with several sockets while also listening for more connections on a listen socket π¦
use IO::Select;
use IO::Socket;
my $lsn = IO::Socket::INET->new(Listen => 1, LocalPort => 8080);
my $sel = IO::Select->new( $lsn );
while(my @ready = $sel->can_read) {
foreach my $fh (@ready) {
if($fh == $lsn) {
# Create a new socket
my $new = $lsn->accept;
$sel->add($new);
}
else {
# Process socket
# Maybe we have finished with the socket
$sel->remove($fh);
$fh->close;
}
}
}
π AUTHOR π
Graham Barr. Currently maintained by the Perl Porters. Please report all bugs at <https://github.com/Perl/perl5/issues>.
π COPYRIGHT π
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.
okay bestie, thatβs the whole IO::Select(3pm) page π«Ά this one is from perl v5.42.3, dated 2026-11-03 β now go make your filehandles behave π
β¨
π¦ where this page comes from
- alpine 3.24: perl-doc 5.42.3-r0 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms