ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/lib/AnyEvent.pm
(Generate patch)

Comparing AnyEvent/lib/AnyEvent.pm (file contents):
Revision 1.227 by root, Mon Jul 6 23:42:24 2009 UTC vs.
Revision 1.228 by root, Wed Jul 8 01:11:12 2009 UTC

176=head2 I/O WATCHERS 176=head2 I/O WATCHERS
177 177
178You can create an I/O watcher by calling the C<< AnyEvent->io >> method 178You can create an I/O watcher by calling the C<< AnyEvent->io >> method
179with the following mandatory key-value pairs as arguments: 179with the following mandatory key-value pairs as arguments:
180 180
181C<fh> is the Perl I<file handle> (I<not> file descriptor) to watch 181C<fh> is the Perl I<file handle> (I<not> file descriptor, see below) to
182for events (AnyEvent might or might not keep a reference to this file 182watch for events (AnyEvent might or might not keep a reference to this
183handle). Note that only file handles pointing to things for which 183file handle). Note that only file handles pointing to things for which
184non-blocking operation makes sense are allowed. This includes sockets, 184non-blocking operation makes sense are allowed. This includes sockets,
185most character devices, pipes, fifos and so on, but not for example files 185most character devices, pipes, fifos and so on, but not for example files
186or block devices. 186or block devices.
187 187
188C<poll> must be a string that is either C<r> or C<w>, which creates a 188C<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
216It is not uncommon to only have a file descriptor, while AnyEvent requires
217a Perl file handle.
218
219There are basically two methods to convert a file descriptor into a file handle. If you own
220the file descriptor, you can open it with C<&=>, as in:
221
222 open my $fh, "<&=$fileno" or die "xxx: ยง!";
223
224This will "own" the file descriptor, meaning that when C<$fh> is
225destroyed, it will automatically close the C<$fileno>. Also, note that
226the open mode (read, write, read/write) must correspond with how the
227underlying file descriptor was opened.
228
229In many cases, taking over the file descriptor is now what you want, in
230which case the only alternative is to dup the file descriptor:
231
232 open my $fh, "<&$fileno" or die "xxx: $!";
233
234This has the advantage of not closing the file descriptor and the
235disadvantage of making a slow copy.
213 236
214=head2 TIME WATCHERS 237=head2 TIME WATCHERS
215 238
216You can create a time watcher by calling the C<< AnyEvent->timer >> 239You can create a time watcher by calling the C<< AnyEvent->timer >>
217method with the following mandatory arguments: 240method with the following mandatory arguments:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines