| 1 |
.\" -*- mode: troff; coding: utf-8 -*- |
| 2 |
.\" Automatically generated by Pod::Man 5.0102 (Pod::Simple 3.45) |
| 3 |
.\" |
| 4 |
.\" Standard preamble: |
| 5 |
.\" ======================================================================== |
| 6 |
.de Sp \" Vertical space (when we can't use .PP) |
| 7 |
.if t .sp .5v |
| 8 |
.if n .sp |
| 9 |
.. |
| 10 |
.de Vb \" Begin verbatim text |
| 11 |
.ft CW |
| 12 |
.nf |
| 13 |
.ne \\$1 |
| 14 |
.. |
| 15 |
.de Ve \" End verbatim text |
| 16 |
.ft R |
| 17 |
.fi |
| 18 |
.. |
| 19 |
.\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. |
| 20 |
.ie n \{\ |
| 21 |
. ds C` "" |
| 22 |
. ds C' "" |
| 23 |
'br\} |
| 24 |
.el\{\ |
| 25 |
. ds C` |
| 26 |
. ds C' |
| 27 |
'br\} |
| 28 |
.\" |
| 29 |
.\" Escape single quotes in literal strings from groff's Unicode transform. |
| 30 |
.ie \n(.g .ds Aq \(aq |
| 31 |
.el .ds Aq ' |
| 32 |
.\" |
| 33 |
.\" If the F register is >0, we'll generate index entries on stderr for |
| 34 |
.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index |
| 35 |
.\" entries marked with X<> in POD. Of course, you'll have to process the |
| 36 |
.\" output yourself in some meaningful fashion. |
| 37 |
.\" |
| 38 |
.\" Avoid warning from groff about undefined register 'F'. |
| 39 |
.de IX |
| 40 |
.. |
| 41 |
.nr rF 0 |
| 42 |
.if \n(.g .if rF .nr rF 1 |
| 43 |
.if (\n(rF:(\n(.g==0)) \{\ |
| 44 |
. if \nF \{\ |
| 45 |
. de IX |
| 46 |
. tm Index:\\$1\t\\n%\t"\\$2" |
| 47 |
.. |
| 48 |
. if !\nF==2 \{\ |
| 49 |
. nr % 0 |
| 50 |
. nr F 2 |
| 51 |
. \} |
| 52 |
. \} |
| 53 |
.\} |
| 54 |
.rr rF |
| 55 |
.\" ======================================================================== |
| 56 |
.\" |
| 57 |
.IX Title "IO::Select 3perl" |
| 58 |
.TH IO::Select 3perl 2025-11-16 "perl v5.40.1" "Perl Programmers Reference Guide" |
| 59 |
.\" For nroff, turn off justification. Always turn off hyphenation; it makes |
| 60 |
.\" way too many mistakes in technical documents. |
| 61 |
.if n .ad l |
| 62 |
.nh |
| 63 |
.SH NAME |
| 64 |
IO::Select \- OO interface to the "select" system call |
| 65 |
.SH SYNOPSIS |
| 66 |
.IX Header "SYNOPSIS" |
| 67 |
.Vb 1 |
| 68 |
\& use IO::Select; |
| 69 |
\& |
| 70 |
\& my $s = IO::Select\->new(); |
| 71 |
\& |
| 72 |
\& $s\->add(\e*STDIN); |
| 73 |
\& $s\->add($some_handle); |
| 74 |
\& |
| 75 |
\& my @ready1 = $s\->can_read($timeout); |
| 76 |
\& |
| 77 |
\& my @ready2 = IO::Select\->new(@handles)\->can_read(0); |
| 78 |
.Ve |
| 79 |
.SH DESCRIPTION |
| 80 |
.IX Header "DESCRIPTION" |
| 81 |
The \f(CW\*(C`IO::Select\*(C'\fR package implements an object approach to the system \f(CW\*(C`select\*(C'\fR |
| 82 |
function call. It allows the user to see what IO handles, see IO::Handle, |
| 83 |
are ready for reading, writing or have an exception pending. |
| 84 |
.SH CONSTRUCTOR |
| 85 |
.IX Header "CONSTRUCTOR" |
| 86 |
.IP "new ( [ HANDLES ] )" 4 |
| 87 |
.IX Item "new ( [ HANDLES ] )" |
| 88 |
The constructor creates a new object and optionally initialises it with a set |
| 89 |
of handles. |
| 90 |
.SH METHODS |
| 91 |
.IX Header "METHODS" |
| 92 |
.IP "add ( HANDLES )" 4 |
| 93 |
.IX Item "add ( HANDLES )" |
| 94 |
Add the list of handles to the \f(CW\*(C`IO::Select\*(C'\fR object. It is these values that |
| 95 |
will be returned when an event occurs. \f(CW\*(C`IO::Select\*(C'\fR keeps these values in a |
| 96 |
cache which is indexed by the \f(CW\*(C`fileno\*(C'\fR of the handle, so if more than one |
| 97 |
handle with the same \f(CW\*(C`fileno\*(C'\fR is specified then only the last one is cached. |
| 98 |
.Sp |
| 99 |
Each handle can be an \f(CW\*(C`IO::Handle\*(C'\fR object, an integer or an array |
| 100 |
reference where the first element is an \f(CW\*(C`IO::Handle\*(C'\fR or an integer. |
| 101 |
.IP "remove ( HANDLES )" 4 |
| 102 |
.IX Item "remove ( HANDLES )" |
| 103 |
Remove all the given handles from the object. This method also works |
| 104 |
by the \f(CW\*(C`fileno\*(C'\fR of the handles. So the exact handles that were added |
| 105 |
need not be passed, just handles that have an equivalent \f(CW\*(C`fileno\*(C'\fR |
| 106 |
.IP "exists ( HANDLE )" 4 |
| 107 |
.IX Item "exists ( HANDLE )" |
| 108 |
Returns a true value (actually the handle itself) if it is present. |
| 109 |
Returns undef otherwise. |
| 110 |
.IP handles 4 |
| 111 |
.IX Item "handles" |
| 112 |
Return an array of all registered handles. |
| 113 |
.IP "can_read ( [ TIMEOUT ] )" 4 |
| 114 |
.IX Item "can_read ( [ TIMEOUT ] )" |
| 115 |
Return an array of handles that are ready for reading. \f(CW\*(C`TIMEOUT\*(C'\fR is the |
| 116 |
maximum amount of time to wait before returning an empty list (with \f(CW$!\fR |
| 117 |
unchanged), in seconds, possibly fractional. If \f(CW\*(C`TIMEOUT\*(C'\fR is not given |
| 118 |
and any handles are registered then the call will block indefinitely. |
| 119 |
Upon error, an empty list is returned, with \f(CW$!\fR set to indicate the |
| 120 |
error. To distinguish between timeout and error, set \f(CW$!\fR to zero |
| 121 |
before calling this method, and check it after an empty list is returned. |
| 122 |
.IP "can_write ( [ TIMEOUT ] )" 4 |
| 123 |
.IX Item "can_write ( [ TIMEOUT ] )" |
| 124 |
Same as \f(CW\*(C`can_read\*(C'\fR except check for handles that can be written to. |
| 125 |
.IP "has_exception ( [ TIMEOUT ] )" 4 |
| 126 |
.IX Item "has_exception ( [ TIMEOUT ] )" |
| 127 |
Same as \f(CW\*(C`can_read\*(C'\fR except check for handles that have an exception |
| 128 |
condition, for example pending out-of-band data. |
| 129 |
.IP "count ()" 4 |
| 130 |
.IX Item "count ()" |
| 131 |
Returns the number of handles that the object will check for when |
| 132 |
one of the \f(CW\*(C`can_\*(C'\fR methods is called or the object is passed to |
| 133 |
the \f(CW\*(C`select\*(C'\fR static method. |
| 134 |
.IP \fBbits()\fR 4 |
| 135 |
.IX Item "bits()" |
| 136 |
Return the bit string suitable as argument to the core \fBselect()\fR call. |
| 137 |
.IP "select ( READ, WRITE, EXCEPTION [, TIMEOUT ] )" 4 |
| 138 |
.IX Item "select ( READ, WRITE, EXCEPTION [, TIMEOUT ] )" |
| 139 |
\&\f(CW\*(C`select\*(C'\fR is a static method, that is you call it with the package name |
| 140 |
like \f(CW\*(C`new\*(C'\fR. \f(CW\*(C`READ\*(C'\fR, \f(CW\*(C`WRITE\*(C'\fR and \f(CW\*(C`EXCEPTION\*(C'\fR are either \f(CW\*(C`undef\*(C'\fR or |
| 141 |
\&\f(CW\*(C`IO::Select\*(C'\fR objects. \f(CW\*(C`TIMEOUT\*(C'\fR is optional and has the same effect as |
| 142 |
for the core select call. |
| 143 |
.Sp |
| 144 |
If at least one handle is ready for the specified kind of operation, |
| 145 |
the result will be an array of 3 elements, each a reference to an array |
| 146 |
which will hold the handles that are ready for reading, writing and |
| 147 |
have exceptions respectively. Upon timeout, an empty list is returned, |
| 148 |
with \f(CW$!\fR unchanged. Upon error, an empty list is returned, with \f(CW$!\fR |
| 149 |
set to indicate the error. To distinguish between timeout and error, |
| 150 |
set \f(CW$!\fR to zero before calling this method, and check it after an |
| 151 |
empty list is returned. |
| 152 |
.SH EXAMPLE |
| 153 |
.IX Header "EXAMPLE" |
| 154 |
Here is a short example which shows how \f(CW\*(C`IO::Select\*(C'\fR could be used |
| 155 |
to write a server which communicates with several sockets while also |
| 156 |
listening for more connections on a listen socket |
| 157 |
.PP |
| 158 |
.Vb 2 |
| 159 |
\& use IO::Select; |
| 160 |
\& use IO::Socket; |
| 161 |
\& |
| 162 |
\& my $lsn = IO::Socket::INET\->new(Listen => 1, LocalPort => 8080); |
| 163 |
\& my $sel = IO::Select\->new( $lsn ); |
| 164 |
\& |
| 165 |
\& while(my @ready = $sel\->can_read) { |
| 166 |
\& foreach my $fh (@ready) { |
| 167 |
\& if($fh == $lsn) { |
| 168 |
\& # Create a new socket |
| 169 |
\& my $new = $lsn\->accept; |
| 170 |
\& $sel\->add($new); |
| 171 |
\& } |
| 172 |
\& else { |
| 173 |
\& # Process socket |
| 174 |
\& |
| 175 |
\& # Maybe we have finished with the socket |
| 176 |
\& $sel\->remove($fh); |
| 177 |
\& $fh\->close; |
| 178 |
\& } |
| 179 |
\& } |
| 180 |
\& } |
| 181 |
.Ve |
| 182 |
.SH AUTHOR |
| 183 |
.IX Header "AUTHOR" |
| 184 |
Graham Barr. Currently maintained by the Perl Porters. Please report all |
| 185 |
bugs at <https://github.com/Perl/perl5/issues>. |
| 186 |
.SH COPYRIGHT |
| 187 |
.IX Header "COPYRIGHT" |
| 188 |
Copyright (c) 1997\-8 Graham Barr <[email protected]>. All rights reserved. |
| 189 |
This program is free software; you can redistribute it and/or |
| 190 |
modify it under the same terms as Perl itself. |