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.15 by root, Mon Oct 30 20:55:05 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
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
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
174=head1 GLOBALS
175
176=over 4
177
178=item $AnyEvent::MODEL
179
180Contains C<undef> until the first watcher is being created. Then it
181contains the event model that is being used, which is the name of the
182Perl class implementing the model. This class is usually one of the
183C<AnyEvent::Impl:xxx> modules, but can be any other class in the case
184AnyEvent has been extended at runtime (e.g. in I<rxvt-unicode>).
185
186The 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
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.
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
194package AnyEvent; 234package AnyEvent;
195 235
196no warnings; 236no warnings;
197use strict 'vars'; 237use strict;
198use Carp; 238use Carp;
199 239
200our $VERSION = '2.0'; 240our $VERSION = '2.5';
201our $MODEL; 241our $MODEL;
202 242
203our $AUTOLOAD; 243our $AUTOLOAD;
204our @ISA; 244our @ISA;
205 245
206our $verbose = $ENV{PERL_ANYEVENT_VERBOSE}*1; 246our $verbose = $ENV{PERL_ANYEVENT_VERBOSE}*1;
207 247
208our @REGISTRY; 248our @REGISTRY;
209 249
210my @models = ( 250my @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
218our %method = map +($_ => 1), qw(io timer condvar broadcast wait DESTROY); 258our %method = map +($_ => 1), qw(io timer condvar broadcast wait signal one_event DESTROY);
219 259
220sub AUTOLOAD { 260sub 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
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;
257 306
258 my $class = shift; 307 my $class = shift;
259 $class->$AUTOLOAD (@_); 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} };
260} 339}
261 340
262=head1 SUPPLYING YOUR OWN EVENT MODEL INTERFACE 341=head1 SUPPLYING YOUR OWN EVENT MODEL INTERFACE
263 342
264If you need to support another event library which isn't directly 343If you need to support another event library which isn't directly
275This tells AnyEvent to (literally) use the C<urxvt::anyevent::> 354This tells AnyEvent to (literally) use the C<urxvt::anyevent::>
276package/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
277AnyEvent 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
278first check for the presence of urxvt. 357first check for the presence of urxvt.
279 358
280The class should prove implementations for all watcher types (see 359The class should provide implementations for all watcher types (see
281L<AnyEvent::Impl::Event> (source code), L<AnyEvent::Impl::Glib> 360L<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
283AnyEvent::Impl::Glib> to see the sources). 362AnyEvent::Impl::Glib> to see the sources).
284 363
285The 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