ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/lib/AnyEvent.pm
(Generate patch)

Comparing AnyEvent/lib/AnyEvent.pm (file contents):
Revision 1.18 by root, Sun Dec 10 21:33:33 2006 UTC vs.
Revision 1.19 by root, Sun Dec 10 23:59:15 2006 UTC

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
96You can create a timer watcher by calling the C<< AnyEvent->timer >> 96You can create a time watcher by calling the C<< AnyEvent->timer >>
97method with the following mandatory arguments: 97method with the following mandatory arguments:
98 98
99C<after> after how many seconds (fractions are supported) should the timer 99C<after> after how many seconds (fractions are supported) should the timer
100activate. C<cb> the callback to invoke. 100activate. 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
164You can listen for signals using a signal watcher, C<signal> is the signal
165I<name> without any C<SIG> prefix.
166
167These watchers might use C<%SIG>, so programs overwriting those signals
168directly will likely not work correctly.
169
170Example: 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
196Returns C<$AnyEvent::MODEL>, forcing autodetection of the event model if
197necessary. You should only call this function right before you would have
198created 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
216package AnyEvent; 234package AnyEvent;
217 235
218no warnings; 236no warnings;
219use strict 'vars'; 237use strict;
220use Carp; 238use Carp;
221 239
222our $VERSION = '2.1'; 240our $VERSION = '2.5';
223our $MODEL; 241our $MODEL;
224 242
225our $AUTOLOAD; 243our $AUTOLOAD;
226our @ISA; 244our @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
240our %method = map +($_ => 1), qw(io timer condvar broadcast wait DESTROY); 258our %method = map +($_ => 1), qw(io timer condvar broadcast wait signal one_event DESTROY);
241 259
242sub AUTOLOAD { 260sub 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
299sub 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
311package AnyEvent::Base;
312
313# default implementation for signal
314
315our %SIG_CB;
316
317sub 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
333sub 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
286If you need to support another event library which isn't directly 343If you need to support another event library which isn't directly
297This tells AnyEvent to (literally) use the C<urxvt::anyevent::> 354This tells AnyEvent to (literally) use the C<urxvt::anyevent::>
298package/class when it finds the C<urxvt> package/module is loaded. When 355package/class when it finds the C<urxvt> package/module is loaded. When
299AnyEvent is loaded and asked to find a suitable event model, it will 356AnyEvent is loaded and asked to find a suitable event model, it will
300first check for the presence of urxvt. 357first check for the presence of urxvt.
301 358
302The class should prove implementations for all watcher types (see 359The class should provide implementations for all watcher types (see
303L<AnyEvent::Impl::Event> (source code), L<AnyEvent::Impl::Glib> 360L<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
305AnyEvent::Impl::Glib> to see the sources). 362AnyEvent::Impl::Glib> to see the sources).
306 363
307The above isn't fictitious, the I<rxvt-unicode> (a.k.a. urxvt) 364The above isn't fictitious, the I<rxvt-unicode> (a.k.a. urxvt)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines