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