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.6 by root, Mon Dec 19 17:03:29 2005 UTC vs.
Revision 1.11 by root, Thu Jul 20 01:32:11 2006 UTC

4 4
5Event, Coro, Glib, Tk - various supported event loops 5Event, Coro, Glib, Tk - various supported event loops
6 6
7=head1 SYNOPSIS 7=head1 SYNOPSIS
8 8
9use AnyEvent; 9 use AnyEvent;
10 10
11 my $w = AnyEvent->io (fh => ..., poll => "[rw]+", cb => sub { 11 my $w = AnyEvent->io (fh => ..., poll => "[rw]+", cb => sub {
12 my ($poll_got) = @_; 12 my ($poll_got) = @_;
13 ... 13 ...
14 }); 14 });
15 15
16- only one io watcher per $fh and $poll type is allowed 16* only one io watcher per $fh and $poll type is allowed (i.e. on a socket
17(i.e. on a socket you can have one r + one w or one rw 17you can have one r + one w or one rw watcher, not any more (limitation by
18watcher, not any more. 18Tk).
19 19
20* the C<$poll_got> passed to the handler needs to be checked by looking
21for single characters (e.g. with a regex), as it can contain more event
22types than were requested (e.g. a 'w' watcher might generate 'rw' events,
23limitation by Glib).
24
20- AnyEvent will keep filehandles alive, so as long as the watcher exists, 25* AnyEvent will keep filehandles alive, so as long as the watcher exists,
21the filehandle exists. 26the filehandle exists.
22 27
23 my $w = AnyEvent->timer (after => $seconds, cb => sub { 28 my $w = AnyEvent->timer (after => $seconds, cb => sub {
24 ... 29 ...
25 }); 30 });
26 31
27- io and time watchers get canceled whenever $w is destroyed, so keep a copy 32* io and time watchers get canceled whenever $w is destroyed, so keep a copy
28 33
29- timers can only be used once and must be recreated for repeated operation 34* timers can only be used once and must be recreated for repeated
35operation (limitation by Glib and Tk).
30 36
31 my $w = AnyEvent->condvar; # kind of main loop replacement 37 my $w = AnyEvent->condvar; # kind of main loop replacement
32 $w->wait; # enters main loop till $condvar gets ->broadcast 38 $w->wait; # enters main loop till $condvar gets ->broadcast
33 $w->broadcast; # wake up current and all future wait's 39 $w->broadcast; # wake up current and all future wait's
34 40
35- condvars are used to give blocking behaviour when neccessary. Create 41* condvars are used to give blocking behaviour when neccessary. Create
36a condvar for any "request" or "event" your module might create, C<< 42a condvar for any "request" or "event" your module might create, C<<
37->broadcast >> it when the event happens and provide a function that calls 43->broadcast >> it when the event happens and provide a function that calls
38C<< ->wait >> for it. See the examples below. 44C<< ->wait >> for it. See the examples below.
39 45
40=head1 DESCRIPTION 46=head1 DESCRIPTION
62 68
63no warnings; 69no warnings;
64use strict 'vars'; 70use strict 'vars';
65use Carp; 71use Carp;
66 72
67our $VERSION = 0.3; 73our $VERSION = '1.02';
68our $MODEL; 74our $MODEL;
69 75
70our $AUTOLOAD; 76our $AUTOLOAD;
71our @ISA; 77our @ISA;
72 78
79our $verbose = $ENV{PERL_ANYEVENT_VERBOSE}*1;
80
81our @REGISTRY;
82
73my @models = ( 83my @models = (
74 [Coro => Coro::Event::], 84 [Coro::Event:: => AnyEvent::Impl::Coro::],
75 [Event => Event::], 85 [Event:: => AnyEvent::Impl::Event::],
76 [Glib => Glib::], 86 [Glib:: => AnyEvent::Impl::Glib::],
77 [Tk => Tk::], 87 [Tk:: => AnyEvent::Impl::Tk::],
78); 88);
79 89
80our %method = map +($_ => 1), qw(io timer condvar broadcast wait cancel DESTROY); 90our %method = map +($_ => 1), qw(io timer condvar broadcast wait cancel DESTROY);
81 91
82sub AUTOLOAD { 92sub AUTOLOAD {
85 $method{$AUTOLOAD} 95 $method{$AUTOLOAD}
86 or croak "$AUTOLOAD: not a valid method for AnyEvent objects"; 96 or croak "$AUTOLOAD: not a valid method for AnyEvent objects";
87 97
88 unless ($MODEL) { 98 unless ($MODEL) {
89 # check for already loaded models 99 # check for already loaded models
90 for (@models) { 100 for (@REGISTRY, @models) {
91 my ($model, $package) = @$_; 101 my ($package, $model) = @$_;
92 if (scalar keys %{ *{"$package\::"} }) { 102 if (${"$package\::VERSION"} > 0) {
93 eval "require AnyEvent::Impl::$model"; 103 if (eval "require $model") {
94 last if $MODEL; 104 $MODEL = $model;
105 warn "AnyEvent: found model '$model', using it.\n" if $verbose > 1;
106 last;
107 }
95 } 108 }
96 } 109 }
97 110
98 unless ($MODEL) { 111 unless ($MODEL) {
99 # try to load a model 112 # try to load a model
100 113
101 for (@models) { 114 for (@REGISTRY, @models) {
102 my ($model, $package) = @$_; 115 my ($package, $model) = @$_;
103 eval "require AnyEvent::Impl::$model"; 116 if (eval "require $model") {
104 last if $MODEL; 117 $MODEL = $model;
118 warn "AnyEvent: autoprobed and loaded model '$model', using it.\n" if $verbose > 1;
119 last;
120 }
105 } 121 }
106 122
107 $MODEL 123 $MODEL
108 or die "No event module selected for AnyEvent and autodetect failed. Install any one of these modules: Coro, Event, Glib or Tk."; 124 or die "No event module selected for AnyEvent and autodetect failed. Install any one of these modules: Coro, Event, Glib or Tk.";
109 } 125 }
114 my $class = shift; 130 my $class = shift;
115 $class->$AUTOLOAD (@_); 131 $class->$AUTOLOAD (@_);
116} 132}
117 133
118=back 134=back
135
136=head1 SUPPLYING YOUR OWN EVENT MODEL INTERFACE
137
138If you need to support another event library which isn't directly
139supported by AnyEvent, you can supply your own interface to it by
140pushing, before the first watcher gets created, the package name of
141the event module and the package name of the interface to use onto
142C<@AnyEvent::REGISTRY>. You can do that before and even without loading
143AnyEvent.
144
145Example:
146
147 push @AnyEvent::REGISTRY, [urxvt => urxvt::anyevent::];
148
149This tells AnyEvent to (literally) use the C<urxvt::anyevent::> module
150when it finds the C<urxvt> module is loaded. When AnyEvent is loaded and
151requested to find a suitable event model, it will first check for the
152urxvt module.
153
154The above isn't fictitious, the I<rxvt-unicode> (a.k.a. urxvt) uses
155the above line exactly. An interface isn't included in AnyEvent
156because it doesn't make sense outside the embedded interpreter inside
157I<rxvt-unicode>, and it is updated and maintained as part of the
158I<rxvt-unicode> distribution.
159
160=head1 ENVIRONMENT VARIABLES
161
162The following environment variables are used by this module:
163
164C<PERL_ANYEVENT_VERBOSE> when set to C<2> or higher, reports which event
165model gets used.
119 166
120=head1 EXAMPLE 167=head1 EXAMPLE
121 168
122The following program uses an io watcher to read data from stdin, a timer 169The following program uses an io watcher to read data from stdin, a timer
123to display a message once per second, and a condvar to exit the program 170to display a message once per second, and a condvar to exit the program

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines