… | |
… | |
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. Multiple signals events can be clumped |
|
|
166 | together into one callback invocation, and callback invocation might or |
|
|
167 | might not be asynchronous. |
|
|
168 | |
|
|
169 | These watchers might use C<%SIG>, so programs overwriting those signals |
|
|
170 | directly will likely not work correctly. |
|
|
171 | |
|
|
172 | Example: exit on SIGINT |
|
|
173 | |
|
|
174 | my $w = AnyEvent->signal (signal => "INT", cb => sub { exit 1 }); |
|
|
175 | |
|
|
176 | =head2 CHILD PROCESS WATCHERS |
|
|
177 | |
|
|
178 | You can also listen for the status of a child process specified by the |
|
|
179 | C<pid> argument. The watcher will only trigger once. This works by |
|
|
180 | installing a signal handler for C<SIGCHLD>. |
|
|
181 | |
|
|
182 | Example: wait for pid 1333 |
|
|
183 | |
|
|
184 | my $w = AnyEvent->child (pid => 1333, cb => sub { warn "exit status $?" }); |
|
|
185 | |
|
|
186 | =head1 GLOBALS |
|
|
187 | |
|
|
188 | =over 4 |
|
|
189 | |
|
|
190 | =item $AnyEvent::MODEL |
|
|
191 | |
|
|
192 | Contains C<undef> until the first watcher is being created. Then it |
|
|
193 | contains the event model that is being used, which is the name of the |
|
|
194 | Perl class implementing the model. This class is usually one of the |
|
|
195 | C<AnyEvent::Impl:xxx> modules, but can be any other class in the case |
|
|
196 | AnyEvent has been extended at runtime (e.g. in I<rxvt-unicode>). |
|
|
197 | |
|
|
198 | The known classes so far are: |
|
|
199 | |
|
|
200 | EV::AnyEvent based on EV (an interface to libev, best choice) |
|
|
201 | AnyEvent::Impl::Coro based on Coro::Event, second best choice. |
|
|
202 | AnyEvent::Impl::Event based on Event, also second best choice :) |
|
|
203 | AnyEvent::Impl::Glib based on Glib, second-best choice. |
|
|
204 | AnyEvent::Impl::Tk based on Tk, very bad choice. |
|
|
205 | AnyEvent::Impl::Perl pure-perl implementation, inefficient. |
|
|
206 | |
|
|
207 | =item AnyEvent::detect |
|
|
208 | |
|
|
209 | Returns C<$AnyEvent::MODEL>, forcing autodetection of the event model if |
|
|
210 | necessary. You should only call this function right before you would have |
|
|
211 | created an AnyEvent watcher anyway, that is, very late at runtime. |
159 | |
212 | |
160 | =back |
213 | =back |
161 | |
214 | |
162 | =head1 WHAT TO DO IN A MODULE |
215 | =head1 WHAT TO DO IN A MODULE |
163 | |
216 | |
… | |
… | |
192 | =cut |
245 | =cut |
193 | |
246 | |
194 | package AnyEvent; |
247 | package AnyEvent; |
195 | |
248 | |
196 | no warnings; |
249 | no warnings; |
197 | use strict 'vars'; |
250 | use strict; |
|
|
251 | |
198 | use Carp; |
252 | use Carp; |
199 | |
253 | |
200 | our $VERSION = '2.0'; |
254 | our $VERSION = '2.55'; |
201 | our $MODEL; |
255 | our $MODEL; |
202 | |
256 | |
203 | our $AUTOLOAD; |
257 | our $AUTOLOAD; |
204 | our @ISA; |
258 | our @ISA; |
205 | |
259 | |
206 | our $verbose = $ENV{PERL_ANYEVENT_VERBOSE}*1; |
260 | our $verbose = $ENV{PERL_ANYEVENT_VERBOSE}*1; |
207 | |
261 | |
208 | our @REGISTRY; |
262 | our @REGISTRY; |
209 | |
263 | |
210 | my @models = ( |
264 | my @models = ( |
211 | [Coro::Event:: => AnyEvent::Impl::Coro::], |
265 | [Coro::Event:: => AnyEvent::Impl::Coro::], |
|
|
266 | [EV:: => EV::AnyEvent::], |
212 | [Event:: => AnyEvent::Impl::Event::], |
267 | [Event:: => AnyEvent::Impl::Event::], |
213 | [Glib:: => AnyEvent::Impl::Glib::], |
268 | [Glib:: => AnyEvent::Impl::Glib::], |
214 | [Tk:: => AnyEvent::Impl::Tk::], |
269 | [Tk:: => AnyEvent::Impl::Tk::], |
215 | [AnyEvent::Impl::Perl:: => AnyEvent::Impl::Perl::], |
270 | [AnyEvent::Impl::Perl:: => AnyEvent::Impl::Perl::], |
216 | ); |
271 | ); |
217 | |
272 | |
218 | our %method = map +($_ => 1), qw(io timer condvar broadcast wait DESTROY); |
273 | our %method = map +($_ => 1), qw(io timer condvar broadcast wait signal one_event DESTROY); |
219 | |
274 | |
220 | sub AUTOLOAD { |
275 | sub detect() { |
221 | $AUTOLOAD =~ s/.*://; |
|
|
222 | |
|
|
223 | $method{$AUTOLOAD} |
|
|
224 | or croak "$AUTOLOAD: not a valid method for AnyEvent objects"; |
|
|
225 | |
|
|
226 | unless ($MODEL) { |
276 | unless ($MODEL) { |
|
|
277 | no strict 'refs'; |
|
|
278 | |
227 | # check for already loaded models |
279 | # check for already loaded models |
228 | for (@REGISTRY, @models) { |
280 | for (@REGISTRY, @models) { |
229 | my ($package, $model) = @$_; |
281 | my ($package, $model) = @$_; |
230 | if (${"$package\::VERSION"} > 0) { |
282 | if (${"$package\::VERSION"} > 0) { |
231 | if (eval "require $model") { |
283 | if (eval "require $model") { |
… | |
… | |
239 | unless ($MODEL) { |
291 | unless ($MODEL) { |
240 | # try to load a model |
292 | # try to load a model |
241 | |
293 | |
242 | for (@REGISTRY, @models) { |
294 | for (@REGISTRY, @models) { |
243 | my ($package, $model) = @$_; |
295 | my ($package, $model) = @$_; |
|
|
296 | if (eval "require $package" |
|
|
297 | and ${"$package\::VERSION"} > 0 |
244 | if (eval "require $model") { |
298 | and eval "require $model") { |
245 | $MODEL = $model; |
299 | $MODEL = $model; |
246 | warn "AnyEvent: autoprobed and loaded model '$model', using it.\n" if $verbose > 1; |
300 | warn "AnyEvent: autoprobed and loaded model '$model', using it.\n" if $verbose > 1; |
247 | last; |
301 | last; |
248 | } |
302 | } |
249 | } |
303 | } |
250 | |
304 | |
251 | $MODEL |
305 | $MODEL |
252 | or die "No event module selected for AnyEvent and autodetect failed. Install any one of these modules: Coro, Event, Glib or Tk."; |
306 | 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 | } |
307 | } |
|
|
308 | |
|
|
309 | unshift @ISA, $MODEL; |
|
|
310 | push @{"$MODEL\::ISA"}, "AnyEvent::Base"; |
254 | } |
311 | } |
255 | |
312 | |
256 | @ISA = $MODEL; |
313 | $MODEL |
|
|
314 | } |
|
|
315 | |
|
|
316 | sub AUTOLOAD { |
|
|
317 | (my $func = $AUTOLOAD) =~ s/.*://; |
|
|
318 | |
|
|
319 | $method{$func} |
|
|
320 | or croak "$func: not a valid method for AnyEvent objects"; |
|
|
321 | |
|
|
322 | detect unless $MODEL; |
257 | |
323 | |
258 | my $class = shift; |
324 | my $class = shift; |
259 | $class->$AUTOLOAD (@_); |
325 | $class->$func (@_); |
|
|
326 | } |
|
|
327 | |
|
|
328 | package AnyEvent::Base; |
|
|
329 | |
|
|
330 | # default implementation for ->condvar, ->wait, ->broadcast |
|
|
331 | |
|
|
332 | sub condvar { |
|
|
333 | bless \my $flag, "AnyEvent::Base::CondVar" |
|
|
334 | } |
|
|
335 | |
|
|
336 | sub AnyEvent::Base::CondVar::broadcast { |
|
|
337 | ${$_[0]}++; |
|
|
338 | } |
|
|
339 | |
|
|
340 | sub AnyEvent::Base::CondVar::wait { |
|
|
341 | AnyEvent->one_event while !${$_[0]}; |
|
|
342 | } |
|
|
343 | |
|
|
344 | # default implementation for ->signal |
|
|
345 | |
|
|
346 | our %SIG_CB; |
|
|
347 | |
|
|
348 | sub signal { |
|
|
349 | my (undef, %arg) = @_; |
|
|
350 | |
|
|
351 | my $signal = uc $arg{signal} |
|
|
352 | or Carp::croak "required option 'signal' is missing"; |
|
|
353 | |
|
|
354 | $SIG_CB{$signal}{$arg{cb}} = $arg{cb}; |
|
|
355 | $SIG{$signal} ||= sub { |
|
|
356 | $_->() for values %{ $SIG_CB{$signal} || {} }; |
|
|
357 | }; |
|
|
358 | |
|
|
359 | bless [$signal, $arg{cb}], "AnyEvent::Base::Signal" |
|
|
360 | } |
|
|
361 | |
|
|
362 | sub AnyEvent::Base::Signal::DESTROY { |
|
|
363 | my ($signal, $cb) = @{$_[0]}; |
|
|
364 | |
|
|
365 | delete $SIG_CB{$signal}{$cb}; |
|
|
366 | |
|
|
367 | $SIG{$signal} = 'DEFAULT' unless keys %{ $SIG_CB{$signal} }; |
|
|
368 | } |
|
|
369 | |
|
|
370 | # default implementation for ->child |
|
|
371 | |
|
|
372 | our %PID_CB; |
|
|
373 | our $CHLD_W; |
|
|
374 | our $PID_IDLE; |
|
|
375 | our $WNOHANG; |
|
|
376 | |
|
|
377 | sub _child_wait { |
|
|
378 | while (0 < (my $pid = waitpid -1, $WNOHANG)) { |
|
|
379 | $_->() for values %{ (delete $PID_CB{$pid}) || {} }; |
|
|
380 | } |
|
|
381 | |
|
|
382 | undef $PID_IDLE; |
|
|
383 | } |
|
|
384 | |
|
|
385 | sub child { |
|
|
386 | my (undef, %arg) = @_; |
|
|
387 | |
|
|
388 | my $pid = uc $arg{pid} |
|
|
389 | or Carp::croak "required option 'pid' is missing"; |
|
|
390 | |
|
|
391 | $PID_CB{$pid}{$arg{cb}} = $arg{cb}; |
|
|
392 | |
|
|
393 | unless ($WNOHANG) { |
|
|
394 | $WNOHANG = eval { require POSIX; &POSIX::WNOHANG } || 1; |
|
|
395 | } |
|
|
396 | |
|
|
397 | unless ($CHLD_W) { |
|
|
398 | $CHLD_W = AnyEvent->signal (signal => 'CHLD', cb => \&_child_wait); |
|
|
399 | # child could be a zombie already |
|
|
400 | $PID_IDLE ||= AnyEvent->timer (after => 0, cb => \&_child_wait); |
|
|
401 | } |
|
|
402 | |
|
|
403 | bless [$pid, $arg{cb}], "AnyEvent::Base::Child" |
|
|
404 | } |
|
|
405 | |
|
|
406 | sub AnyEvent::Base::Child::DESTROY { |
|
|
407 | my ($pid, $cb) = @{$_[0]}; |
|
|
408 | |
|
|
409 | delete $PID_CB{$pid}{$cb}; |
|
|
410 | delete $PID_CB{$pid} unless keys %{ $PID_CB{$pid} }; |
|
|
411 | |
|
|
412 | undef $CHLD_W unless keys %PID_CB; |
260 | } |
413 | } |
261 | |
414 | |
262 | =head1 SUPPLYING YOUR OWN EVENT MODEL INTERFACE |
415 | =head1 SUPPLYING YOUR OWN EVENT MODEL INTERFACE |
263 | |
416 | |
264 | If you need to support another event library which isn't directly |
417 | If you need to support another event library which isn't directly |
… | |
… | |
275 | This tells AnyEvent to (literally) use the C<urxvt::anyevent::> |
428 | This tells AnyEvent to (literally) use the C<urxvt::anyevent::> |
276 | package/class when it finds the C<urxvt> package/module is loaded. When |
429 | 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 |
430 | AnyEvent is loaded and asked to find a suitable event model, it will |
278 | first check for the presence of urxvt. |
431 | first check for the presence of urxvt. |
279 | |
432 | |
280 | The class should prove implementations for all watcher types (see |
433 | The class should provide implementations for all watcher types (see |
281 | L<AnyEvent::Impl::Event> (source code), L<AnyEvent::Impl::Glib> |
434 | L<AnyEvent::Impl::Event> (source code), L<AnyEvent::Impl::Glib> |
282 | (Source code) and so on for actual examples, use C<perldoc -m |
435 | (Source code) and so on for actual examples, use C<perldoc -m |
283 | AnyEvent::Impl::Glib> to see the sources). |
436 | AnyEvent::Impl::Glib> to see the sources). |
284 | |
437 | |
285 | The above isn't fictitious, the I<rxvt-unicode> (a.k.a. urxvt) |
438 | The above isn't fictitious, the I<rxvt-unicode> (a.k.a. urxvt) |
… | |
… | |
289 | I<rxvt-unicode> distribution. |
442 | I<rxvt-unicode> distribution. |
290 | |
443 | |
291 | I<rxvt-unicode> also cheats a bit by not providing blocking access to |
444 | I<rxvt-unicode> also cheats a bit by not providing blocking access to |
292 | condition variables: code blocking while waiting for a condition will |
445 | condition variables: code blocking while waiting for a condition will |
293 | C<die>. This still works with most modules/usages, and blocking calls must |
446 | C<die>. This still works with most modules/usages, and blocking calls must |
294 | not be in an interactive appliation, so it makes sense. |
447 | not be in an interactive application, so it makes sense. |
295 | |
448 | |
296 | =head1 ENVIRONMENT VARIABLES |
449 | =head1 ENVIRONMENT VARIABLES |
297 | |
450 | |
298 | The following environment variables are used by this module: |
451 | The following environment variables are used by this module: |
299 | |
452 | |