… | |
… | |
4 | |
4 | |
5 | Event, Coro, Glib, Tk - various supported event loops |
5 | Event, Coro, Glib, Tk - various supported event loops |
6 | |
6 | |
7 | =head1 SYNOPSIS |
7 | =head1 SYNOPSIS |
8 | |
8 | |
9 | use 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 |
17 | you can have one r + one w or one rw watcher, not any more (limitation by |
18 | watcher, not any more. |
18 | Tk). |
19 | |
19 | |
|
|
20 | * the C<$poll_got> passed to the handler needs to be checked by looking |
|
|
21 | for single characters (e.g. with a regex), as it can contain more event |
|
|
22 | types than were requested (e.g. a 'w' watcher might generate 'rw' events, |
|
|
23 | limitation 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, |
21 | the filehandle exists. |
26 | the 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 |
|
|
35 | operation (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 |
36 | a condvar for any "request" or "event" your module might create, C<< |
42 | a 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 |
38 | C<< ->wait >> for it. See the examples below. |
44 | C<< ->wait >> for it. See the examples below. |
39 | |
45 | |
40 | =head1 DESCRIPTION |
46 | =head1 DESCRIPTION |
… | |
… | |
62 | |
68 | |
63 | no warnings; |
69 | no warnings; |
64 | use strict 'vars'; |
70 | use strict 'vars'; |
65 | use Carp; |
71 | use Carp; |
66 | |
72 | |
67 | our $VERSION = 0.3; |
73 | our $VERSION = '1.02'; |
68 | our $MODEL; |
74 | our $MODEL; |
69 | |
75 | |
70 | our $AUTOLOAD; |
76 | our $AUTOLOAD; |
71 | our @ISA; |
77 | our @ISA; |
72 | |
78 | |
|
|
79 | our $verbose = $ENV{PERL_ANYEVENT_VERBOSE}*1; |
|
|
80 | |
|
|
81 | our @REGISTRY; |
|
|
82 | |
73 | my @models = ( |
83 | my @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 | |
80 | our %method = map +($_ => 1), qw(io timer condvar broadcast wait cancel DESTROY); |
90 | our %method = map +($_ => 1), qw(io timer condvar broadcast wait cancel DESTROY); |
81 | |
91 | |
82 | sub AUTOLOAD { |
92 | sub 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 | |
|
|
138 | If you need to support another event library which isn't directly |
|
|
139 | supported by AnyEvent, you can supply your own interface to it by |
|
|
140 | pushing, before the first watch gets created, the package name of |
|
|
141 | the event module and the package name of the interface to use onto |
|
|
142 | C<@AnyEvent::REGISTRY>. You can do that before and even without loading |
|
|
143 | AnyEvent. |
|
|
144 | |
|
|
145 | Example: |
|
|
146 | |
|
|
147 | push @AnyEvent::REGISTRY, [urxvt => urxvt::anyevent::]; |
|
|
148 | |
|
|
149 | This tells AnyEvent to (literally) use the C<urxvt::anyevent::> module |
|
|
150 | when it finds the C<urxvt> module is loaded. When AnyEvent is loaded and |
|
|
151 | requested to find a suitable event model, it will first check for the |
|
|
152 | urxvt module. |
|
|
153 | |
|
|
154 | The above isn't fictitious, the I<rxvt-unicode> (a.k.a. urxvt) uses |
|
|
155 | the above line exactly. An interface isn't included in AnyEvent |
|
|
156 | because it doesn't make sense outside the embedded interpreter inside |
|
|
157 | I<rxvt-unicode>, and it is updated and maintained as part of the |
|
|
158 | I<rxvt-unicode> distribution. |
|
|
159 | |
|
|
160 | =head1 ENVIRONMENT VARIABLES |
|
|
161 | |
|
|
162 | The following environment variables are used by this module: |
|
|
163 | |
|
|
164 | C<PERL_ANYEVENT_VERBOSE> when set to C<2> or higher, reports which event |
|
|
165 | model gets used. |
119 | |
166 | |
120 | =head1 EXAMPLE |
167 | =head1 EXAMPLE |
121 | |
168 | |
122 | The following program uses an io watcher to read data from stdin, a timer |
169 | The following program uses an io watcher to read data from stdin, a timer |
123 | to display a message once per second, and a condvar to exit the program |
170 | to display a message once per second, and a condvar to exit the program |