--- AnyEvent/README 2005/12/04 09:44:32 1.3 +++ AnyEvent/README 2006/01/08 04:41:08 1.5 @@ -4,34 +4,40 @@ Event, Coro, Glib, Tk - various supported event loops SYNOPSIS - use AnyEvent; + use AnyEvent; my $w = AnyEvent->io (fh => ..., poll => "[rw]+", cb => sub { my ($poll_got) = @_; ... }); - - only one io watcher per $fh and $poll type is allowed (i.e. on a - socket you can have one r + one w or one rw watcher, not any more. + * only one io watcher per $fh and $poll type is allowed (i.e. on a + socket you can have one r + one w or one rw watcher, not any more + (limitation by Tk). + + * the $poll_got passed to the handler needs to be checked by looking for + single characters (e.g. with a regex), as it can contain more event + types than were requested (e.g. a 'w' watcher might generate 'rw' + events, limitation by Glib). - - AnyEvent will keep filehandles alive, so as long as the watcher + * AnyEvent will keep filehandles alive, so as long as the watcher exists, the filehandle exists. my $w = AnyEvent->timer (after => $seconds, cb => sub { ... }); - - io and time watchers get canceled whenever $w is destroyed, so keep a + * io and time watchers get canceled whenever $w is destroyed, so keep a copy - - timers can only be used once and must be recreated for repeated - operation + * timers can only be used once and must be recreated for repeated + operation (limitation by Glib and Tk). my $w = AnyEvent->condvar; # kind of main loop replacement $w->wait; # enters main loop till $condvar gets ->broadcast $w->broadcast; # wake up current and all future wait's - - condvars are used to give blocking behaviour when neccessary. Create a + * condvars are used to give blocking behaviour when neccessary. Create a condvar for any "request" or "event" your module might create, "->broadcast" it when the event happens and provide a function that calls "->wait" for it. See the examples below. @@ -52,6 +58,35 @@ order given. The first one that could be successfully loaded will be used. If still none could be found, it will issue an error. +SUPPLYING YOUR OWN EVENT MODEL INTERFACE + If you need to support another event library which isn't directly + supported by AnyEvent, you can supply your own interface to it by + pushing, before the first watch gets created, the package name of the + event module and the package name of the interface to use onto + @AnyEvent::REGISTRY. You can do that before and even without loading + AnyEvent. + + Example: + + push @AnyEvent::REGISTRY, [urxvt => urxvt::anyevent::]; + + This tells AnyEvent to (literally) use the "urxvt::anyevent::" module + when it finds the "urxvt" module is loaded. When AnyEvent is loaded and + requested to find a suitable event model, it will first check for the + urxvt module. + + The above isn't fictitious, the *rxvt-unicode* (a.k.a. urxvt) uses the + above line exactly. An interface isn't included in AnyEvent because it + doesn't make sense outside the embedded interpreter inside + *rxvt-unicode*, and it is updated and maintained as part of the + *rxvt-unicode* distribution. + +ENVIRONMENT VARIABLES + The following environment variables are used by this module: + + "PERL_ANYEVENT_VERBOSE" when set to 2 or higher, reports which event + model gets used. + EXAMPLE The following program uses an io watcher to read data from stdin, a timer to display a message once per second, and a condvar to exit the @@ -118,7 +153,7 @@ and !$!{EINPROGRESS} and Carp::croak "unable to connect: $!\n"; - Then it creates a write-watcher which gets called wehnever an error + Then it creates a write-watcher which gets called whenever an error occurs or the connection succeeds: $txn->{w} = AnyEvent->io (fh => $txn->{fh}, poll => 'w', cb => sub { $txn->fh_ready_w }); @@ -145,6 +180,7 @@ if (end-of-file or data complete) { $txn->{result} = $txn->{buf}; $txn->{finished}->broadcast; + $txb->{cb}->($txn) of $txn->{cb}; # also call callback } The "result" method, finally, just waits for the finished signal (if the @@ -152,7 +188,7 @@ the data: $txn->{finished}->wait; - return $txn->{buf}; + return $txn->{result}; The actual code goes further and collects all errors ("die"s, exceptions) that occured during request processing. The "result" method