… | |
… | |
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. |
|
|
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 | |
162 | =head1 GLOBALS |
174 | =head1 GLOBALS |
163 | |
175 | |
164 | =over 4 |
176 | =over 4 |
165 | |
177 | |
166 | =item $AnyEvent::MODEL |
178 | =item $AnyEvent::MODEL |
… | |
… | |
176 | AnyEvent::Impl::Coro based on Coro::Event, best choise. |
188 | AnyEvent::Impl::Coro based on Coro::Event, best choise. |
177 | AnyEvent::Impl::Event based on Event, also best choice :) |
189 | AnyEvent::Impl::Event based on Event, also best choice :) |
178 | AnyEvent::Impl::Glib based on Glib, second-best choice. |
190 | AnyEvent::Impl::Glib based on Glib, second-best choice. |
179 | AnyEvent::Impl::Tk based on Tk, very bad choice. |
191 | AnyEvent::Impl::Tk based on Tk, very bad choice. |
180 | AnyEvent::Impl::Perl pure-perl implementation, inefficient. |
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. |
181 | |
199 | |
182 | =back |
200 | =back |
183 | |
201 | |
184 | =head1 WHAT TO DO IN A MODULE |
202 | =head1 WHAT TO DO IN A MODULE |
185 | |
203 | |
… | |
… | |
214 | =cut |
232 | =cut |
215 | |
233 | |
216 | package AnyEvent; |
234 | package AnyEvent; |
217 | |
235 | |
218 | no warnings; |
236 | no warnings; |
219 | use strict 'vars'; |
237 | use strict; |
220 | use Carp; |
238 | use Carp; |
221 | |
239 | |
222 | our $VERSION = '2.1'; |
240 | our $VERSION = '2.5'; |
223 | our $MODEL; |
241 | our $MODEL; |
224 | |
242 | |
225 | our $AUTOLOAD; |
243 | our $AUTOLOAD; |
226 | our @ISA; |
244 | our @ISA; |
227 | |
245 | |
… | |
… | |
235 | [Glib:: => AnyEvent::Impl::Glib::], |
253 | [Glib:: => AnyEvent::Impl::Glib::], |
236 | [Tk:: => AnyEvent::Impl::Tk::], |
254 | [Tk:: => AnyEvent::Impl::Tk::], |
237 | [AnyEvent::Impl::Perl:: => AnyEvent::Impl::Perl::], |
255 | [AnyEvent::Impl::Perl:: => AnyEvent::Impl::Perl::], |
238 | ); |
256 | ); |
239 | |
257 | |
240 | 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); |
241 | |
259 | |
242 | sub AUTOLOAD { |
260 | sub detect() { |
243 | (my $func = $AUTOLOAD) =~ s/.*://; |
|
|
244 | |
|
|
245 | $method{$func} |
|
|
246 | or croak "$func: not a valid method for AnyEvent objects"; |
|
|
247 | |
|
|
248 | unless ($MODEL) { |
261 | unless ($MODEL) { |
|
|
262 | no strict 'refs'; |
|
|
263 | |
249 | # check for already loaded models |
264 | # check for already loaded models |
250 | for (@REGISTRY, @models) { |
265 | for (@REGISTRY, @models) { |
251 | my ($package, $model) = @$_; |
266 | my ($package, $model) = @$_; |
252 | if (${"$package\::VERSION"} > 0) { |
267 | if (${"$package\::VERSION"} > 0) { |
253 | if (eval "require $model") { |
268 | if (eval "require $model") { |
… | |
… | |
269 | last; |
284 | last; |
270 | } |
285 | } |
271 | } |
286 | } |
272 | |
287 | |
273 | $MODEL |
288 | $MODEL |
274 | 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."; |
275 | } |
290 | } |
|
|
291 | |
|
|
292 | unshift @ISA, $MODEL; |
|
|
293 | push @{"$MODEL\::ISA"}, "AnyEvent::Base"; |
276 | } |
294 | } |
277 | |
295 | |
278 | @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; |
279 | |
306 | |
280 | my $class = shift; |
307 | my $class = shift; |
281 | $class->$func (@_); |
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} }; |
282 | } |
339 | } |
283 | |
340 | |
284 | =head1 SUPPLYING YOUR OWN EVENT MODEL INTERFACE |
341 | =head1 SUPPLYING YOUR OWN EVENT MODEL INTERFACE |
285 | |
342 | |
286 | 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 |
… | |
… | |
297 | This tells AnyEvent to (literally) use the C<urxvt::anyevent::> |
354 | This tells AnyEvent to (literally) use the C<urxvt::anyevent::> |
298 | 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 |
299 | 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 |
300 | first check for the presence of urxvt. |
357 | first check for the presence of urxvt. |
301 | |
358 | |
302 | The class should prove implementations for all watcher types (see |
359 | The class should provide implementations for all watcher types (see |
303 | L<AnyEvent::Impl::Event> (source code), L<AnyEvent::Impl::Glib> |
360 | L<AnyEvent::Impl::Event> (source code), L<AnyEvent::Impl::Glib> |
304 | (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 |
305 | AnyEvent::Impl::Glib> to see the sources). |
362 | AnyEvent::Impl::Glib> to see the sources). |
306 | |
363 | |
307 | 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) |