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.331 by root, Tue Aug 31 01:00:48 2010 UTC vs.
Revision 1.410 by root, Fri Mar 1 06:03:21 2013 UTC

1=head1 NAME 1=head1 NAME
2 2
3AnyEvent - the DBI of event loop programming 3AnyEvent - the DBI of event loop programming
4 4
5EV, Event, Glib, Tk, Perl, Event::Lib, Irssi, rxvt-unicode, IO::Async, Qt 5EV, Event, Glib, Tk, Perl, Event::Lib, Irssi, rxvt-unicode, IO::Async, Qt,
6and POE are various supported event loops/environments. 6FLTK and POE are various supported event loops/environments.
7 7
8=head1 SYNOPSIS 8=head1 SYNOPSIS
9 9
10 use AnyEvent; 10 use AnyEvent;
11 11
46in a tutorial or some gentle introduction, have a look at the 46in a tutorial or some gentle introduction, have a look at the
47L<AnyEvent::Intro> manpage. 47L<AnyEvent::Intro> manpage.
48 48
49=head1 SUPPORT 49=head1 SUPPORT
50 50
51An FAQ document is available as L<AnyEvent::FAQ>.
52
51There is a mailinglist for discussing all things AnyEvent, and an IRC 53There also is a mailinglist for discussing all things AnyEvent, and an IRC
52channel, too. 54channel, too.
53 55
54See the AnyEvent project page at the B<Schmorpforge Ta-Sa Software 56See the AnyEvent project page at the B<Schmorpforge Ta-Sa Software
55Repository>, at L<http://anyevent.schmorp.de>, for more info. 57Repository>, at L<http://anyevent.schmorp.de>, for more info.
56 58
83that isn't them. What's worse, all the potential users of your 85that isn't them. What's worse, all the potential users of your
84module are I<also> forced to use the same event loop you use. 86module are I<also> forced to use the same event loop you use.
85 87
86AnyEvent is different: AnyEvent + POE works fine. AnyEvent + Glib works 88AnyEvent is different: AnyEvent + POE works fine. AnyEvent + Glib works
87fine. AnyEvent + Tk works fine etc. etc. but none of these work together 89fine. AnyEvent + Tk works fine etc. etc. but none of these work together
88with the rest: POE + IO::Async? No go. Tk + Event? No go. Again: if 90with the rest: POE + EV? No go. Tk + Event? No go. Again: if your module
89your module uses one of those, every user of your module has to use it, 91uses one of those, every user of your module has to use it, too. But if
90too. But if your module uses AnyEvent, it works transparently with all 92your module uses AnyEvent, it works transparently with all event models it
91event models it supports (including stuff like IO::Async, as long as those 93supports (including stuff like IO::Async, as long as those use one of the
92use one of the supported event loops. It is easy to add new event loops 94supported event loops. It is easy to add new event loops to AnyEvent, too,
93to AnyEvent, too, so it is future-proof). 95so it is future-proof).
94 96
95In addition to being free of having to use I<the one and only true event 97In addition to being free of having to use I<the one and only true event
96model>, AnyEvent also is free of bloat and policy: with POE or similar 98model>, AnyEvent also is free of bloat and policy: with POE or similar
97modules, you get an enormous amount of code and strict rules you have to 99modules, you get an enormous amount of code and strict rules you have to
98follow. AnyEvent, on the other hand, is lean and to the point, by only 100follow. AnyEvent, on the other hand, is lean and to the point, by only
119The interface itself is vaguely similar, but not identical to the L<Event> 121The interface itself is vaguely similar, but not identical to the L<Event>
120module. 122module.
121 123
122During the first call of any watcher-creation method, the module tries 124During the first call of any watcher-creation method, the module tries
123to detect the currently loaded event loop by probing whether one of the 125to detect the currently loaded event loop by probing whether one of the
124following modules is already loaded: L<EV>, L<AnyEvent::Impl::Perl>, 126following modules is already loaded: L<EV>, L<AnyEvent::Loop>,
125L<Event>, L<Glib>, L<Tk>, L<Event::Lib>, L<Qt>, L<POE>. The first one 127L<Event>, L<Glib>, L<Tk>, L<Event::Lib>, L<Qt>, L<POE>. The first one
126found is used. If none are detected, the module tries to load the first 128found is used. If none are detected, the module tries to load the first
127four modules in the order given; but note that if L<EV> is not 129four modules in the order given; but note that if L<EV> is not
128available, the pure-perl L<AnyEvent::Impl::Perl> should always work, so 130available, the pure-perl L<AnyEvent::Loop> should always work, so
129the other two are not normally tried. 131the other two are not normally tried.
130 132
131Because AnyEvent first checks for modules that are already loaded, loading 133Because AnyEvent first checks for modules that are already loaded, loading
132an event model explicitly before first using AnyEvent will likely make 134an event model explicitly before first using AnyEvent will likely make
133that model the default. For example: 135that model the default. For example:
140The I<likely> means that, if any module loads another event model and 142The I<likely> means that, if any module loads another event model and
141starts using it, all bets are off - this case should be very rare though, 143starts using it, all bets are off - this case should be very rare though,
142as very few modules hardcode event loops without announcing this very 144as very few modules hardcode event loops without announcing this very
143loudly. 145loudly.
144 146
145The pure-perl implementation of AnyEvent is called 147The pure-perl implementation of AnyEvent is called C<AnyEvent::Loop>. Like
146C<AnyEvent::Impl::Perl>. Like other event modules you can load it 148other event modules you can load it explicitly and enjoy the high
147explicitly and enjoy the high availability of that event loop :) 149availability of that event loop :)
148 150
149=head1 WATCHERS 151=head1 WATCHERS
150 152
151AnyEvent has the central concept of a I<watcher>, which is an object that 153AnyEvent has the central concept of a I<watcher>, which is an object that
152stores relevant data for each kind of event you are waiting for, such as 154stores relevant data for each kind of event you are waiting for, such as
354difference between C<< AnyEvent->time >> and C<< AnyEvent->now >> into 356difference between C<< AnyEvent->time >> and C<< AnyEvent->now >> into
355account. 357account.
356 358
357=item AnyEvent->now_update 359=item AnyEvent->now_update
358 360
359Some event loops (such as L<EV> or L<AnyEvent::Impl::Perl>) cache 361Some event loops (such as L<EV> or L<AnyEvent::Loop>) cache the current
360the current time for each loop iteration (see the discussion of L<< 362time for each loop iteration (see the discussion of L<< AnyEvent->now >>,
361AnyEvent->now >>, above). 363above).
362 364
363When a callback runs for a long time (or when the process sleeps), then 365When a callback runs for a long time (or when the process sleeps), then
364this "current" time will differ substantially from the real time, which 366this "current" time will differ substantially from the real time, which
365might affect timers and time-outs. 367might affect timers and time-outs.
366 368
413not restart syscalls (that includes L<Async::Interrupt> and AnyEvent's 415not restart syscalls (that includes L<Async::Interrupt> and AnyEvent's
414pure perl implementation). 416pure perl implementation).
415 417
416=head3 Safe/Unsafe Signals 418=head3 Safe/Unsafe Signals
417 419
418Perl signals can be either "safe" (synchronous to opcode handling) or 420Perl signals can be either "safe" (synchronous to opcode handling)
419"unsafe" (asynchronous) - the former might get delayed indefinitely, the 421or "unsafe" (asynchronous) - the former might delay signal delivery
420latter might corrupt your memory. 422indefinitely, the latter might corrupt your memory.
421 423
422AnyEvent signal handlers are, in addition, synchronous to the event loop, 424AnyEvent signal handlers are, in addition, synchronous to the event loop,
423i.e. they will not interrupt your running perl program but will only be 425i.e. they will not interrupt your running perl program but will only be
424called as part of the normal event handling (just like timer, I/O etc. 426called as part of the normal event handling (just like timer, I/O etc.
425callbacks, too). 427callbacks, too).
426 428
427=head3 Signal Races, Delays and Workarounds 429=head3 Signal Races, Delays and Workarounds
428 430
429Many event loops (e.g. Glib, Tk, Qt, IO::Async) do not support attaching 431Many event loops (e.g. Glib, Tk, Qt, IO::Async) do not support
430callbacks to signals in a generic way, which is a pity, as you cannot 432attaching callbacks to signals in a generic way, which is a pity,
431do race-free signal handling in perl, requiring C libraries for 433as you cannot do race-free signal handling in perl, requiring
432this. AnyEvent will try to do its best, which means in some cases, 434C libraries for this. AnyEvent will try to do its best, which
433signals will be delayed. The maximum time a signal might be delayed is 435means in some cases, signals will be delayed. The maximum time
434specified in C<$AnyEvent::MAX_SIGNAL_LATENCY> (default: 10 seconds). This 436a signal might be delayed is 10 seconds by default, but can
435variable can be changed only before the first signal watcher is created, 437be overriden via C<$ENV{PERL_ANYEVENT_MAX_SIGNAL_LATENCY}> or
436and should be left alone otherwise. This variable determines how often 438C<$AnyEvent::MAX_SIGNAL_LATENCY> - see the L<ENVIRONMENT VARIABLES>
437AnyEvent polls for signals (in case a wake-up was missed). Higher values 439section for details.
438will cause fewer spurious wake-ups, which is better for power and CPU
439saving.
440 440
441All these problems can be avoided by installing the optional 441All these problems can be avoided by installing the optional
442L<Async::Interrupt> module, which works with most event loops. It will not 442L<Async::Interrupt> module, which works with most event loops. It will not
443work with inherently broken event loops such as L<Event> or L<Event::Lib> 443work with inherently broken event loops such as L<Event> or L<Event::Lib>
444(and not with L<POE> currently, as POE does its own workaround with 444(and not with L<POE> currently). For those, you just have to suffer the
445one-second latency). For those, you just have to suffer the delays. 445delays.
446 446
447=head2 CHILD PROCESS WATCHERS 447=head2 CHILD PROCESS WATCHERS
448 448
449 $w = AnyEvent->child (pid => <process id>, cb => <callback>); 449 $w = AnyEvent->child (pid => <process id>, cb => <callback>);
450 450
480thing in an AnyEvent program, you I<have> to create at least one 480thing in an AnyEvent program, you I<have> to create at least one
481watcher before you C<fork> the child (alternatively, you can call 481watcher before you C<fork> the child (alternatively, you can call
482C<AnyEvent::detect>). 482C<AnyEvent::detect>).
483 483
484As most event loops do not support waiting for child events, they will be 484As most event loops do not support waiting for child events, they will be
485emulated by AnyEvent in most cases, in which the latency and race problems 485emulated by AnyEvent in most cases, in which case the latency and race
486mentioned in the description of signal watchers apply. 486problems mentioned in the description of signal watchers apply.
487 487
488Example: fork a process and wait for it 488Example: fork a process and wait for it
489 489
490 my $done = AnyEvent->condvar; 490 my $done = AnyEvent->condvar;
491 491
763 }; 763 };
764 } 764 }
765 765
766 $cv->end; 766 $cv->end;
767 767
768 ...
769
770 my $results = $cv->recv;
771
768This code fragment supposedly pings a number of hosts and calls 772This code fragment supposedly pings a number of hosts and calls
769C<send> after results for all then have have been gathered - in any 773C<send> after results for all then have have been gathered - in any
770order. To achieve this, the code issues a call to C<begin> when it starts 774order. To achieve this, the code issues a call to C<begin> when it starts
771each ping request and calls C<end> when it has received some result for 775each ping request and calls C<end> when it has received some result for
772it. Since C<begin> and C<end> only maintain a counter, the order in which 776it. Since C<begin> and C<end> only maintain a counter, the order in which
807 811
808In list context, all parameters passed to C<send> will be returned, 812In list context, all parameters passed to C<send> will be returned,
809in scalar context only the first one will be returned. 813in scalar context only the first one will be returned.
810 814
811Note that doing a blocking wait in a callback is not supported by any 815Note that doing a blocking wait in a callback is not supported by any
812event loop, that is, recursive invocation of a blocking C<< ->recv 816event loop, that is, recursive invocation of a blocking C<< ->recv >> is
813>> is not allowed, and the C<recv> call will C<croak> if such a 817not allowed and the C<recv> call will C<croak> if such a condition is
814condition is detected. This condition can be slightly loosened by using 818detected. This requirement can be dropped by relying on L<Coro::AnyEvent>
815L<Coro::AnyEvent>, which allows you to do a blocking C<< ->recv >> from 819, which allows you to do a blocking C<< ->recv >> from any thread
816any thread that doesn't run the event loop itself. 820that doesn't run the event loop itself. L<Coro::AnyEvent> is loaded
821automatically when L<Coro> is used with L<AnyEvent>, so code does not need
822to do anything special to take advantage of that: any code that would
823normally block your program because it calls C<recv>, be executed in an
824C<async> thread instead without blocking other threads.
817 825
818Not all event models support a blocking wait - some die in that case 826Not all event models support a blocking wait - some die in that case
819(programs might want to do that to stay interactive), so I<if you are 827(programs might want to do that to stay interactive), so I<if you are
820using this from a module, never require a blocking wait>. Instead, let the 828using this from a module, never require a blocking wait>. Instead, let the
821caller decide whether the call will block or not (for example, by coupling 829caller decide whether the call will block or not (for example, by coupling
858use. If EV is not installed, then AnyEvent will fall back to its own 866use. If EV is not installed, then AnyEvent will fall back to its own
859pure-perl implementation, which is available everywhere as it comes with 867pure-perl implementation, which is available everywhere as it comes with
860AnyEvent itself. 868AnyEvent itself.
861 869
862 AnyEvent::Impl::EV based on EV (interface to libev, best choice). 870 AnyEvent::Impl::EV based on EV (interface to libev, best choice).
863 AnyEvent::Impl::Perl pure-perl implementation, fast and portable. 871 AnyEvent::Impl::Perl pure-perl AnyEvent::Loop, fast and portable.
864 872
865=item Backends that are transparently being picked up when they are used. 873=item Backends that are transparently being picked up when they are used.
866 874
867These will be used if they are already loaded when the first watcher 875These will be used if they are already loaded when the first watcher
868is created, in which case it is assumed that the application is using 876is created, in which case it is assumed that the application is using
874 AnyEvent::Impl::Glib based on Glib, slow but very stable. 882 AnyEvent::Impl::Glib based on Glib, slow but very stable.
875 AnyEvent::Impl::Tk based on Tk, very broken. 883 AnyEvent::Impl::Tk based on Tk, very broken.
876 AnyEvent::Impl::EventLib based on Event::Lib, leaks memory and worse. 884 AnyEvent::Impl::EventLib based on Event::Lib, leaks memory and worse.
877 AnyEvent::Impl::POE based on POE, very slow, some limitations. 885 AnyEvent::Impl::POE based on POE, very slow, some limitations.
878 AnyEvent::Impl::Irssi used when running within irssi. 886 AnyEvent::Impl::Irssi used when running within irssi.
887 AnyEvent::Impl::IOAsync based on IO::Async.
888 AnyEvent::Impl::Cocoa based on Cocoa::EventLoop.
889 AnyEvent::Impl::FLTK based on FLTK (fltk 2 binding).
879 890
880=item Backends with special needs. 891=item Backends with special needs.
881 892
882Qt requires the Qt::Application to be instantiated first, but will 893Qt requires the Qt::Application to be instantiated first, but will
883otherwise be picked up automatically. As long as the main program 894otherwise be picked up automatically. As long as the main program
884instantiates the application before any AnyEvent watchers are created, 895instantiates the application before any AnyEvent watchers are created,
885everything should just work. 896everything should just work.
886 897
887 AnyEvent::Impl::Qt based on Qt. 898 AnyEvent::Impl::Qt based on Qt.
888
889Support for IO::Async can only be partial, as it is too broken and
890architecturally limited to even support the AnyEvent API. It also
891is the only event loop that needs the loop to be set explicitly, so
892it can only be used by a main program knowing about AnyEvent. See
893L<AnyEvent::Impl::IOAsync> for the gory details.
894
895 AnyEvent::Impl::IOAsync based on IO::Async, cannot be autoprobed.
896 899
897=item Event loops that are indirectly supported via other backends. 900=item Event loops that are indirectly supported via other backends.
898 901
899Some event loops can be supported via other modules: 902Some event loops can be supported via other modules:
900 903
936 939
937Returns C<$AnyEvent::MODEL>, forcing autodetection of the event model 940Returns C<$AnyEvent::MODEL>, forcing autodetection of the event model
938if necessary. You should only call this function right before you would 941if necessary. You should only call this function right before you would
939have created an AnyEvent watcher anyway, that is, as late as possible at 942have created an AnyEvent watcher anyway, that is, as late as possible at
940runtime, and not e.g. during initialisation of your module. 943runtime, and not e.g. during initialisation of your module.
944
945The effect of calling this function is as if a watcher had been created
946(specifically, actions that happen "when the first watcher is created"
947happen when calling detetc as well).
941 948
942If you need to do some initialisation before AnyEvent watchers are 949If you need to do some initialisation before AnyEvent watchers are
943created, use C<post_detect>. 950created, use C<post_detect>.
944 951
945=item $guard = AnyEvent::post_detect { BLOCK } 952=item $guard = AnyEvent::post_detect { BLOCK }
1008 # AnyEvent not yet initialised, so make sure to load Coro::AnyEvent 1015 # AnyEvent not yet initialised, so make sure to load Coro::AnyEvent
1009 # as soon as it is 1016 # as soon as it is
1010 push @AnyEvent::post_detect, sub { require Coro::AnyEvent }; 1017 push @AnyEvent::post_detect, sub { require Coro::AnyEvent };
1011 } 1018 }
1012 1019
1020=item AnyEvent::postpone { BLOCK }
1021
1022Arranges for the block to be executed as soon as possible, but not before
1023the call itself returns. In practise, the block will be executed just
1024before the event loop polls for new events, or shortly afterwards.
1025
1026This function never returns anything (to make the C<return postpone { ...
1027}> idiom more useful.
1028
1029To understand the usefulness of this function, consider a function that
1030asynchronously does something for you and returns some transaction
1031object or guard to let you cancel the operation. For example,
1032C<AnyEvent::Socket::tcp_connect>:
1033
1034 # start a conenction attempt unless one is active
1035 $self->{connect_guard} ||= AnyEvent::Socket::tcp_connect "www.example.net", 80, sub {
1036 delete $self->{connect_guard};
1037 ...
1038 };
1039
1040Imagine that this function could instantly call the callback, for
1041example, because it detects an obvious error such as a negative port
1042number. Invoking the callback before the function returns causes problems
1043however: the callback will be called and will try to delete the guard
1044object. But since the function hasn't returned yet, there is nothing to
1045delete. When the function eventually returns it will assign the guard
1046object to C<< $self->{connect_guard} >>, where it will likely never be
1047deleted, so the program thinks it is still trying to connect.
1048
1049This is where C<AnyEvent::postpone> should be used. Instead of calling the
1050callback directly on error:
1051
1052 $cb->(undef), return # signal error to callback, BAD!
1053 if $some_error_condition;
1054
1055It should use C<postpone>:
1056
1057 AnyEvent::postpone { $cb->(undef) }, return # signal error to callback, later
1058 if $some_error_condition;
1059
1060=item AnyEvent::log $level, $msg[, @args]
1061
1062Log the given C<$msg> at the given C<$level>.
1063
1064If L<AnyEvent::Log> is not loaded then this function makes a simple test
1065to see whether the message will be logged. If the test succeeds it will
1066load AnyEvent::Log and call C<AnyEvent::Log::log> - consequently, look at
1067the L<AnyEvent::Log> documentation for details.
1068
1069If the test fails it will simply return. Right now this happens when a
1070numerical loglevel is used and it is larger than the level specified via
1071C<$ENV{PERL_ANYEVENT_VERBOSE}>.
1072
1073If you want to sprinkle loads of logging calls around your code, consider
1074creating a logger callback with the C<AnyEvent::Log::logger> function,
1075which can reduce typing, codesize and can reduce the logging overhead
1076enourmously.
1077
1013=back 1078=back
1014 1079
1015=head1 WHAT TO DO IN A MODULE 1080=head1 WHAT TO DO IN A MODULE
1016 1081
1017As a module author, you should C<use AnyEvent> and call AnyEvent methods 1082As a module author, you should C<use AnyEvent> and call AnyEvent methods
1050modules might create watchers when they are loaded, and AnyEvent will 1115modules might create watchers when they are loaded, and AnyEvent will
1051decide on the event model to use as soon as it creates watchers, and it 1116decide on the event model to use as soon as it creates watchers, and it
1052might choose the wrong one unless you load the correct one yourself. 1117might choose the wrong one unless you load the correct one yourself.
1053 1118
1054You can chose to use a pure-perl implementation by loading the 1119You can chose to use a pure-perl implementation by loading the
1055C<AnyEvent::Impl::Perl> module, which gives you similar behaviour 1120C<AnyEvent::Loop> module, which gives you similar behaviour
1056everywhere, but letting AnyEvent chose the model is generally better. 1121everywhere, but letting AnyEvent chose the model is generally better.
1057 1122
1058=head2 MAINLOOP EMULATION 1123=head2 MAINLOOP EMULATION
1059 1124
1060Sometimes (often for short test scripts, or even standalone programs who 1125Sometimes (often for short test scripts, or even standalone programs who
1073 1138
1074 1139
1075=head1 OTHER MODULES 1140=head1 OTHER MODULES
1076 1141
1077The following is a non-exhaustive list of additional modules that use 1142The following is a non-exhaustive list of additional modules that use
1078AnyEvent as a client and can therefore be mixed easily with other AnyEvent 1143AnyEvent as a client and can therefore be mixed easily with other
1079modules and other event loops in the same program. Some of the modules 1144AnyEvent modules and other event loops in the same program. Some of the
1080come as part of AnyEvent, the others are available via CPAN. 1145modules come as part of AnyEvent, the others are available via CPAN (see
1146L<http://search.cpan.org/search?m=module&q=anyevent%3A%3A*> for
1147a longer non-exhaustive list), and the list is heavily biased towards
1148modules of the AnyEvent author himself :)
1081 1149
1082=over 4 1150=over 4
1083 1151
1084=item L<AnyEvent::Util> 1152=item L<AnyEvent::Util>
1085 1153
1105=item L<AnyEvent::HTTP>, L<AnyEvent::IRC>, L<AnyEvent::XMPP>, L<AnyEvent::GPSD>, L<AnyEvent::IGS>, L<AnyEvent::FCP> 1173=item L<AnyEvent::HTTP>, L<AnyEvent::IRC>, L<AnyEvent::XMPP>, L<AnyEvent::GPSD>, L<AnyEvent::IGS>, L<AnyEvent::FCP>
1106 1174
1107Implement event-based interfaces to the protocols of the same name (for 1175Implement event-based interfaces to the protocols of the same name (for
1108the curious, IGS is the International Go Server and FCP is the Freenet 1176the curious, IGS is the International Go Server and FCP is the Freenet
1109Client Protocol). 1177Client Protocol).
1110
1111=item L<AnyEvent::Handle::UDP>
1112
1113Here be danger!
1114
1115As Pauli would put it, "Not only is it not right, it's not even wrong!" -
1116there are so many things wrong with AnyEvent::Handle::UDP, most notably
1117its use of a stream-based API with a protocol that isn't streamable, that
1118the only way to improve it is to delete it.
1119
1120It features data corruption (but typically only under load) and general
1121confusion. On top, the author is not only clueless about UDP but also
1122fact-resistant - some gems of his understanding: "connect doesn't work
1123with UDP", "UDP packets are not IP packets", "UDP only has datagrams, not
1124packets", "I don't need to implement proper error checking as UDP doesn't
1125support error checking" and so on - he doesn't even understand what's
1126wrong with his module when it is explained to him.
1127
1128=item L<AnyEvent::DBI>
1129
1130Executes L<DBI> requests asynchronously in a proxy process for you,
1131notifying you in an event-based way when the operation is finished.
1132 1178
1133=item L<AnyEvent::AIO> 1179=item L<AnyEvent::AIO>
1134 1180
1135Truly asynchronous (as opposed to non-blocking) I/O, should be in the 1181Truly asynchronous (as opposed to non-blocking) I/O, should be in the
1136toolbox of every event programmer. AnyEvent::AIO transparently fuses 1182toolbox of every event programmer. AnyEvent::AIO transparently fuses
1137L<IO::AIO> and AnyEvent together, giving AnyEvent access to event-based 1183L<IO::AIO> and AnyEvent together, giving AnyEvent access to event-based
1138file I/O, and much more. 1184file I/O, and much more.
1139 1185
1186=item L<AnyEvent::Filesys::Notify>
1187
1188AnyEvent is good for non-blocking stuff, but it can't detect file or
1189path changes (e.g. "watch this directory for new files", "watch this
1190file for changes"). The L<AnyEvent::Filesys::Notify> module promises to
1191do just that in a portbale fashion, supporting inotify on GNU/Linux and
1192some weird, without doubt broken, stuff on OS X to monitor files. It can
1193fall back to blocking scans at regular intervals transparently on other
1194platforms, so it's about as portable as it gets.
1195
1196(I haven't used it myself, but I haven't heard anybody complaining about
1197it yet).
1198
1199=item L<AnyEvent::DBI>
1200
1201Executes L<DBI> requests asynchronously in a proxy process for you,
1202notifying you in an event-based way when the operation is finished.
1203
1140=item L<AnyEvent::HTTPD> 1204=item L<AnyEvent::HTTPD>
1141 1205
1142A simple embedded webserver. 1206A simple embedded webserver.
1143 1207
1144=item L<AnyEvent::FastPing> 1208=item L<AnyEvent::FastPing>
1145 1209
1146The fastest ping in the west. 1210The fastest ping in the west.
1147 1211
1148=item L<Coro> 1212=item L<Coro>
1149 1213
1150Has special support for AnyEvent via L<Coro::AnyEvent>. 1214Has special support for AnyEvent via L<Coro::AnyEvent>, which allows you
1215to simply invert the flow control - don't call us, we will call you:
1216
1217 async {
1218 Coro::AnyEvent::sleep 5; # creates a 5s timer and waits for it
1219 print "5 seconds later!\n";
1220
1221 Coro::AnyEvent::readable *STDIN; # uses an I/O watcher
1222 my $line = <STDIN>; # works for ttys
1223
1224 AnyEvent::HTTP::http_get "url", Coro::rouse_cb;
1225 my ($body, $hdr) = Coro::rouse_wait;
1226 };
1151 1227
1152=back 1228=back
1153 1229
1154=cut 1230=cut
1155 1231
1156package AnyEvent; 1232package AnyEvent;
1157 1233
1158# basically a tuned-down version of common::sense 1234# basically a tuned-down version of common::sense
1159sub common_sense { 1235sub common_sense {
1160 # from common:.sense 1.0 1236 # from common:.sense 3.5
1237 local $^W;
1161 ${^WARNING_BITS} = "\xfc\x3f\x33\x00\x0f\xf3\xcf\xc0\xf3\xfc\x33\x00"; 1238 ${^WARNING_BITS} ^= ${^WARNING_BITS} ^ "\x3c\x3f\x33\x00\x0f\xf0\x0f\xc0\xf0\xfc\x33\x00";
1162 # use strict vars subs - NO UTF-8, as Util.pm doesn't like this atm. (uts46data.pl) 1239 # use strict vars subs - NO UTF-8, as Util.pm doesn't like this atm. (uts46data.pl)
1163 $^H |= 0x00000600; 1240 $^H |= 0x00000600;
1164} 1241}
1165 1242
1166BEGIN { AnyEvent::common_sense } 1243BEGIN { AnyEvent::common_sense }
1167 1244
1168use Carp (); 1245use Carp ();
1169 1246
1170our $VERSION = '5.271'; 1247our $VERSION = '7.04';
1171our $MODEL; 1248our $MODEL;
1172
1173our $AUTOLOAD;
1174our @ISA; 1249our @ISA;
1175
1176our @REGISTRY; 1250our @REGISTRY;
1177
1178our $VERBOSE; 1251our $VERBOSE;
1252our %PROTOCOL; # (ipv4|ipv6) => (1|2), higher numbers are preferred
1253our $MAX_SIGNAL_LATENCY = $ENV{PERL_ANYEVENT_MAX_SIGNAL_LATENCY} || 10; # executes after the BEGIN block below (tainting!)
1179 1254
1180BEGIN { 1255BEGIN {
1181 require "AnyEvent/constants.pl"; 1256 require "AnyEvent/constants.pl";
1182 1257
1183 eval "sub TAINT (){" . (${^TAINT}*1) . "}"; 1258 eval "sub TAINT (){" . (${^TAINT}*1) . "}";
1184 1259
1185 delete @ENV{grep /^PERL_ANYEVENT_/, keys %ENV} 1260 delete @ENV{grep /^PERL_ANYEVENT_/, keys %ENV}
1186 if ${^TAINT}; 1261 if ${^TAINT};
1187 1262
1188 $VERBOSE = $ENV{PERL_ANYEVENT_VERBOSE}*1; 1263 $ENV{"PERL_ANYEVENT_$_"} = $ENV{"AE_$_"}
1264 for grep s/^AE_// && !exists $ENV{"PERL_ANYEVENT_$_"}, keys %ENV;
1189 1265
1190} 1266 @ENV{grep /^PERL_ANYEVENT_/, keys %ENV} = ()
1267 if ${^TAINT};
1191 1268
1192our $MAX_SIGNAL_LATENCY = 10; 1269 # $ENV{PERL_ANYEVENT_xxx} now valid
1193 1270
1194our %PROTOCOL; # (ipv4|ipv6) => (1|2), higher numbers are preferred 1271 $VERBOSE = length $ENV{PERL_ANYEVENT_VERBOSE} ? $ENV{PERL_ANYEVENT_VERBOSE}*1 : 4;
1195 1272
1196{
1197 my $idx; 1273 my $idx;
1198 $PROTOCOL{$_} = ++$idx 1274 $PROTOCOL{$_} = ++$idx
1199 for reverse split /\s*,\s*/, 1275 for reverse split /\s*,\s*/,
1200 $ENV{PERL_ANYEVENT_PROTOCOLS} || "ipv4,ipv6"; 1276 $ENV{PERL_ANYEVENT_PROTOCOLS} || "ipv4,ipv6";
1201} 1277}
1202 1278
1279our @post_detect;
1280
1281sub post_detect(&) {
1282 my ($cb) = @_;
1283
1284 push @post_detect, $cb;
1285
1286 defined wantarray
1287 ? bless \$cb, "AnyEvent::Util::postdetect"
1288 : ()
1289}
1290
1291sub AnyEvent::Util::postdetect::DESTROY {
1292 @post_detect = grep $_ != ${$_[0]}, @post_detect;
1293}
1294
1295our $POSTPONE_W;
1296our @POSTPONE;
1297
1298sub _postpone_exec {
1299 undef $POSTPONE_W;
1300
1301 &{ shift @POSTPONE }
1302 while @POSTPONE;
1303}
1304
1305sub postpone(&) {
1306 push @POSTPONE, shift;
1307
1308 $POSTPONE_W ||= AE::timer (0, 0, \&_postpone_exec);
1309
1310 ()
1311}
1312
1313sub log($$;@) {
1314 # only load the big bloated module when we actually are about to log something
1315 if ($_[0] <= ($VERBOSE || 1)) { # also catches non-numeric levels(!) and fatal
1316 local ($!, $@);
1317 require AnyEvent::Log; # among other things, sets $VERBOSE to 9
1318 # AnyEvent::Log overwrites this function
1319 goto &log;
1320 }
1321
1322 0 # not logged
1323}
1324
1325sub _logger($;$) {
1326 my ($level, $renabled) = @_;
1327
1328 $$renabled = $level <= $VERBOSE;
1329
1330 my $logger = [(caller)[0], $level, $renabled];
1331
1332 $AnyEvent::Log::LOGGER{$logger+0} = $logger;
1333
1334# return unless defined wantarray;
1335#
1336# require AnyEvent::Util;
1337# my $guard = AnyEvent::Util::guard (sub {
1338# # "clean up"
1339# delete $LOGGER{$logger+0};
1340# });
1341#
1342# sub {
1343# return 0 unless $$renabled;
1344#
1345# $guard if 0; # keep guard alive, but don't cause runtime overhead
1346# require AnyEvent::Log unless $AnyEvent::Log::VERSION;
1347# package AnyEvent::Log;
1348# _log ($logger->[0], $level, @_) # logger->[0] has been converted at load time
1349# }
1350}
1351
1352if (length $ENV{PERL_ANYEVENT_LOG}) {
1353 require AnyEvent::Log; # AnyEvent::Log does the thing for us
1354}
1355
1203my @models = ( 1356our @models = (
1204 [EV:: => AnyEvent::Impl::EV:: , 1], 1357 [EV:: => AnyEvent::Impl::EV::],
1205 [AnyEvent::Impl::Perl:: => AnyEvent::Impl::Perl:: , 1], 1358 [AnyEvent::Loop:: => AnyEvent::Impl::Perl::],
1206 # everything below here will not (normally) be autoprobed 1359 # everything below here will not (normally) be autoprobed
1207 # as the pureperl backend should work everywhere 1360 # as the pure perl backend should work everywhere
1208 # and is usually faster 1361 # and is usually faster
1362 [Irssi:: => AnyEvent::Impl::Irssi::], # Irssi has a bogus "Event" package, so msut be near the top
1209 [Event:: => AnyEvent::Impl::Event::, 1], 1363 [Event:: => AnyEvent::Impl::Event::], # slow, stable
1210 [Glib:: => AnyEvent::Impl::Glib:: , 1], # becomes extremely slow with many watchers 1364 [Glib:: => AnyEvent::Impl::Glib::], # becomes extremely slow with many watchers
1365 # everything below here should not be autoloaded
1211 [Event::Lib:: => AnyEvent::Impl::EventLib::], # too buggy 1366 [Event::Lib:: => AnyEvent::Impl::EventLib::], # too buggy
1212 [Irssi:: => AnyEvent::Impl::Irssi::], # Irssi has a bogus "Event" package
1213 [Tk:: => AnyEvent::Impl::Tk::], # crashes with many handles 1367 [Tk:: => AnyEvent::Impl::Tk::], # crashes with many handles
1214 [Qt:: => AnyEvent::Impl::Qt::], # requires special main program 1368 [Qt:: => AnyEvent::Impl::Qt::], # requires special main program
1215 [POE::Kernel:: => AnyEvent::Impl::POE::], # lasciate ogni speranza 1369 [POE::Kernel:: => AnyEvent::Impl::POE::], # lasciate ogni speranza
1216 [Wx:: => AnyEvent::Impl::POE::], 1370 [Wx:: => AnyEvent::Impl::POE::],
1217 [Prima:: => AnyEvent::Impl::POE::], 1371 [Prima:: => AnyEvent::Impl::POE::],
1218 # IO::Async is just too broken - we would need workarounds for its 1372 [IO::Async::Loop:: => AnyEvent::Impl::IOAsync::], # a bitch to autodetect
1219 # byzantine signal and broken child handling, among others. 1373 [Cocoa::EventLoop:: => AnyEvent::Impl::Cocoa::],
1220 # IO::Async is rather hard to detect, as it doesn't have any 1374 [FLTK:: => AnyEvent::Impl::FLTK::],
1221 # obvious default class.
1222 [IO::Async:: => AnyEvent::Impl::IOAsync::], # requires special main program
1223 [IO::Async::Loop:: => AnyEvent::Impl::IOAsync::], # requires special main program
1224 [IO::Async::Notifier:: => AnyEvent::Impl::IOAsync::], # requires special main program
1225 [AnyEvent::Impl::IOAsync:: => AnyEvent::Impl::IOAsync::], # requires special main program
1226); 1375);
1227 1376
1228our %method = map +($_ => 1), 1377our @isa_hook;
1378
1379sub _isa_set {
1380 my @pkg = ("AnyEvent", (map $_->[0], grep defined, @isa_hook), $MODEL);
1381
1382 @{"$pkg[$_-1]::ISA"} = $pkg[$_]
1383 for 1 .. $#pkg;
1384
1385 grep $_ && $_->[1], @isa_hook
1386 and AE::_reset ();
1387}
1388
1389# used for hooking AnyEvent::Strict and AnyEvent::Debug::Wrap into the class hierarchy
1390sub _isa_hook($$;$) {
1391 my ($i, $pkg, $reset_ae) = @_;
1392
1393 $isa_hook[$i] = $pkg ? [$pkg, $reset_ae] : undef;
1394
1395 _isa_set;
1396}
1397
1398# all autoloaded methods reserve the complete glob, not just the method slot.
1399# due to bugs in perls method cache implementation.
1229 qw(io timer time now now_update signal child idle condvar one_event DESTROY); 1400our @methods = qw(io timer time now now_update signal child idle condvar);
1230
1231our @post_detect;
1232
1233sub post_detect(&) {
1234 my ($cb) = @_;
1235
1236 push @post_detect, $cb;
1237
1238 defined wantarray
1239 ? bless \$cb, "AnyEvent::Util::postdetect"
1240 : ()
1241}
1242
1243sub AnyEvent::Util::postdetect::DESTROY {
1244 @post_detect = grep $_ != ${$_[0]}, @post_detect;
1245}
1246 1401
1247sub detect() { 1402sub detect() {
1403 return $MODEL if $MODEL; # some programs keep references to detect
1404
1405 # IO::Async::Loop::AnyEvent is extremely evil, refuse to work with it
1406 # the author knows about the problems and what it does to AnyEvent as a whole
1407 # (and the ability of others to use AnyEvent), but simply wants to abuse AnyEvent
1408 # anyway.
1409 AnyEvent::log fatal => "IO::Async::Loop::AnyEvent detected - that module is broken by\n"
1410 . "design, abuses internals and breaks AnyEvent - will not continue."
1411 if exists $INC{"IO/Async/Loop/AnyEvent.pm"};
1412
1413 local $!; # for good measure
1414 local $SIG{__DIE__}; # we use eval
1415
1248 # free some memory 1416 # free some memory
1249 *detect = sub () { $MODEL }; 1417 *detect = sub () { $MODEL };
1418 # undef &func doesn't correctly update the method cache. grmbl.
1419 # so we delete the whole glob. grmbl.
1420 # otoh, perl doesn't let me undef an active usb, but it lets me free
1421 # a glob with an active sub. hrm. i hope it works, but perl is
1422 # usually buggy in this department. sigh.
1423 delete @{"AnyEvent::"}{@methods};
1424 undef @methods;
1250 1425
1251 local $!; # for good measure
1252 local $SIG{__DIE__};
1253
1254 if ($ENV{PERL_ANYEVENT_MODEL} =~ /^([a-zA-Z]+)$/) { 1426 if ($ENV{PERL_ANYEVENT_MODEL} =~ /^([a-zA-Z0-9:]+)$/) {
1255 my $model = "AnyEvent::Impl::$1"; 1427 my $model = $1;
1428 $model = "AnyEvent::Impl::$model" unless $model =~ s/::$//;
1256 if (eval "require $model") { 1429 if (eval "require $model") {
1430 AnyEvent::log 7 => "Loaded model '$model' (forced by \$ENV{PERL_ANYEVENT_MODEL}), using it.";
1257 $MODEL = $model; 1431 $MODEL = $model;
1258 warn "AnyEvent: loaded model '$model' (forced by \$ENV{PERL_ANYEVENT_MODEL}), using it.\n" if $VERBOSE >= 2;
1259 } else { 1432 } else {
1260 warn "AnyEvent: unable to load model '$model' (from \$ENV{PERL_ANYEVENT_MODEL}):\n$@" if $VERBOSE; 1433 AnyEvent::log 4 => "Unable to load model '$model' (from \$ENV{PERL_ANYEVENT_MODEL}):\n$@";
1261 } 1434 }
1262 } 1435 }
1263 1436
1264 # check for already loaded models 1437 # check for already loaded models
1265 unless ($MODEL) { 1438 unless ($MODEL) {
1266 for (@REGISTRY, @models) { 1439 for (@REGISTRY, @models) {
1267 my ($package, $model) = @$_; 1440 my ($package, $model) = @$_;
1268 if (${"$package\::VERSION"} > 0) { 1441 if (${"$package\::VERSION"} > 0) {
1269 if (eval "require $model") { 1442 if (eval "require $model") {
1443 AnyEvent::log 7 => "Autodetected model '$model', using it.";
1270 $MODEL = $model; 1444 $MODEL = $model;
1271 warn "AnyEvent: autodetected model '$model', using it.\n" if $VERBOSE >= 2; 1445 last;
1446 } else {
1447 AnyEvent::log 8 => "Detected event loop $package, but cannot load '$model', skipping: $@";
1448 }
1449 }
1450 }
1451
1452 unless ($MODEL) {
1453 # try to autoload a model
1454 for (@REGISTRY, @models) {
1455 my ($package, $model) = @$_;
1456 if (
1457 eval "require $package"
1458 and ${"$package\::VERSION"} > 0
1459 and eval "require $model"
1460 ) {
1461 AnyEvent::log 7 => "Autoloaded model '$model', using it.";
1462 $MODEL = $model;
1272 last; 1463 last;
1273 } 1464 }
1274 } 1465 }
1275 }
1276
1277 unless ($MODEL) {
1278 # try to autoload a model
1279 for (@REGISTRY, @models) {
1280 my ($package, $model, $autoload) = @$_;
1281 if (
1282 $autoload
1283 and eval "require $package"
1284 and ${"$package\::VERSION"} > 0
1285 and eval "require $model"
1286 ) {
1287 $MODEL = $model;
1288 warn "AnyEvent: autoloaded model '$model', using it.\n" if $VERBOSE >= 2;
1289 last;
1290 }
1291 }
1292 1466
1293 $MODEL 1467 $MODEL
1294 or die "No event module selected for AnyEvent and autodetect failed. Install any one of these modules: EV, Event or Glib.\n"; 1468 or AnyEvent::log fatal => "Backend autodetection failed - did you properly install AnyEvent?";
1295 } 1469 }
1296 } 1470 }
1297 1471
1298 @models = (); # free probe data 1472 # free memory only needed for probing
1473 undef @models;
1474 undef @REGISTRY;
1299 1475
1300 push @{"$MODEL\::ISA"}, "AnyEvent::Base"; 1476 push @{"$MODEL\::ISA"}, "AnyEvent::Base";
1301 unshift @ISA, $MODEL;
1302 1477
1303 # now nuke some methods that are overriden by the backend. 1478 # now nuke some methods that are overridden by the backend.
1304 # SUPER is not allowed. 1479 # SUPER usage is not allowed in these.
1305 for (qw(time signal child idle)) { 1480 for (qw(time signal child idle)) {
1306 undef &{"AnyEvent::Base::$_"} 1481 undef &{"AnyEvent::Base::$_"}
1307 if defined &{"$MODEL\::$_"}; 1482 if defined &{"$MODEL\::$_"};
1308 } 1483 }
1309 1484
1310 require AnyEvent::Strict if $ENV{PERL_ANYEVENT_STRICT}; 1485 _isa_set;
1486
1487 # we're officially open!
1488
1489 if ($ENV{PERL_ANYEVENT_STRICT}) {
1490 require AnyEvent::Strict;
1491 }
1492
1493 if ($ENV{PERL_ANYEVENT_DEBUG_WRAP}) {
1494 require AnyEvent::Debug;
1495 AnyEvent::Debug::wrap ($ENV{PERL_ANYEVENT_DEBUG_WRAP});
1496 }
1497
1498 if (length $ENV{PERL_ANYEVENT_DEBUG_SHELL}) {
1499 require AnyEvent::Socket;
1500 require AnyEvent::Debug;
1501
1502 my $shell = $ENV{PERL_ANYEVENT_DEBUG_SHELL};
1503 $shell =~ s/\$\$/$$/g;
1504
1505 my ($host, $service) = AnyEvent::Socket::parse_hostport ($shell);
1506 $AnyEvent::Debug::SHELL = AnyEvent::Debug::shell ($host, $service);
1507 }
1508
1509 # now the anyevent environment is set up as the user told us to, so
1510 # call the actual user code - post detects
1311 1511
1312 (shift @post_detect)->() while @post_detect; 1512 (shift @post_detect)->() while @post_detect;
1513 undef @post_detect;
1313 1514
1314 *post_detect = sub(&) { 1515 *post_detect = sub(&) {
1315 shift->(); 1516 shift->();
1316 1517
1317 undef 1518 undef
1318 }; 1519 };
1319 1520
1320 $MODEL 1521 $MODEL
1321} 1522}
1322 1523
1323sub AUTOLOAD { 1524for my $name (@methods) {
1324 (my $func = $AUTOLOAD) =~ s/.*://; 1525 *$name = sub {
1325
1326 $method{$func}
1327 or Carp::croak "$func: not a valid AnyEvent class method";
1328
1329 detect; 1526 detect;
1330 1527 # we use goto because
1331 my $class = shift; 1528 # a) it makes the thunk more transparent
1332 $class->$func (@_); 1529 # b) it allows us to delete the thunk later
1530 goto &{ UNIVERSAL::can AnyEvent => "SUPER::$name" }
1531 };
1333} 1532}
1334 1533
1335# utility function to dup a filehandle. this is used by many backends 1534# utility function to dup a filehandle. this is used by many backends
1336# to support binding more than one watcher per filehandle (they usually 1535# to support binding more than one watcher per filehandle (they usually
1337# allow only one watcher per fd, so we dup it to get a different one). 1536# allow only one watcher per fd, so we dup it to get a different one).
1361 1560
1362package AE; 1561package AE;
1363 1562
1364our $VERSION = $AnyEvent::VERSION; 1563our $VERSION = $AnyEvent::VERSION;
1365 1564
1565sub _reset() {
1566 eval q{
1366# fall back to the main API by default - backends and AnyEvent::Base 1567 # fall back to the main API by default - backends and AnyEvent::Base
1367# implementations can overwrite these. 1568 # implementations can overwrite these.
1368 1569
1369sub io($$$) { 1570 sub io($$$) {
1370 AnyEvent->io (fh => $_[0], poll => $_[1] ? "w" : "r", cb => $_[2]) 1571 AnyEvent->io (fh => $_[0], poll => $_[1] ? "w" : "r", cb => $_[2])
1371} 1572 }
1372 1573
1373sub timer($$$) { 1574 sub timer($$$) {
1374 AnyEvent->timer (after => $_[0], interval => $_[1], cb => $_[2]) 1575 AnyEvent->timer (after => $_[0], interval => $_[1], cb => $_[2])
1375} 1576 }
1376 1577
1377sub signal($$) { 1578 sub signal($$) {
1378 AnyEvent->signal (signal => $_[0], cb => $_[1]) 1579 AnyEvent->signal (signal => $_[0], cb => $_[1])
1379} 1580 }
1380 1581
1381sub child($$) { 1582 sub child($$) {
1382 AnyEvent->child (pid => $_[0], cb => $_[1]) 1583 AnyEvent->child (pid => $_[0], cb => $_[1])
1383} 1584 }
1384 1585
1385sub idle($) { 1586 sub idle($) {
1386 AnyEvent->idle (cb => $_[0]) 1587 AnyEvent->idle (cb => $_[0]);
1387} 1588 }
1388 1589
1389sub cv(;&) { 1590 sub cv(;&) {
1390 AnyEvent->condvar (@_ ? (cb => $_[0]) : ()) 1591 AnyEvent->condvar (@_ ? (cb => $_[0]) : ())
1391} 1592 }
1392 1593
1393sub now() { 1594 sub now() {
1394 AnyEvent->now 1595 AnyEvent->now
1395} 1596 }
1396 1597
1397sub now_update() { 1598 sub now_update() {
1398 AnyEvent->now_update 1599 AnyEvent->now_update
1399} 1600 }
1400 1601
1401sub time() { 1602 sub time() {
1402 AnyEvent->time 1603 AnyEvent->time
1604 }
1605
1606 *postpone = \&AnyEvent::postpone;
1607 *log = \&AnyEvent::log;
1608 };
1609 die if $@;
1403} 1610}
1611
1612BEGIN { _reset }
1404 1613
1405package AnyEvent::Base; 1614package AnyEvent::Base;
1406 1615
1407# default implementations for many methods 1616# default implementations for many methods
1408 1617
1409sub time { 1618sub time {
1410 eval q{ # poor man's autoloading {} 1619 eval q{ # poor man's autoloading {}
1411 # probe for availability of Time::HiRes 1620 # probe for availability of Time::HiRes
1412 if (eval "use Time::HiRes (); Time::HiRes::time (); 1") { 1621 if (eval "use Time::HiRes (); Time::HiRes::time (); 1") {
1413 warn "AnyEvent: using Time::HiRes for sub-second timing accuracy.\n" if $VERBOSE >= 8; 1622 *time = sub { Time::HiRes::time () };
1414 *AE::time = \&Time::HiRes::time; 1623 *AE::time = \& Time::HiRes::time ;
1624 *now = \&time;
1625 AnyEvent::log 8 => "using Time::HiRes for sub-second timing accuracy.";
1415 # if (eval "use POSIX (); (POSIX::times())... 1626 # if (eval "use POSIX (); (POSIX::times())...
1416 } else { 1627 } else {
1417 warn "AnyEvent: using built-in time(), WARNING, no sub-second resolution!\n" if $VERBOSE; 1628 *time = sub { CORE::time };
1418 *AE::time = sub (){ time }; # epic fail 1629 *AE::time = sub (){ CORE::time };
1630 *now = \&time;
1631 AnyEvent::log 3 => "Using built-in time(), no sub-second resolution!";
1419 } 1632 }
1420
1421 *time = sub { AE::time }; # different prototypes
1422 }; 1633 };
1423 die if $@; 1634 die if $@;
1424 1635
1425 &time 1636 &time
1426} 1637}
1427 1638
1428*now = \&time; 1639*now = \&time;
1429
1430sub now_update { } 1640sub now_update { }
1431 1641
1642sub _poll {
1643 Carp::croak "$AnyEvent::MODEL does not support blocking waits. Caught";
1644}
1645
1432# default implementation for ->condvar 1646# default implementation for ->condvar
1647# in fact, the default should not be overwritten
1433 1648
1434sub condvar { 1649sub condvar {
1435 eval q{ # poor man's autoloading {} 1650 eval q{ # poor man's autoloading {}
1436 *condvar = sub { 1651 *condvar = sub {
1437 bless { @_ == 3 ? (_ae_cb => $_[2]) : () }, "AnyEvent::CondVar" 1652 bless { @_ == 3 ? (_ae_cb => $_[2]) : () }, "AnyEvent::CondVar"
1515 1730
1516sub signal { 1731sub signal {
1517 eval q{ # poor man's autoloading {} 1732 eval q{ # poor man's autoloading {}
1518 # probe for availability of Async::Interrupt 1733 # probe for availability of Async::Interrupt
1519 if (_have_async_interrupt) { 1734 if (_have_async_interrupt) {
1520 warn "AnyEvent: using Async::Interrupt for race-free signal handling.\n" if $VERBOSE >= 8; 1735 AnyEvent::log 8 => "Using Async::Interrupt for race-free signal handling.";
1521 1736
1522 $SIGPIPE_R = new Async::Interrupt::EventPipe; 1737 $SIGPIPE_R = new Async::Interrupt::EventPipe;
1523 $SIG_IO = AE::io $SIGPIPE_R->fileno, 0, \&_signal_exec; 1738 $SIG_IO = AE::io $SIGPIPE_R->fileno, 0, \&_signal_exec;
1524 1739
1525 } else { 1740 } else {
1526 warn "AnyEvent: using emulated perl signal handling with latency timer.\n" if $VERBOSE >= 8; 1741 AnyEvent::log 8 => "Using emulated perl signal handling with latency timer.";
1527 1742
1528 if (AnyEvent::WIN32) { 1743 if (AnyEvent::WIN32) {
1529 require AnyEvent::Util; 1744 require AnyEvent::Util;
1530 1745
1531 ($SIGPIPE_R, $SIGPIPE_W) = AnyEvent::Util::portable_pipe (); 1746 ($SIGPIPE_R, $SIGPIPE_W) = AnyEvent::Util::portable_pipe ();
1607 : sysread $SIGPIPE_R, (my $dummy), 9; 1822 : sysread $SIGPIPE_R, (my $dummy), 9;
1608 1823
1609 while (%SIG_EV) { 1824 while (%SIG_EV) {
1610 for (keys %SIG_EV) { 1825 for (keys %SIG_EV) {
1611 delete $SIG_EV{$_}; 1826 delete $SIG_EV{$_};
1612 $_->() for values %{ $SIG_CB{$_} || {} }; 1827 &$_ for values %{ $SIG_CB{$_} || {} };
1613 } 1828 }
1614 } 1829 }
1615 }; 1830 };
1616 }; 1831 };
1617 die if $@; 1832 die if $@;
1622# default implementation for ->child 1837# default implementation for ->child
1623 1838
1624our %PID_CB; 1839our %PID_CB;
1625our $CHLD_W; 1840our $CHLD_W;
1626our $CHLD_DELAY_W; 1841our $CHLD_DELAY_W;
1627our $WNOHANG;
1628 1842
1629# used by many Impl's 1843# used by many Impl's
1630sub _emit_childstatus($$) { 1844sub _emit_childstatus($$) {
1631 my (undef, $rpid, $rstatus) = @_; 1845 my (undef, $rpid, $rstatus) = @_;
1632 1846
1639 eval q{ # poor man's autoloading {} 1853 eval q{ # poor man's autoloading {}
1640 *_sigchld = sub { 1854 *_sigchld = sub {
1641 my $pid; 1855 my $pid;
1642 1856
1643 AnyEvent->_emit_childstatus ($pid, $?) 1857 AnyEvent->_emit_childstatus ($pid, $?)
1644 while ($pid = waitpid -1, $WNOHANG) > 0; 1858 while ($pid = waitpid -1, WNOHANG) > 0;
1645 }; 1859 };
1646 1860
1647 *child = sub { 1861 *child = sub {
1648 my (undef, %arg) = @_; 1862 my (undef, %arg) = @_;
1649 1863
1650 defined (my $pid = $arg{pid} + 0) 1864 my $pid = $arg{pid};
1651 or Carp::croak "required option 'pid' is missing"; 1865 my $cb = $arg{cb};
1652 1866
1653 $PID_CB{$pid}{$arg{cb}} = $arg{cb}; 1867 $PID_CB{$pid}{$cb+0} = $cb;
1654
1655 # WNOHANG is almost cetrainly 1 everywhere
1656 $WNOHANG ||= $^O =~ /^(?:openbsd|netbsd|linux|freebsd|cygwin|MSWin32)$/
1657 ? 1
1658 : eval { local $SIG{__DIE__}; require POSIX; &POSIX::WNOHANG } || 1;
1659 1868
1660 unless ($CHLD_W) { 1869 unless ($CHLD_W) {
1661 $CHLD_W = AE::signal CHLD => \&_sigchld; 1870 $CHLD_W = AE::signal CHLD => \&_sigchld;
1662 # child could be a zombie already, so make at least one round 1871 # child could be a zombie already, so make at least one round
1663 &_sigchld; 1872 &_sigchld;
1664 } 1873 }
1665 1874
1666 bless [$pid, $arg{cb}], "AnyEvent::Base::child" 1875 bless [$pid, $cb+0], "AnyEvent::Base::child"
1667 }; 1876 };
1668 1877
1669 *AnyEvent::Base::child::DESTROY = sub { 1878 *AnyEvent::Base::child::DESTROY = sub {
1670 my ($pid, $cb) = @{$_[0]}; 1879 my ($pid, $icb) = @{$_[0]};
1671 1880
1672 delete $PID_CB{$pid}{$cb}; 1881 delete $PID_CB{$pid}{$icb};
1673 delete $PID_CB{$pid} unless keys %{ $PID_CB{$pid} }; 1882 delete $PID_CB{$pid} unless keys %{ $PID_CB{$pid} };
1674 1883
1675 undef $CHLD_W unless keys %PID_CB; 1884 undef $CHLD_W unless keys %PID_CB;
1676 }; 1885 };
1677 }; 1886 };
1690 1899
1691 my ($cb, $w, $rcb) = $arg{cb}; 1900 my ($cb, $w, $rcb) = $arg{cb};
1692 1901
1693 $rcb = sub { 1902 $rcb = sub {
1694 if ($cb) { 1903 if ($cb) {
1695 $w = _time; 1904 $w = AE::time;
1696 &$cb; 1905 &$cb;
1697 $w = _time - $w; 1906 $w = AE::time - $w;
1698 1907
1699 # never use more then 50% of the time for the idle watcher, 1908 # never use more then 50% of the time for the idle watcher,
1700 # within some limits 1909 # within some limits
1701 $w = 0.0001 if $w < 0.0001; 1910 $w = 0.0001 if $w < 0.0001;
1702 $w = 5 if $w > 5; 1911 $w = 5 if $w > 5;
1725 1934
1726package AnyEvent::CondVar; 1935package AnyEvent::CondVar;
1727 1936
1728our @ISA = AnyEvent::CondVar::Base::; 1937our @ISA = AnyEvent::CondVar::Base::;
1729 1938
1939# only to be used for subclassing
1940sub new {
1941 my $class = shift;
1942 bless AnyEvent->condvar (@_), $class
1943}
1944
1730package AnyEvent::CondVar::Base; 1945package AnyEvent::CondVar::Base;
1731 1946
1732#use overload 1947#use overload
1733# '&{}' => sub { my $self = shift; sub { $self->send (@_) } }, 1948# '&{}' => sub { my $self = shift; sub { $self->send (@_) } },
1734# fallback => 1; 1949# fallback => 1;
1743 1958
1744sub _send { 1959sub _send {
1745 # nop 1960 # nop
1746} 1961}
1747 1962
1963sub _wait {
1964 AnyEvent->_poll until $_[0]{_ae_sent};
1965}
1966
1748sub send { 1967sub send {
1749 my $cv = shift; 1968 my $cv = shift;
1750 $cv->{_ae_sent} = [@_]; 1969 $cv->{_ae_sent} = [@_];
1751 (delete $cv->{_ae_cb})->($cv) if $cv->{_ae_cb}; 1970 (delete $cv->{_ae_cb})->($cv) if $cv->{_ae_cb};
1752 $cv->_send; 1971 $cv->_send;
1759 1978
1760sub ready { 1979sub ready {
1761 $_[0]{_ae_sent} 1980 $_[0]{_ae_sent}
1762} 1981}
1763 1982
1764sub _wait {
1765 $WAITING
1766 and !$_[0]{_ae_sent}
1767 and Carp::croak "AnyEvent::CondVar: recursive blocking wait detected";
1768
1769 local $WAITING = 1;
1770 AnyEvent->one_event while !$_[0]{_ae_sent};
1771}
1772
1773sub recv { 1983sub recv {
1984 unless ($_[0]{_ae_sent}) {
1985 $WAITING
1986 and Carp::croak "AnyEvent::CondVar: recursive blocking wait attempted";
1987
1988 local $WAITING = 1;
1774 $_[0]->_wait; 1989 $_[0]->_wait;
1990 }
1775 1991
1776 Carp::croak $_[0]{_ae_croak} if $_[0]{_ae_croak}; 1992 $_[0]{_ae_croak}
1777 wantarray ? @{ $_[0]{_ae_sent} } : $_[0]{_ae_sent}[0] 1993 and Carp::croak $_[0]{_ae_croak};
1994
1995 wantarray
1996 ? @{ $_[0]{_ae_sent} }
1997 : $_[0]{_ae_sent}[0]
1778} 1998}
1779 1999
1780sub cb { 2000sub cb {
1781 my $cv = shift; 2001 my $cv = shift;
1782 2002
1798 &{ $_[0]{_ae_end_cb} || sub { $_[0]->send } }; 2018 &{ $_[0]{_ae_end_cb} || sub { $_[0]->send } };
1799} 2019}
1800 2020
1801# undocumented/compatibility with pre-3.4 2021# undocumented/compatibility with pre-3.4
1802*broadcast = \&send; 2022*broadcast = \&send;
1803*wait = \&_wait; 2023*wait = \&recv;
1804 2024
1805=head1 ERROR AND EXCEPTION HANDLING 2025=head1 ERROR AND EXCEPTION HANDLING
1806 2026
1807In general, AnyEvent does not do any error handling - it relies on the 2027In general, AnyEvent does not do any error handling - it relies on the
1808caller to do that if required. The L<AnyEvent::Strict> module (see also 2028caller to do that if required. The L<AnyEvent::Strict> module (see also
1820$Event/EV::DIED->() >>, L<Glib> uses C<< install_exception_handler >> and 2040$Event/EV::DIED->() >>, L<Glib> uses C<< install_exception_handler >> and
1821so on. 2041so on.
1822 2042
1823=head1 ENVIRONMENT VARIABLES 2043=head1 ENVIRONMENT VARIABLES
1824 2044
1825The following environment variables are used by this module or its 2045AnyEvent supports a number of environment variables that tune the
1826submodules. 2046runtime behaviour. They are usually evaluated when AnyEvent is
2047loaded, initialised, or a submodule that uses them is loaded. Many of
2048them also cause AnyEvent to load additional modules - for example,
2049C<PERL_ANYEVENT_DEBUG_WRAP> causes the L<AnyEvent::Debug> module to be
2050loaded.
1827 2051
1828Note that AnyEvent will remove I<all> environment variables starting with 2052All the environment variables documented here start with
1829C<PERL_ANYEVENT_> from C<%ENV> when it is loaded while taint mode is 2053C<PERL_ANYEVENT_>, which is what AnyEvent considers its own
1830enabled. 2054namespace. Other modules are encouraged (but by no means required) to use
2055C<PERL_ANYEVENT_SUBMODULE> if they have registered the AnyEvent::Submodule
2056namespace on CPAN, for any submodule. For example, L<AnyEvent::HTTP> could
2057be expected to use C<PERL_ANYEVENT_HTTP_PROXY> (it should not access env
2058variables starting with C<AE_>, see below).
2059
2060All variables can also be set via the C<AE_> prefix, that is, instead
2061of setting C<PERL_ANYEVENT_VERBOSE> you can also set C<AE_VERBOSE>. In
2062case there is a clash btween anyevent and another program that uses
2063C<AE_something> you can set the corresponding C<PERL_ANYEVENT_something>
2064variable to the empty string, as those variables take precedence.
2065
2066When AnyEvent is first loaded, it copies all C<AE_xxx> env variables
2067to their C<PERL_ANYEVENT_xxx> counterpart unless that variable already
2068exists. If taint mode is on, then AnyEvent will remove I<all> environment
2069variables starting with C<PERL_ANYEVENT_> from C<%ENV> (or replace them
2070with C<undef> or the empty string, if the corresaponding C<AE_> variable
2071is set).
2072
2073The exact algorithm is currently:
2074
2075 1. if taint mode enabled, delete all PERL_ANYEVENT_xyz variables from %ENV
2076 2. copy over AE_xyz to PERL_ANYEVENT_xyz unless the latter alraedy exists
2077 3. if taint mode enabled, set all PERL_ANYEVENT_xyz variables to undef.
2078
2079This ensures that child processes will not see the C<AE_> variables.
2080
2081The following environment variables are currently known to AnyEvent:
1831 2082
1832=over 4 2083=over 4
1833 2084
1834=item C<PERL_ANYEVENT_VERBOSE> 2085=item C<PERL_ANYEVENT_VERBOSE>
1835 2086
1836By default, AnyEvent will be completely silent except in fatal 2087By default, AnyEvent will log messages with loglevel C<4> (C<error>) or
1837conditions. You can set this environment variable to make AnyEvent more 2088higher (see L<AnyEvent::Log>). You can set this environment variable to a
1838talkative. 2089numerical loglevel to make AnyEvent more (or less) talkative.
1839 2090
2091If you want to do more than just set the global logging level
2092you should have a look at C<PERL_ANYEVENT_LOG>, which allows much more
2093complex specifications.
2094
2095When set to C<0> (C<off>), then no messages whatsoever will be logged with
2096everything else at defaults.
2097
1840When set to C<1> or higher, causes AnyEvent to warn about unexpected 2098When set to C<5> or higher (C<warn>), AnyEvent warns about unexpected
1841conditions, such as not being able to load the event model specified by 2099conditions, such as not being able to load the event model specified by
1842C<PERL_ANYEVENT_MODEL>. 2100C<PERL_ANYEVENT_MODEL>, or a guard callback throwing an exception - this
2101is the minimum recommended level for use during development.
1843 2102
1844When set to C<2> or higher, cause AnyEvent to report to STDERR which event 2103When set to C<7> or higher (info), AnyEvent reports which event model it
1845model it chooses. 2104chooses.
1846 2105
1847When set to C<8> or higher, then AnyEvent will report extra information on 2106When set to C<8> or higher (debug), then AnyEvent will report extra
1848which optional modules it loads and how it implements certain features. 2107information on which optional modules it loads and how it implements
2108certain features.
2109
2110=item C<PERL_ANYEVENT_LOG>
2111
2112Accepts rather complex logging specifications. For example, you could log
2113all C<debug> messages of some module to stderr, warnings and above to
2114stderr, and errors and above to syslog, with:
2115
2116 PERL_ANYEVENT_LOG=Some::Module=debug,+log:filter=warn,+%syslog:%syslog=error,syslog
2117
2118For the rather extensive details, see L<AnyEvent::Log>.
2119
2120This variable is evaluated when AnyEvent (or L<AnyEvent::Log>) is loaded,
2121so will take effect even before AnyEvent has initialised itself.
2122
2123Note that specifying this environment variable causes the L<AnyEvent::Log>
2124module to be loaded, while C<PERL_ANYEVENT_VERBOSE> does not, so only
2125using the latter saves a few hundred kB of memory unless a module
2126explicitly needs the extra features of AnyEvent::Log.
1849 2127
1850=item C<PERL_ANYEVENT_STRICT> 2128=item C<PERL_ANYEVENT_STRICT>
1851 2129
1852AnyEvent does not do much argument checking by default, as thorough 2130AnyEvent does not do much argument checking by default, as thorough
1853argument checking is very costly. Setting this variable to a true value 2131argument checking is very costly. Setting this variable to a true value
1860Unlike C<use strict> (or its modern cousin, C<< use L<common::sense> 2138Unlike C<use strict> (or its modern cousin, C<< use L<common::sense>
1861>>, it is definitely recommended to keep it off in production. Keeping 2139>>, it is definitely recommended to keep it off in production. Keeping
1862C<PERL_ANYEVENT_STRICT=1> in your environment while developing programs 2140C<PERL_ANYEVENT_STRICT=1> in your environment while developing programs
1863can be very useful, however. 2141can be very useful, however.
1864 2142
2143=item C<PERL_ANYEVENT_DEBUG_SHELL>
2144
2145If this env variable is nonempty, then its contents will be interpreted by
2146C<AnyEvent::Socket::parse_hostport> and C<AnyEvent::Debug::shell> (after
2147replacing every occurance of C<$$> by the process pid). The shell object
2148is saved in C<$AnyEvent::Debug::SHELL>.
2149
2150This happens when the first watcher is created.
2151
2152For example, to bind a debug shell on a unix domain socket in
2153F<< /tmp/debug<pid>.sock >>, you could use this:
2154
2155 PERL_ANYEVENT_DEBUG_SHELL=/tmp/debug\$\$.sock perlprog
2156 # connect with e.g.: socat readline /tmp/debug123.sock
2157
2158Or to bind to tcp port 4545 on localhost:
2159
2160 PERL_ANYEVENT_DEBUG_SHELL=127.0.0.1:4545 perlprog
2161 # connect with e.g.: telnet localhost 4545
2162
2163Note that creating sockets in F</tmp> or on localhost is very unsafe on
2164multiuser systems.
2165
2166=item C<PERL_ANYEVENT_DEBUG_WRAP>
2167
2168Can be set to C<0>, C<1> or C<2> and enables wrapping of all watchers for
2169debugging purposes. See C<AnyEvent::Debug::wrap> for details.
2170
1865=item C<PERL_ANYEVENT_MODEL> 2171=item C<PERL_ANYEVENT_MODEL>
1866 2172
1867This can be used to specify the event model to be used by AnyEvent, before 2173This can be used to specify the event model to be used by AnyEvent, before
1868auto detection and -probing kicks in. It must be a string consisting 2174auto detection and -probing kicks in.
1869entirely of ASCII letters. The string C<AnyEvent::Impl::> gets prepended 2175
2176It normally is a string consisting entirely of ASCII letters (e.g. C<EV>
2177or C<IOAsync>). The string C<AnyEvent::Impl::> gets prepended and the
1870and the resulting module name is loaded and if the load was successful, 2178resulting module name is loaded and - if the load was successful - used as
1871used as event model. If it fails to load AnyEvent will proceed with 2179event model backend. If it fails to load then AnyEvent will proceed with
1872auto detection and -probing. 2180auto detection and -probing.
1873 2181
1874This functionality might change in future versions. 2182If the string ends with C<::> instead (e.g. C<AnyEvent::Impl::EV::>) then
2183nothing gets prepended and the module name is used as-is (hint: C<::> at
2184the end of a string designates a module name and quotes it appropriately).
1875 2185
1876For example, to force the pure perl model (L<AnyEvent::Impl::Perl>) you 2186For example, to force the pure perl model (L<AnyEvent::Loop::Perl>) you
1877could start your program like this: 2187could start your program like this:
1878 2188
1879 PERL_ANYEVENT_MODEL=Perl perl ... 2189 PERL_ANYEVENT_MODEL=Perl perl ...
2190
2191=item C<PERL_ANYEVENT_IO_MODEL>
2192
2193The current file I/O model - see L<AnyEvent::IO> for more info.
2194
2195At the moment, only C<Perl> (small, pure-perl, synchronous) and
2196C<IOAIO> (truly asynchronous) are supported. The default is C<IOAIO> if
2197L<AnyEvent::AIO> can be loaded, otherwise it is C<Perl>.
1880 2198
1881=item C<PERL_ANYEVENT_PROTOCOLS> 2199=item C<PERL_ANYEVENT_PROTOCOLS>
1882 2200
1883Used by both L<AnyEvent::DNS> and L<AnyEvent::Socket> to determine preferences 2201Used by both L<AnyEvent::DNS> and L<AnyEvent::Socket> to determine preferences
1884for IPv4 or IPv6. The default is unspecified (and might change, or be the result 2202for IPv4 or IPv6. The default is unspecified (and might change, or be the result
1897but support both and try to use both. C<PERL_ANYEVENT_PROTOCOLS=ipv4> 2215but support both and try to use both. C<PERL_ANYEVENT_PROTOCOLS=ipv4>
1898- only support IPv4, never try to resolve or contact IPv6 2216- only support IPv4, never try to resolve or contact IPv6
1899addresses. C<PERL_ANYEVENT_PROTOCOLS=ipv6,ipv4> support either IPv4 or 2217addresses. C<PERL_ANYEVENT_PROTOCOLS=ipv6,ipv4> support either IPv4 or
1900IPv6, but prefer IPv6 over IPv4. 2218IPv6, but prefer IPv6 over IPv4.
1901 2219
2220=item C<PERL_ANYEVENT_HOSTS>
2221
2222This variable, if specified, overrides the F</etc/hosts> file used by
2223L<AnyEvent::Socket>C<::resolve_sockaddr>, i.e. hosts aliases will be read
2224from that file instead.
2225
1902=item C<PERL_ANYEVENT_EDNS0> 2226=item C<PERL_ANYEVENT_EDNS0>
1903 2227
1904Used by L<AnyEvent::DNS> to decide whether to use the EDNS0 extension 2228Used by L<AnyEvent::DNS> to decide whether to use the EDNS0 extension for
1905for DNS. This extension is generally useful to reduce DNS traffic, but 2229DNS. This extension is generally useful to reduce DNS traffic, especially
1906some (broken) firewalls drop such DNS packets, which is why it is off by 2230when DNSSEC is involved, but some (broken) firewalls drop such DNS
1907default. 2231packets, which is why it is off by default.
1908 2232
1909Setting this variable to C<1> will cause L<AnyEvent::DNS> to announce 2233Setting this variable to C<1> will cause L<AnyEvent::DNS> to announce
1910EDNS0 in its DNS requests. 2234EDNS0 in its DNS requests.
1911 2235
1912=item C<PERL_ANYEVENT_MAX_FORKS> 2236=item C<PERL_ANYEVENT_MAX_FORKS>
1918 2242
1919The default value for the C<max_outstanding> parameter for the default DNS 2243The default value for the C<max_outstanding> parameter for the default DNS
1920resolver - this is the maximum number of parallel DNS requests that are 2244resolver - this is the maximum number of parallel DNS requests that are
1921sent to the DNS server. 2245sent to the DNS server.
1922 2246
2247=item C<PERL_ANYEVENT_MAX_SIGNAL_LATENCY>
2248
2249Perl has inherently racy signal handling (you can basically choose between
2250losing signals and memory corruption) - pure perl event loops (including
2251C<AnyEvent::Loop>, when C<Async::Interrupt> isn't available) therefore
2252have to poll regularly to avoid losing signals.
2253
2254Some event loops are racy, but don't poll regularly, and some event loops
2255are written in C but are still racy. For those event loops, AnyEvent
2256installs a timer that regularly wakes up the event loop.
2257
2258By default, the interval for this timer is C<10> seconds, but you can
2259override this delay with this environment variable (or by setting
2260the C<$AnyEvent::MAX_SIGNAL_LATENCY> variable before creating signal
2261watchers).
2262
2263Lower values increase CPU (and energy) usage, higher values can introduce
2264long delays when reaping children or waiting for signals.
2265
2266The L<AnyEvent::Async> module, if available, will be used to avoid this
2267polling (with most event loops).
2268
1923=item C<PERL_ANYEVENT_RESOLV_CONF> 2269=item C<PERL_ANYEVENT_RESOLV_CONF>
1924 2270
1925The file to use instead of F</etc/resolv.conf> (or OS-specific 2271The absolute path to a F<resolv.conf>-style file to use instead of
1926configuration) in the default resolver. When set to the empty string, no 2272F</etc/resolv.conf> (or the OS-specific configuration) in the default
1927default config will be used. 2273resolver, or the empty string to select the default configuration.
1928 2274
1929=item C<PERL_ANYEVENT_CA_FILE>, C<PERL_ANYEVENT_CA_PATH>. 2275=item C<PERL_ANYEVENT_CA_FILE>, C<PERL_ANYEVENT_CA_PATH>.
1930 2276
1931When neither C<ca_file> nor C<ca_path> was specified during 2277When neither C<ca_file> nor C<ca_path> was specified during
1932L<AnyEvent::TLS> context creation, and either of these environment 2278L<AnyEvent::TLS> context creation, and either of these environment
1933variables exist, they will be used to specify CA certificate locations 2279variables are nonempty, they will be used to specify CA certificate
1934instead of a system-dependent default. 2280locations instead of a system-dependent default.
1935 2281
1936=item C<PERL_ANYEVENT_AVOID_GUARD> and C<PERL_ANYEVENT_AVOID_ASYNC_INTERRUPT> 2282=item C<PERL_ANYEVENT_AVOID_GUARD> and C<PERL_ANYEVENT_AVOID_ASYNC_INTERRUPT>
1937 2283
1938When these are set to C<1>, then the respective modules are not 2284When these are set to C<1>, then the respective modules are not
1939loaded. Mostly good for testing AnyEvent itself. 2285loaded. Mostly good for testing AnyEvent itself.
2271(even when used without AnyEvent), but most event loops have acceptable 2617(even when used without AnyEvent), but most event loops have acceptable
2272performance with or without AnyEvent. 2618performance with or without AnyEvent.
2273 2619
2274=item * The overhead AnyEvent adds is usually much smaller than the overhead of 2620=item * The overhead AnyEvent adds is usually much smaller than the overhead of
2275the actual event loop, only with extremely fast event loops such as EV 2621the actual event loop, only with extremely fast event loops such as EV
2276adds AnyEvent significant overhead. 2622does AnyEvent add significant overhead.
2277 2623
2278=item * You should avoid POE like the plague if you want performance or 2624=item * You should avoid POE like the plague if you want performance or
2279reasonable memory usage. 2625reasonable memory usage.
2280 2626
2281=back 2627=back
2580 2926
2581=item L<Time::HiRes> 2927=item L<Time::HiRes>
2582 2928
2583This module is part of perl since release 5.008. It will be used when the 2929This module is part of perl since release 5.008. It will be used when the
2584chosen event library does not come with a timing source of its own. The 2930chosen event library does not come with a timing source of its own. The
2585pure-perl event loop (L<AnyEvent::Impl::Perl>) will additionally use it to 2931pure-perl event loop (L<AnyEvent::Loop>) will additionally load it to
2586try to use a monotonic clock for timing stability. 2932try to use a monotonic clock for timing stability.
2933
2934=item L<AnyEvent::AIO> (and L<IO::AIO>)
2935
2936The default implementation of L<AnyEvent::IO> is to do I/O synchronously,
2937stopping programs while they access the disk, which is fine for a lot of
2938programs.
2939
2940Installing AnyEvent::AIO (and its IO::AIO dependency) makes it switch to
2941a true asynchronous implementation, so event processing can continue even
2942while waiting for disk I/O.
2587 2943
2588=back 2944=back
2589 2945
2590 2946
2591=head1 FORK 2947=head1 FORK
2652pronounced). 3008pronounced).
2653 3009
2654 3010
2655=head1 SEE ALSO 3011=head1 SEE ALSO
2656 3012
2657Utility functions: L<AnyEvent::Util>. 3013Tutorial/Introduction: L<AnyEvent::Intro>.
2658 3014
2659Event modules: L<EV>, L<EV::Glib>, L<Glib::EV>, L<Event>, L<Glib::Event>, 3015FAQ: L<AnyEvent::FAQ>.
2660L<Glib>, L<Tk>, L<Event::Lib>, L<Qt>, L<POE>. 3016
3017Utility functions: L<AnyEvent::Util> (misc. grab-bag), L<AnyEvent::Log>
3018(simply logging).
3019
3020Development/Debugging: L<AnyEvent::Strict> (stricter checking),
3021L<AnyEvent::Debug> (interactive shell, watcher tracing).
3022
3023Supported event modules: L<AnyEvent::Loop>, L<EV>, L<EV::Glib>,
3024L<Glib::EV>, L<Event>, L<Glib::Event>, L<Glib>, L<Tk>, L<Event::Lib>,
3025L<Qt>, L<POE>, L<FLTK>.
2661 3026
2662Implementations: L<AnyEvent::Impl::EV>, L<AnyEvent::Impl::Event>, 3027Implementations: L<AnyEvent::Impl::EV>, L<AnyEvent::Impl::Event>,
2663L<AnyEvent::Impl::Glib>, L<AnyEvent::Impl::Tk>, L<AnyEvent::Impl::Perl>, 3028L<AnyEvent::Impl::Glib>, L<AnyEvent::Impl::Tk>, L<AnyEvent::Impl::Perl>,
2664L<AnyEvent::Impl::EventLib>, L<AnyEvent::Impl::Qt>, 3029L<AnyEvent::Impl::EventLib>, L<AnyEvent::Impl::Qt>,
2665L<AnyEvent::Impl::POE>, L<AnyEvent::Impl::IOAsync>, L<Anyevent::Impl::Irssi>. 3030L<AnyEvent::Impl::POE>, L<AnyEvent::Impl::IOAsync>, L<Anyevent::Impl::Irssi>,
3031L<AnyEvent::Impl::FLTK>.
2666 3032
2667Non-blocking file handles, sockets, TCP clients and 3033Non-blocking handles, pipes, stream sockets, TCP clients and
2668servers: L<AnyEvent::Handle>, L<AnyEvent::Socket>, L<AnyEvent::TLS>. 3034servers: L<AnyEvent::Handle>, L<AnyEvent::Socket>, L<AnyEvent::TLS>.
2669 3035
3036Asynchronous File I/O: L<AnyEvent::IO>.
3037
2670Asynchronous DNS: L<AnyEvent::DNS>. 3038Asynchronous DNS: L<AnyEvent::DNS>.
2671 3039
2672Coroutine support: L<Coro>, L<Coro::AnyEvent>, L<Coro::EV>, 3040Thread support: L<Coro>, L<Coro::AnyEvent>, L<Coro::EV>, L<Coro::Event>.
2673L<Coro::Event>,
2674 3041
2675Nontrivial usage examples: L<AnyEvent::GPSD>, L<AnyEvent::XMPP>, 3042Nontrivial usage examples: L<AnyEvent::GPSD>, L<AnyEvent::IRC>,
2676L<AnyEvent::HTTP>. 3043L<AnyEvent::HTTP>.
2677 3044
2678 3045
2679=head1 AUTHOR 3046=head1 AUTHOR
2680 3047
2681 Marc Lehmann <schmorp@schmorp.de> 3048 Marc Lehmann <schmorp@schmorp.de>
2682 http://home.schmorp.de/ 3049 http://anyevent.schmorp.de
2683 3050
2684=cut 3051=cut
2685 3052
26861 30531
2687 3054

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines