… | |
… | |
89 | chomp (my $input = <STDIN>); |
89 | chomp (my $input = <STDIN>); |
90 | warn "read: $input\n"; |
90 | warn "read: $input\n"; |
91 | undef $w; |
91 | undef $w; |
92 | }); |
92 | }); |
93 | |
93 | |
94 | =head2 TIMER WATCHERS |
94 | =head2 TIME WATCHERS |
95 | |
95 | |
96 | You can create a timer watcher by calling the C<< AnyEvent->timer >> |
96 | You can create a time watcher by calling the C<< AnyEvent->timer >> |
97 | method with the following mandatory arguments: |
97 | method with the following mandatory arguments: |
98 | |
98 | |
99 | C<after> after how many seconds (fractions are supported) should the timer |
99 | C<after> after how many seconds (fractions are supported) should the timer |
100 | activate. C<cb> the callback to invoke. |
100 | activate. C<cb> the callback to invoke. |
101 | |
101 | |
… | |
… | |
154 | # do something such as adding a timer |
154 | # do something such as adding a timer |
155 | # or socket watcher the calls $result_ready->broadcast |
155 | # or socket watcher the calls $result_ready->broadcast |
156 | # when the "result" is ready. |
156 | # when the "result" is ready. |
157 | |
157 | |
158 | $result_ready->wait; |
158 | $result_ready->wait; |
|
|
159 | |
|
|
160 | =back |
|
|
161 | |
|
|
162 | =head2 SIGNAL WATCHERS |
|
|
163 | |
|
|
164 | You can listen for signals using a signal watcher, C<signal> is the signal |
|
|
165 | I<name> without any C<SIG> prefix. |
|
|
166 | |
|
|
167 | These watchers might use C<%SIG>, so programs overwriting those signals |
|
|
168 | directly will likely not work correctly. |
|
|
169 | |
|
|
170 | Example: exit on SIGINT |
|
|
171 | |
|
|
172 | my $w = AnyEvent->signal (signal => "INT", cb => sub { exit 1 }); |
|
|
173 | |
|
|
174 | =head1 GLOBALS |
|
|
175 | |
|
|
176 | =over 4 |
|
|
177 | |
|
|
178 | =item $AnyEvent::MODEL |
|
|
179 | |
|
|
180 | Contains C<undef> until the first watcher is being created. Then it |
|
|
181 | contains the event model that is being used, which is the name of the |
|
|
182 | Perl class implementing the model. This class is usually one of the |
|
|
183 | C<AnyEvent::Impl:xxx> modules, but can be any other class in the case |
|
|
184 | AnyEvent has been extended at runtime (e.g. in I<rxvt-unicode>). |
|
|
185 | |
|
|
186 | The known classes so far are: |
|
|
187 | |
|
|
188 | AnyEvent::Impl::Coro based on Coro::Event, best choise. |
|
|
189 | AnyEvent::Impl::Event based on Event, also best choice :) |
|
|
190 | AnyEvent::Impl::Glib based on Glib, second-best choice. |
|
|
191 | AnyEvent::Impl::Tk based on Tk, very bad choice. |
|
|
192 | AnyEvent::Impl::Perl pure-perl implementation, inefficient. |
|
|
193 | |
|
|
194 | =item AnyEvent::detect |
|
|
195 | |
|
|
196 | Returns C<$AnyEvent::MODEL>, forcing autodetection of the event model if |
|
|
197 | necessary. You should only call this function right before you would have |
|
|
198 | created an AnyEvent watcher anyway, that is, very late at runtime. |
159 | |
199 | |
160 | =back |
200 | =back |
161 | |
201 | |
162 | =head1 WHAT TO DO IN A MODULE |
202 | =head1 WHAT TO DO IN A MODULE |
163 | |
203 | |
… | |
… | |
192 | =cut |
232 | =cut |
193 | |
233 | |
194 | package AnyEvent; |
234 | package AnyEvent; |
195 | |
235 | |
196 | no warnings; |
236 | no warnings; |
197 | use strict 'vars'; |
237 | use strict; |
198 | use Carp; |
238 | use Carp; |
199 | |
239 | |
200 | our $VERSION = '2.0'; |
240 | our $VERSION = '2.5'; |
201 | our $MODEL; |
241 | our $MODEL; |
202 | |
242 | |
203 | our $AUTOLOAD; |
243 | our $AUTOLOAD; |
204 | our @ISA; |
244 | our @ISA; |
205 | |
245 | |
206 | our $verbose = $ENV{PERL_ANYEVENT_VERBOSE}*1; |
246 | our $verbose = $ENV{PERL_ANYEVENT_VERBOSE}*1; |
207 | |
247 | |
208 | our @REGISTRY; |
248 | our @REGISTRY; |
209 | |
249 | |
210 | my @models = ( |
250 | my @models = ( |
211 | [Coro::Event:: => AnyEvent::Impl::Coro::], |
251 | [Coro::Event:: => AnyEvent::Impl::Coro::], |
212 | [Event:: => AnyEvent::Impl::Event::], |
252 | [Event:: => AnyEvent::Impl::Event::], |
213 | [Glib:: => AnyEvent::Impl::Glib::], |
253 | [Glib:: => AnyEvent::Impl::Glib::], |
214 | [Tk:: => AnyEvent::Impl::Tk::], |
254 | [Tk:: => AnyEvent::Impl::Tk::], |
215 | [AnyEvent::Impl::Perl:: => AnyEvent::Impl::Perl::], |
255 | [AnyEvent::Impl::Perl:: => AnyEvent::Impl::Perl::], |
216 | ); |
256 | ); |
217 | |
257 | |
218 | our %method = map +($_ => 1), qw(io timer condvar broadcast wait DESTROY); |
258 | our %method = map +($_ => 1), qw(io timer condvar broadcast wait signal one_event DESTROY); |
219 | |
259 | |
220 | sub AUTOLOAD { |
260 | sub detect() { |
221 | $AUTOLOAD =~ s/.*://; |
|
|
222 | |
|
|
223 | $method{$AUTOLOAD} |
|
|
224 | or croak "$AUTOLOAD: not a valid method for AnyEvent objects"; |
|
|
225 | |
|
|
226 | unless ($MODEL) { |
261 | unless ($MODEL) { |
|
|
262 | no strict 'refs'; |
|
|
263 | |
227 | # check for already loaded models |
264 | # check for already loaded models |
228 | for (@REGISTRY, @models) { |
265 | for (@REGISTRY, @models) { |
229 | my ($package, $model) = @$_; |
266 | my ($package, $model) = @$_; |
230 | if (${"$package\::VERSION"} > 0) { |
267 | if (${"$package\::VERSION"} > 0) { |
231 | if (eval "require $model") { |
268 | if (eval "require $model") { |
… | |
… | |
247 | last; |
284 | last; |
248 | } |
285 | } |
249 | } |
286 | } |
250 | |
287 | |
251 | $MODEL |
288 | $MODEL |
252 | or die "No event module selected for AnyEvent and autodetect failed. Install any one of these modules: Coro, Event, Glib or Tk."; |
289 | or die "No event module selected for AnyEvent and autodetect failed. Install any one of these modules: Event (or Coro+Event), Glib or Tk."; |
253 | } |
290 | } |
|
|
291 | |
|
|
292 | unshift @ISA, $MODEL; |
|
|
293 | push @{"$MODEL\::ISA"}, "AnyEvent::Base"; |
254 | } |
294 | } |
255 | |
295 | |
256 | @ISA = $MODEL; |
296 | $MODEL |
|
|
297 | } |
|
|
298 | |
|
|
299 | sub AUTOLOAD { |
|
|
300 | (my $func = $AUTOLOAD) =~ s/.*://; |
|
|
301 | |
|
|
302 | $method{$func} |
|
|
303 | or croak "$func: not a valid method for AnyEvent objects"; |
|
|
304 | |
|
|
305 | detect unless $MODEL; |
257 | |
306 | |
258 | my $class = shift; |
307 | my $class = shift; |
259 | $class->$AUTOLOAD (@_); |
308 | $class->$func (@_); |
|
|
309 | } |
|
|
310 | |
|
|
311 | package AnyEvent::Base; |
|
|
312 | |
|
|
313 | # default implementation for signal |
|
|
314 | |
|
|
315 | our %SIG_CB; |
|
|
316 | |
|
|
317 | sub signal { |
|
|
318 | my (undef, %arg) = @_; |
|
|
319 | |
|
|
320 | my $signal = uc $arg{signal} |
|
|
321 | or Carp::croak "required option 'signal' is missing"; |
|
|
322 | |
|
|
323 | my $w = bless [$signal, $arg{cb}], "AnyEvent::Base::Signal"; |
|
|
324 | |
|
|
325 | $SIG_CB{$signal}{$arg{cb}} = $arg{cb}; |
|
|
326 | $SIG{$signal} ||= sub { |
|
|
327 | $_->() for values %{ $SIG_CB{$signal} }; |
|
|
328 | }; |
|
|
329 | |
|
|
330 | $w |
|
|
331 | } |
|
|
332 | |
|
|
333 | sub AnyEvent::Base::Signal::DESTROY { |
|
|
334 | my ($signal, $cb) = @{$_[0]}; |
|
|
335 | |
|
|
336 | delete $SIG_CB{$signal}{$cb}; |
|
|
337 | |
|
|
338 | $SIG{$signal} = 'DEFAULT' unless keys %{ $SIG_CB{$signal} }; |
260 | } |
339 | } |
261 | |
340 | |
262 | =head1 SUPPLYING YOUR OWN EVENT MODEL INTERFACE |
341 | =head1 SUPPLYING YOUR OWN EVENT MODEL INTERFACE |
263 | |
342 | |
264 | If you need to support another event library which isn't directly |
343 | If you need to support another event library which isn't directly |
… | |
… | |
275 | This tells AnyEvent to (literally) use the C<urxvt::anyevent::> |
354 | This tells AnyEvent to (literally) use the C<urxvt::anyevent::> |
276 | package/class when it finds the C<urxvt> package/module is loaded. When |
355 | package/class when it finds the C<urxvt> package/module is loaded. When |
277 | AnyEvent is loaded and asked to find a suitable event model, it will |
356 | AnyEvent is loaded and asked to find a suitable event model, it will |
278 | first check for the presence of urxvt. |
357 | first check for the presence of urxvt. |
279 | |
358 | |
280 | The class should prove implementations for all watcher types (see |
359 | The class should provide implementations for all watcher types (see |
281 | L<AnyEvent::Impl::Event> (source code), L<AnyEvent::Impl::Glib> |
360 | L<AnyEvent::Impl::Event> (source code), L<AnyEvent::Impl::Glib> |
282 | (Source code) and so on for actual examples, use C<perldoc -m |
361 | (Source code) and so on for actual examples, use C<perldoc -m |
283 | AnyEvent::Impl::Glib> to see the sources). |
362 | AnyEvent::Impl::Glib> to see the sources). |
284 | |
363 | |
285 | The above isn't fictitious, the I<rxvt-unicode> (a.k.a. urxvt) |
364 | The above isn't fictitious, the I<rxvt-unicode> (a.k.a. urxvt) |