--- AnyEvent/lib/AnyEvent.pm 2008/05/10 00:45:18 1.109 +++ AnyEvent/lib/AnyEvent.pm 2008/07/08 19:50:25 1.164 @@ -17,8 +17,14 @@ }); my $w = AnyEvent->condvar; # stores whether a condition was flagged - $w->wait; # enters "main loop" till $condvar gets ->send - $w->send; # wake up current and all future wait's + $w->send; # wake up current and all future recv's + $w->recv; # enters "main loop" till $condvar gets ->send + +=head1 INTRODUCTION/TUTORIAL + +This manpage is mainly a reference manual. If you are interested +in a tutorial or some gentle introduction, have a look at the +L manpage. =head1 WHY YOU SHOULD USE THIS MODULE (OR NOT) @@ -50,7 +56,7 @@ AnyEvent is different: AnyEvent + POE works fine. AnyEvent + Glib works fine. AnyEvent + Tk works fine etc. etc. but none of these work together -with the rest: POE + IO::Async? no go. Tk + Event? no go. Again: if +with the rest: POE + IO::Async? No go. Tk + Event? No go. Again: if your module uses one of those, every user of your module has to use it, too. But if your module uses AnyEvent, it works transparently with all event models it supports (including stuff like POE and IO::Async, as long @@ -59,12 +65,18 @@ In addition to being free of having to use I, AnyEvent also is free of bloat and policy: with POE or similar -modules, you get an enourmous amount of code and strict rules you have to +modules, you get an enormous amount of code and strict rules you have to follow. AnyEvent, on the other hand, is lean and up to the point, by only offering the functionality that is necessary, in as thin as a wrapper as technically possible. -Of course, if you want lots of policy (this can arguably be somewhat +Of course, AnyEvent comes with a big (and fully optional!) toolbox +of useful functionality, such as an asynchronous DNS resolver, 100% +non-blocking connects (even with TLS/SSL, IPv6 and on broken platforms +such as Windows) and lots of real-world knowledge and workarounds for +platform bugs and differences. + +Now, if you I lots of policy (this can arguably be somewhat useful) and you want to force your users to use the one and only event model, you should I use this module. @@ -104,13 +116,13 @@ The pure-perl implementation of AnyEvent is called C. Like other event modules you can load it -explicitly. +explicitly and enjoy the high availability of that event loop :) =head1 WATCHERS AnyEvent has the central concept of a I, which is an object that stores relevant data for each kind of event you are waiting for, such as -the callback to call, the filehandle to watch, etc. +the callback to call, the file handle to watch, etc. These watchers are normal Perl objects with normal Perl lifetime. After creating a watcher it will immediately "watch" for events and invoke the @@ -128,10 +140,10 @@ An any way to achieve that is this pattern: - my $w; $w = AnyEvent->type (arg => value ..., cb => sub { - # you can use $w here, for example to undef it - undef $w; - }); + my $w; $w = AnyEvent->type (arg => value ..., cb => sub { + # you can use $w here, for example to undef it + undef $w; + }); Note that C combination. This is necessary because in Perl, my variables are only visible after the statement in which they are @@ -160,9 +172,9 @@ always use non-blocking calls when reading/writing from/to your file handles. -Example: +Example: wait for readability of STDIN, then read a line and disable the +watcher. - # wait for readability of STDIN, then read a line and disable the watcher my $w; $w = AnyEvent->io (fh => \*STDIN, poll => 'r', cb => sub { chomp (my $input = ); warn "read: $input\n"; @@ -182,13 +194,17 @@ presence is undefined and you cannot rely on them. Portable AnyEvent callbacks cannot use arguments passed to time watcher callbacks. -The timer callback will be invoked at most once: if you want a repeating -timer you have to create a new watcher (this is a limitation by both Tk -and Glib). +The callback will normally be invoked once only. If you specify another +parameter, C, as a positive number, then the callback will be +invoked regularly at that interval (in fractional seconds) after the first +invocation. + +The callback will be rescheduled before invoking the callback, but no +attempt is done to avoid timer drift in most backends, so the interval is +only approximate. -Example: +Example: fire an event after 7.7 seconds. - # fire an event after 7.7 seconds my $w = AnyEvent->timer (after => 7.7, cb => sub { warn "timeout\n"; }); @@ -196,19 +212,12 @@ # to cancel the timer: undef $w; -Example 2: +Example 2: fire an event after 0.5 seconds, then roughly every second. - # fire an event after 0.5 seconds, then roughly every second - my $w; - - my $cb = sub { - # cancel the old timer while creating a new one - $w = AnyEvent->timer (after => 1, cb => $cb); + my $w = AnyEvent->timer (after => 0.5, interval => 1, cb => sub { + warn "timeout\n"; }; - # start the "loop" by creating the first watcher - $w = AnyEvent->timer (after => 0.5, cb => $cb); - =head3 TIMING ISSUES There are two ways to handle timers: based on real time (relative, "fire @@ -229,6 +238,69 @@ AnyEvent always prefers relative timers, if available, matching the AnyEvent API. +AnyEvent has two additional methods that return the "current time": + +=over 4 + +=item AnyEvent->time + +This returns the "current wallclock time" as a fractional number of +seconds since the Epoch (the same thing as C