1IO::Select(3pm) Perl Programmers Reference Guide IO::Select(3pm)
2
3
4
6 IO::Select - OO interface to the select system call
7
9 use IO::Select;
10
11 $s = IO::Select->new();
12
13 $s->add(\*STDIN);
14 $s->add($some_handle);
15
16 @ready = $s->can_read($timeout);
17
18 @ready = IO::Select->new(@handles)->can_read(0);
19
21 The "IO::Select" package implements an object approach to the system
22 "select" function call. It allows the user to see what IO handles, see
23 IO::Handle, are ready for reading, writing or have an exception
24 pending.
25
27 new ( [ HANDLES ] )
28 The constructor creates a new object and optionally initialises it
29 with a set of handles.
30
32 add ( HANDLES )
33 Add the list of handles to the "IO::Select" object. It is these
34 values that will be returned when an event occurs. "IO::Select"
35 keeps these values in a cache which is indexed by the "fileno" of
36 the handle, so if more than one handle with the same "fileno" is
37 specified then only the last one is cached.
38
39 Each handle can be an "IO::Handle" object, an integer or an array
40 reference where the first element is an "IO::Handle" or an integer.
41
42 remove ( HANDLES )
43 Remove all the given handles from the object. This method also
44 works by the "fileno" of the handles. So the exact handles that
45 were added need not be passed, just handles that have an equivalent
46 "fileno"
47
48 exists ( HANDLE )
49 Returns a true value (actually the handle itself) if it is present.
50 Returns undef otherwise.
51
52 handles
53 Return an array of all registered handles.
54
55 can_read ( [ TIMEOUT ] )
56 Return an array of handles that are ready for reading. "TIMEOUT"
57 is the maximum amount of time to wait before returning an empty
58 list (with $! unchanged), in seconds, possibly fractional. If
59 "TIMEOUT" is not given and any handles are registered then the call
60 will block indefinitely. Upon error, an empty list is returned,
61 with $! set to indicate the error. To distinguish between timeout
62 and error, set $! to zero before calling this method, and check it
63 after an empty list is returned.
64
65 can_write ( [ TIMEOUT ] )
66 Same as "can_read" except check for handles that can be written to.
67
68 has_exception ( [ TIMEOUT ] )
69 Same as "can_read" except check for handles that have an exception
70 condition, for example pending out-of-band data.
71
72 count ()
73 Returns the number of handles that the object will check for when
74 one of the "can_" methods is called or the object is passed to the
75 "select" static method.
76
77 bits()
78 Return the bit string suitable as argument to the core select()
79 call.
80
81 select ( READ, WRITE, EXCEPTION [, TIMEOUT ] )
82 "select" is a static method, that is you call it with the package
83 name like "new". "READ", "WRITE" and "EXCEPTION" are either "undef"
84 or "IO::Select" objects. "TIMEOUT" is optional and has the same
85 effect as for the core select call.
86
87 If at least one handle is ready for the specified kind of
88 operation, the result will be an array of 3 elements, each a
89 reference to an array which will hold the handles that are ready
90 for reading, writing and have exceptions respectively. Upon
91 timeout, an empty list is returned, with $! unchanged. Upon error,
92 an empty list is returned, with $! set to indicate the error. To
93 distinguish between timeout and error, set $! to zero before
94 calling this method, and check it after an empty list is returned.
95
97 Here is a short example which shows how "IO::Select" could be used to
98 write a server which communicates with several sockets while also
99 listening for more connections on a listen socket
100
101 use IO::Select;
102 use IO::Socket;
103
104 $lsn = IO::Socket::INET->new(Listen => 1, LocalPort => 8080);
105 $sel = IO::Select->new( $lsn );
106
107 while(@ready = $sel->can_read) {
108 foreach $fh (@ready) {
109 if($fh == $lsn) {
110 # Create a new socket
111 $new = $lsn->accept;
112 $sel->add($new);
113 }
114 else {
115 # Process socket
116
117 # Maybe we have finished with the socket
118 $sel->remove($fh);
119 $fh->close;
120 }
121 }
122 }
123
125 Graham Barr. Currently maintained by the Perl Porters. Please report
126 all bugs to <perlbug@perl.org>.
127
129 Copyright (c) 1997-8 Graham Barr <gbarr@pobox.com>. All rights
130 reserved. This program is free software; you can redistribute it
131 and/or modify it under the same terms as Perl itself.
132
133
134
135perl v5.32.1 2021-05-31 IO::Select(3pm)