… | |
… | |
176 | =head2 I/O WATCHERS |
176 | =head2 I/O WATCHERS |
177 | |
177 | |
178 | You can create an I/O watcher by calling the C<< AnyEvent->io >> method |
178 | You can create an I/O watcher by calling the C<< AnyEvent->io >> method |
179 | with the following mandatory key-value pairs as arguments: |
179 | with the following mandatory key-value pairs as arguments: |
180 | |
180 | |
181 | C<fh> is the Perl I<file handle> (I<not> file descriptor, see below) to |
181 | C<fh> is the Perl I<file handle> (or a naked file descriptor) to watch |
182 | watch for events (AnyEvent might or might not keep a reference to this |
182 | for events (AnyEvent might or might not keep a reference to this file |
183 | file handle). Note that only file handles pointing to things for which |
183 | handle). Note that only file handles pointing to things for which |
184 | non-blocking operation makes sense are allowed. This includes sockets, |
184 | non-blocking operation makes sense are allowed. This includes sockets, |
185 | most character devices, pipes, fifos and so on, but not for example files |
185 | most character devices, pipes, fifos and so on, but not for example files |
186 | or block devices. |
186 | or block devices. |
187 | |
187 | |
188 | C<poll> must be a string that is either C<r> or C<w>, which creates a |
188 | C<poll> must be a string that is either C<r> or C<w>, which creates a |
… | |
… | |
208 | my $w; $w = AnyEvent->io (fh => \*STDIN, poll => 'r', cb => sub { |
208 | my $w; $w = AnyEvent->io (fh => \*STDIN, poll => 'r', cb => sub { |
209 | chomp (my $input = <STDIN>); |
209 | chomp (my $input = <STDIN>); |
210 | warn "read: $input\n"; |
210 | warn "read: $input\n"; |
211 | undef $w; |
211 | undef $w; |
212 | }); |
212 | }); |
213 | |
|
|
214 | =head3 GETTING A FILE HANDLE FROM A FILE DESCRIPTOR |
|
|
215 | |
|
|
216 | It is not uncommon to only have a file descriptor, while AnyEvent requires |
|
|
217 | a Perl file handle. |
|
|
218 | |
|
|
219 | There are basically two methods to convert a file descriptor into a file handle. If you own |
|
|
220 | the file descriptor, you can open it with C<&=>, as in: |
|
|
221 | |
|
|
222 | open my $fh, "<&=$fileno" or die "xxx: ยง!"; |
|
|
223 | |
|
|
224 | This will "own" the file descriptor, meaning that when C<$fh> is |
|
|
225 | destroyed, it will automatically close the C<$fileno>. Also, note that |
|
|
226 | the open mode (read, write, read/write) must correspond with how the |
|
|
227 | underlying file descriptor was opened. |
|
|
228 | |
|
|
229 | In many cases, taking over the file descriptor is now what you want, in |
|
|
230 | which case the only alternative is to dup the file descriptor: |
|
|
231 | |
|
|
232 | open my $fh, "<&$fileno" or die "xxx: $!"; |
|
|
233 | |
|
|
234 | This has the advantage of not closing the file descriptor and the |
|
|
235 | disadvantage of making a slow copy. |
|
|
236 | |
213 | |
237 | =head2 TIME WATCHERS |
214 | =head2 TIME WATCHERS |
238 | |
215 | |
239 | You can create a time watcher by calling the C<< AnyEvent->timer >> |
216 | You can create a time watcher by calling the C<< AnyEvent->timer >> |
240 | method with the following mandatory arguments: |
217 | method with the following mandatory arguments: |
… | |
… | |
1146 | # allow only one watcher per fd, so we dup it to get a different one). |
1123 | # allow only one watcher per fd, so we dup it to get a different one). |
1147 | sub _dupfh($$;$$) { |
1124 | sub _dupfh($$;$$) { |
1148 | my ($poll, $fh, $r, $w) = @_; |
1125 | my ($poll, $fh, $r, $w) = @_; |
1149 | |
1126 | |
1150 | # cygwin requires the fh mode to be matching, unix doesn't |
1127 | # cygwin requires the fh mode to be matching, unix doesn't |
1151 | my ($rw, $mode) = $poll eq "r" ? ($r, "<") |
1128 | my ($rw, $mode) = $poll eq "r" ? ($r, "<") : ($w, ">"); |
1152 | : $poll eq "w" ? ($w, ">") |
|
|
1153 | : Carp::croak "AnyEvent->io requires poll set to either 'r' or 'w'"; |
|
|
1154 | |
1129 | |
1155 | open my $fh2, "$mode&" . fileno $fh |
1130 | open my $fh2, "$mode&", $fh |
1156 | or die "cannot dup() filehandle: $!,"; |
1131 | or die "AnyEvent->io: cannot dup() filehandle in mode '$poll': $!,"; |
1157 | |
1132 | |
1158 | # we assume CLOEXEC is already set by perl in all important cases |
1133 | # we assume CLOEXEC is already set by perl in all important cases |
1159 | |
1134 | |
1160 | ($fh2, $rw) |
1135 | ($fh2, $rw) |
1161 | } |
1136 | } |