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.54 by root, Tue Apr 22 05:12:19 2008 UTC vs.
Revision 1.55 by root, Wed Apr 23 11:25:42 2008 UTC

1=head1 NAME 1=head1 NAME
2 2
3AnyEvent - provide framework for multiple event loops 3AnyEvent - provide framework for multiple event loops
4 4
5EV, Event, Coro::EV, Coro::Event, Glib, Tk, Perl - various supported event loops 5EV, Event, Coro::EV, Coro::Event, Glib, Tk, Perl, Event::Lib - various supported event loops
6 6
7=head1 SYNOPSIS 7=head1 SYNOPSIS
8 8
9 use AnyEvent; 9 use AnyEvent;
10 10
358 AnyEvent::Impl::EV based on EV (an interface to libev, also best choice). 358 AnyEvent::Impl::EV based on EV (an interface to libev, also best choice).
359 AnyEvent::Impl::Event based on Event, also second best choice :) 359 AnyEvent::Impl::Event based on Event, also second best choice :)
360 AnyEvent::Impl::Glib based on Glib, third-best choice. 360 AnyEvent::Impl::Glib based on Glib, third-best choice.
361 AnyEvent::Impl::Tk based on Tk, very bad choice. 361 AnyEvent::Impl::Tk based on Tk, very bad choice.
362 AnyEvent::Impl::Perl pure-perl implementation, inefficient but portable. 362 AnyEvent::Impl::Perl pure-perl implementation, inefficient but portable.
363 AnyEvent::Impl::EventLib based on Event::Lib, leaks memory and worse.
363 364
364=item AnyEvent::detect 365=item AnyEvent::detect
365 366
366Returns C<$AnyEvent::MODEL>, forcing autodetection of the event model 367Returns C<$AnyEvent::MODEL>, forcing autodetection of the event model
367if necessary. You should only call this function right before you would 368if necessary. You should only call this function right before you would
436 [EV:: => AnyEvent::Impl::EV::], 437 [EV:: => AnyEvent::Impl::EV::],
437 [Event:: => AnyEvent::Impl::Event::], 438 [Event:: => AnyEvent::Impl::Event::],
438 [Glib:: => AnyEvent::Impl::Glib::], 439 [Glib:: => AnyEvent::Impl::Glib::],
439 [Tk:: => AnyEvent::Impl::Tk::], 440 [Tk:: => AnyEvent::Impl::Tk::],
440 [AnyEvent::Impl::Perl:: => AnyEvent::Impl::Perl::], 441 [AnyEvent::Impl::Perl:: => AnyEvent::Impl::Perl::],
442 [Event::Lib:: => AnyEvent::Impl::EventLib::],
441); 443);
442 444
443our %method = map +($_ => 1), qw(io timer condvar broadcast wait signal one_event DESTROY); 445our %method = map +($_ => 1), qw(io timer condvar broadcast wait signal one_event DESTROY);
444 446
445sub detect() { 447sub detect() {
446 unless ($MODEL) { 448 unless ($MODEL) {
447 no strict 'refs'; 449 no strict 'refs';
448 450
451 if ($ENV{PERL_ANYEVENT_MODEL} =~ /^([a-zA-Z]+)$/) {
452 my $model = "AnyEvent::Impl::$1";
453 if (eval "require $model") {
454 $MODEL = $model;
455 warn "AnyEvent: loaded model '$model' (forced by \$PERL_ANYEVENT_MODEL), using it.\n" if $verbose > 1;
456 }
457 }
458
449 # check for already loaded models 459 # check for already loaded models
460 unless ($MODEL) {
450 for (@REGISTRY, @models) { 461 for (@REGISTRY, @models) {
451 my ($package, $model) = @$_; 462 my ($package, $model) = @$_;
452 if (${"$package\::VERSION"} > 0) { 463 if (${"$package\::VERSION"} > 0) {
453 if (eval "require $model") { 464 if (eval "require $model") {
454 $MODEL = $model; 465 $MODEL = $model;
455 warn "AnyEvent: found model '$model', using it.\n" if $verbose > 1; 466 warn "AnyEvent: autodetected model '$model', using it.\n" if $verbose > 1;
456 last; 467 last;
468 }
457 } 469 }
458 } 470 }
459 }
460 471
461 unless ($MODEL) { 472 unless ($MODEL) {
462 # try to load a model 473 # try to load a model
463 474
464 for (@REGISTRY, @models) { 475 for (@REGISTRY, @models) {
465 my ($package, $model) = @$_; 476 my ($package, $model) = @$_;
466 if (eval "require $package" 477 if (eval "require $package"
467 and ${"$package\::VERSION"} > 0 478 and ${"$package\::VERSION"} > 0
468 and eval "require $model") { 479 and eval "require $model") {
469 $MODEL = $model; 480 $MODEL = $model;
470 warn "AnyEvent: autoprobed and loaded model '$model', using it.\n" if $verbose > 1; 481 warn "AnyEvent: autoprobed model '$model', using it.\n" if $verbose > 1;
471 last; 482 last;
483 }
472 } 484 }
485
486 $MODEL
487 or die "No event module selected for AnyEvent and autodetect failed. Install any one of these modules: EV (or Coro+EV), Event (or Coro+Event) or Glib.";
473 } 488 }
474
475 $MODEL
476 or die "No event module selected for AnyEvent and autodetect failed. Install any one of these modules: EV (or Coro+EV), Event (or Coro+Event), Glib or Tk.";
477 } 489 }
478 490
479 unshift @ISA, $MODEL; 491 unshift @ISA, $MODEL;
480 push @{"$MODEL\::ISA"}, "AnyEvent::Base"; 492 push @{"$MODEL\::ISA"}, "AnyEvent::Base";
481 } 493 }
637 649
638=head1 ENVIRONMENT VARIABLES 650=head1 ENVIRONMENT VARIABLES
639 651
640The following environment variables are used by this module: 652The following environment variables are used by this module:
641 653
642C<PERL_ANYEVENT_VERBOSE> when set to C<2> or higher, cause AnyEvent to 654=over 4
643report to STDERR which event model it chooses. 655
656=item C<PERL_ANYEVENT_VERBOSE>
657
658When set to C<2> or higher, cause AnyEvent to report to STDERR which event
659model it chooses.
660
661=item C<PERL_ANYEVENT_MODEL>
662
663This can be used to specify the event model to be used by AnyEvent, before
664autodetection and -probing kicks in. It must be a string consisting
665entirely of ASCII letters. The string C<AnyEvent::Impl::> gets prepended
666and the resulting module name is loaded and if the load was successful,
667used as event model. If it fails to load AnyEvent will proceed with
668autodetection and -probing.
669
670This functionality might change in future versions.
671
672For example, to force the pure perl model (L<AnyEvent::Impl::Perl>) you
673could start your program like this:
674
675 PERL_ANYEVENT_MODEL=Perl perl ...
676
677=back
644 678
645=head1 EXAMPLE PROGRAM 679=head1 EXAMPLE PROGRAM
646 680
647The following program uses an IO watcher to read data from STDIN, a timer 681The following program uses an IO watcher to read data from STDIN, a timer
648to display a message once per second, and a condition variable to quit the 682to display a message once per second, and a condition variable to quit the
796 $quit->broadcast; 830 $quit->broadcast;
797 }); 831 });
798 832
799 $quit->wait; 833 $quit->wait;
800 834
835=head1 FORK
836
837Most event libraries are not fork-safe. The ones who are usually are
838because they are so inefficient. Only L<EV> is fully fork-aware.
839
840If you have to fork, you must either do so I<before> creating your first
841watcher OR you must not use AnyEvent at all in the child.
842
843=head1 SECURITY CONSIDERATIONS
844
845AnyEvent can be forced to load any event model via
846$ENV{PERL_ANYEVENT_MODEL}. While this cannot (to my knowledge) be used to
847execute arbitrary code or directly gain access, it can easily be used to
848make the program hang or malfunction in subtle ways, as AnyEvent watchers
849will not be active when the program uses a different event model than
850specified in the variable.
851
852You can make AnyEvent completely ignore this variable by deleting it
853before the first watcher gets created, e.g. with a C<BEGIN> block:
854
855 BEGIN { delete $ENV{PERL_ANYEVENT_MODEL} }
856
857 use AnyEvent;
858
801=head1 SEE ALSO 859=head1 SEE ALSO
802 860
803Event modules: L<Coro::EV>, L<EV>, L<EV::Glib>, L<Glib::EV>, 861Event modules: L<Coro::EV>, L<EV>, L<EV::Glib>, L<Glib::EV>,
804L<Coro::Event>, L<Event>, L<Glib::Event>, L<Glib>, L<Coro>, L<Tk>. 862L<Coro::Event>, L<Event>, L<Glib::Event>, L<Glib>, L<Coro>, L<Tk>,
863L<Event::Lib>.
805 864
806Implementations: L<AnyEvent::Impl::CoroEV>, L<AnyEvent::Impl::EV>, 865Implementations: L<AnyEvent::Impl::CoroEV>, L<AnyEvent::Impl::EV>,
807L<AnyEvent::Impl::CoroEvent>, L<AnyEvent::Impl::Event>, 866L<AnyEvent::Impl::CoroEvent>, L<AnyEvent::Impl::Event>, L<AnyEvent::Impl::Glib>,
808L<AnyEvent::Impl::Glib>, L<AnyEvent::Impl::Tk>, L<AnyEvent::Impl::Perl>. 867L<AnyEvent::Impl::Tk>, L<AnyEvent::Impl::Perl>, L<AnyEvent::Impl::EventLib>.
809 868
810Nontrivial usage examples: L<Net::FCP>, L<Net::XMPP2>. 869Nontrivial usage examples: L<Net::FCP>, L<Net::XMPP2>.
811 870
812=head1 AUTHOR 871=head1 AUTHOR
813 872

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines