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" is
57 the maximum amount of time to wait before returning an empty list,
58 in seconds, possibly fractional. If "TIMEOUT" is not given and any
59 handles are registered then the call will block.
60
61 can_write ( [ TIMEOUT ] )
62 Same as "can_read" except check for handles that can be written to.
63
64 has_exception ( [ TIMEOUT ] )
65 Same as "can_read" except check for handles that have an exception
66 condition, for example pending out-of-band data.
67
68 count ()
69 Returns the number of handles that the object will check for when
70 one of the "can_" methods is called or the object is passed to the
71 "select" static method.
72
73 bits()
74 Return the bit string suitable as argument to the core select()
75 call.
76
77 select ( READ, WRITE, EXCEPTION [, TIMEOUT ] )
78 "select" is a static method, that is you call it with the package
79 name like "new". "READ", "WRITE" and "EXCEPTION" are either "undef"
80 or "IO::Select" objects. "TIMEOUT" is optional and has the same
81 effect as for the core select call.
82
83 The result will be an array of 3 elements, each a reference to an
84 array which will hold the handles that are ready for reading,
85 writing and have exceptions respectively. Upon error an empty list
86 is returned.
87
89 Here is a short example which shows how "IO::Select" could be used to
90 write a server which communicates with several sockets while also
91 listening for more connections on a listen socket
92
93 use IO::Select;
94 use IO::Socket;
95
96 $lsn = IO::Socket::INET->new(Listen => 1, LocalPort => 8080);
97 $sel = IO::Select->new( $lsn );
98
99 while(@ready = $sel->can_read) {
100 foreach $fh (@ready) {
101 if($fh == $lsn) {
102 # Create a new socket
103 $new = $lsn->accept;
104 $sel->add($new);
105 }
106 else {
107 # Process socket
108
109 # Maybe we have finished with the socket
110 $sel->remove($fh);
111 $fh->close;
112 }
113 }
114 }
115
117 Graham Barr. Currently maintained by the Perl Porters. Please report
118 all bugs to <perlbug@perl.org>.
119
121 Copyright (c) 1997-8 Graham Barr <gbarr@pobox.com>. All rights
122 reserved. This program is free software; you can redistribute it
123 and/or modify it under the same terms as Perl itself.
124
125
126
127perl v5.16.3 2013-03-04 IO::Select(3pm)