… | |
… | |
46 | in a tutorial or some gentle introduction, have a look at the |
46 | in a tutorial or some gentle introduction, have a look at the |
47 | L<AnyEvent::Intro> manpage. |
47 | L<AnyEvent::Intro> manpage. |
48 | |
48 | |
49 | =head1 SUPPORT |
49 | =head1 SUPPORT |
50 | |
50 | |
|
|
51 | An FAQ document is available as L<AnyEvent::FAQ>. |
|
|
52 | |
51 | There is a mailinglist for discussing all things AnyEvent, and an IRC |
53 | There also is a mailinglist for discussing all things AnyEvent, and an IRC |
52 | channel, too. |
54 | channel, too. |
53 | |
55 | |
54 | See the AnyEvent project page at the B<Schmorpforge Ta-Sa Software |
56 | See the AnyEvent project page at the B<Schmorpforge Ta-Sa Software |
55 | Repository>, at L<http://anyevent.schmorp.de>, for more info. |
57 | Repository>, at L<http://anyevent.schmorp.de>, for more info. |
56 | |
58 | |
… | |
… | |
83 | that isn't them. What's worse, all the potential users of your |
85 | that isn't them. What's worse, all the potential users of your |
84 | module are I<also> forced to use the same event loop you use. |
86 | module are I<also> forced to use the same event loop you use. |
85 | |
87 | |
86 | AnyEvent is different: AnyEvent + POE works fine. AnyEvent + Glib works |
88 | AnyEvent is different: AnyEvent + POE works fine. AnyEvent + Glib works |
87 | fine. AnyEvent + Tk works fine etc. etc. but none of these work together |
89 | fine. AnyEvent + Tk works fine etc. etc. but none of these work together |
88 | with the rest: POE + IO::Async? No go. Tk + Event? No go. Again: if |
90 | with the rest: POE + EV? No go. Tk + Event? No go. Again: if your module |
89 | your module uses one of those, every user of your module has to use it, |
91 | uses one of those, every user of your module has to use it, too. But if |
90 | too. But if your module uses AnyEvent, it works transparently with all |
92 | your module uses AnyEvent, it works transparently with all event models it |
91 | event models it supports (including stuff like IO::Async, as long as those |
93 | supports (including stuff like IO::Async, as long as those use one of the |
92 | use one of the supported event loops. It is easy to add new event loops |
94 | supported event loops. It is easy to add new event loops to AnyEvent, too, |
93 | to AnyEvent, too, so it is future-proof). |
95 | so it is future-proof). |
94 | |
96 | |
95 | In addition to being free of having to use I<the one and only true event |
97 | In addition to being free of having to use I<the one and only true event |
96 | model>, AnyEvent also is free of bloat and policy: with POE or similar |
98 | model>, AnyEvent also is free of bloat and policy: with POE or similar |
97 | modules, you get an enormous amount of code and strict rules you have to |
99 | modules, you get an enormous amount of code and strict rules you have to |
98 | follow. AnyEvent, on the other hand, is lean and to the point, by only |
100 | follow. AnyEvent, on the other hand, is lean and to the point, by only |
… | |
… | |
119 | The interface itself is vaguely similar, but not identical to the L<Event> |
121 | The interface itself is vaguely similar, but not identical to the L<Event> |
120 | module. |
122 | module. |
121 | |
123 | |
122 | During the first call of any watcher-creation method, the module tries |
124 | During the first call of any watcher-creation method, the module tries |
123 | to detect the currently loaded event loop by probing whether one of the |
125 | to detect the currently loaded event loop by probing whether one of the |
124 | following modules is already loaded: L<EV>, |
126 | following modules is already loaded: L<EV>, L<AnyEvent::Impl::Perl>, |
125 | L<Event>, L<Glib>, L<AnyEvent::Impl::Perl>, L<Tk>, L<Event::Lib>, L<Qt>, |
127 | L<Event>, L<Glib>, L<Tk>, L<Event::Lib>, L<Qt>, L<POE>. The first one |
126 | L<POE>. The first one found is used. If none are found, the module tries |
128 | found is used. If none are detected, the module tries to load the first |
127 | to load these modules (excluding Tk, Event::Lib, Qt and POE as the pure perl |
129 | four modules in the order given; but note that if L<EV> is not |
128 | adaptor should always succeed) in the order given. The first one that can |
130 | available, the pure-perl L<AnyEvent::Impl::Perl> should always work, so |
129 | be successfully loaded will be used. If, after this, still none could be |
131 | the other two are not normally tried. |
130 | found, AnyEvent will fall back to a pure-perl event loop, which is not |
|
|
131 | very efficient, but should work everywhere. |
|
|
132 | |
132 | |
133 | Because AnyEvent first checks for modules that are already loaded, loading |
133 | Because AnyEvent first checks for modules that are already loaded, loading |
134 | an event model explicitly before first using AnyEvent will likely make |
134 | an event model explicitly before first using AnyEvent will likely make |
135 | that model the default. For example: |
135 | that model the default. For example: |
136 | |
136 | |
… | |
… | |
876 | AnyEvent::Impl::Glib based on Glib, slow but very stable. |
876 | AnyEvent::Impl::Glib based on Glib, slow but very stable. |
877 | AnyEvent::Impl::Tk based on Tk, very broken. |
877 | AnyEvent::Impl::Tk based on Tk, very broken. |
878 | AnyEvent::Impl::EventLib based on Event::Lib, leaks memory and worse. |
878 | AnyEvent::Impl::EventLib based on Event::Lib, leaks memory and worse. |
879 | AnyEvent::Impl::POE based on POE, very slow, some limitations. |
879 | AnyEvent::Impl::POE based on POE, very slow, some limitations. |
880 | AnyEvent::Impl::Irssi used when running within irssi. |
880 | AnyEvent::Impl::Irssi used when running within irssi. |
|
|
881 | AnyEvent::Impl::IOAsync based on IO::Async. |
|
|
882 | AnyEvent::Impl::Cocoa based on Cocoa::EventLoop. |
881 | |
883 | |
882 | =item Backends with special needs. |
884 | =item Backends with special needs. |
883 | |
885 | |
884 | Qt requires the Qt::Application to be instantiated first, but will |
886 | Qt requires the Qt::Application to be instantiated first, but will |
885 | otherwise be picked up automatically. As long as the main program |
887 | otherwise be picked up automatically. As long as the main program |
886 | instantiates the application before any AnyEvent watchers are created, |
888 | instantiates the application before any AnyEvent watchers are created, |
887 | everything should just work. |
889 | everything should just work. |
888 | |
890 | |
889 | AnyEvent::Impl::Qt based on Qt. |
891 | AnyEvent::Impl::Qt based on Qt. |
890 | |
|
|
891 | Support for IO::Async can only be partial, as it is too broken and |
|
|
892 | architecturally limited to even support the AnyEvent API. It also |
|
|
893 | is the only event loop that needs the loop to be set explicitly, so |
|
|
894 | it can only be used by a main program knowing about AnyEvent. See |
|
|
895 | L<AnyEvent::Impl::IOAsync> for the gory details. |
|
|
896 | |
|
|
897 | AnyEvent::Impl::IOAsync based on IO::Async, cannot be autoprobed. |
|
|
898 | |
892 | |
899 | =item Event loops that are indirectly supported via other backends. |
893 | =item Event loops that are indirectly supported via other backends. |
900 | |
894 | |
901 | Some event loops can be supported via other modules: |
895 | Some event loops can be supported via other modules: |
902 | |
896 | |
… | |
… | |
1157 | |
1151 | |
1158 | package AnyEvent; |
1152 | package AnyEvent; |
1159 | |
1153 | |
1160 | # basically a tuned-down version of common::sense |
1154 | # basically a tuned-down version of common::sense |
1161 | sub common_sense { |
1155 | sub common_sense { |
1162 | # from common:.sense 1.0 |
1156 | # from common:.sense 3.4 |
1163 | ${^WARNING_BITS} = "\xfc\x3f\x33\x00\x0f\xf3\xcf\xc0\xf3\xfc\x33\x00"; |
1157 | ${^WARNING_BITS} ^= ${^WARNING_BITS} ^ "\x3c\x3f\x33\x00\x0f\xf0\x0f\xc0\xf0\xfc\x33\x00"; |
1164 | # use strict vars subs - NO UTF-8, as Util.pm doesn't like this atm. (uts46data.pl) |
1158 | # use strict vars subs - NO UTF-8, as Util.pm doesn't like this atm. (uts46data.pl) |
1165 | $^H |= 0x00000600; |
1159 | $^H |= 0x00000600; |
1166 | } |
1160 | } |
1167 | |
1161 | |
1168 | BEGIN { AnyEvent::common_sense } |
1162 | BEGIN { AnyEvent::common_sense } |
1169 | |
1163 | |
1170 | use Carp (); |
1164 | use Carp (); |
1171 | |
1165 | |
1172 | our $VERSION = '5.271'; |
1166 | our $VERSION = '5.33'; |
1173 | our $MODEL; |
1167 | our $MODEL; |
1174 | |
1168 | |
1175 | our $AUTOLOAD; |
1169 | our $AUTOLOAD; |
1176 | our @ISA; |
1170 | our @ISA; |
1177 | |
1171 | |
… | |
… | |
1215 | [Tk:: => AnyEvent::Impl::Tk::], # crashes with many handles |
1209 | [Tk:: => AnyEvent::Impl::Tk::], # crashes with many handles |
1216 | [Qt:: => AnyEvent::Impl::Qt::], # requires special main program |
1210 | [Qt:: => AnyEvent::Impl::Qt::], # requires special main program |
1217 | [POE::Kernel:: => AnyEvent::Impl::POE::], # lasciate ogni speranza |
1211 | [POE::Kernel:: => AnyEvent::Impl::POE::], # lasciate ogni speranza |
1218 | [Wx:: => AnyEvent::Impl::POE::], |
1212 | [Wx:: => AnyEvent::Impl::POE::], |
1219 | [Prima:: => AnyEvent::Impl::POE::], |
1213 | [Prima:: => AnyEvent::Impl::POE::], |
1220 | # IO::Async is just too broken - we would need workarounds for its |
|
|
1221 | # byzantine signal and broken child handling, among others. |
|
|
1222 | # IO::Async is rather hard to detect, as it doesn't have any |
|
|
1223 | # obvious default class. |
|
|
1224 | [IO::Async:: => AnyEvent::Impl::IOAsync::], # requires special main program |
|
|
1225 | [IO::Async::Loop:: => AnyEvent::Impl::IOAsync::], # requires special main program |
1214 | [IO::Async::Loop:: => AnyEvent::Impl::IOAsync::], |
1226 | [IO::Async::Notifier:: => AnyEvent::Impl::IOAsync::], # requires special main program |
1215 | [Cocoa::EventLoop:: => AnyEvent::Impl::Cocoa::], |
1227 | [AnyEvent::Impl::IOAsync:: => AnyEvent::Impl::IOAsync::], # requires special main program |
|
|
1228 | ); |
1216 | ); |
1229 | |
1217 | |
1230 | our %method = map +($_ => 1), |
1218 | our %method = map +($_ => 1), |
1231 | qw(io timer time now now_update signal child idle condvar one_event DESTROY); |
1219 | qw(io timer time now now_update signal child idle condvar one_event DESTROY); |
1232 | |
1220 | |
… | |
… | |
1291 | last; |
1279 | last; |
1292 | } |
1280 | } |
1293 | } |
1281 | } |
1294 | |
1282 | |
1295 | $MODEL |
1283 | $MODEL |
1296 | or die "No event module selected for AnyEvent and autodetect failed. Install any one of these modules: EV, Event or Glib.\n"; |
1284 | or die "AnyEvent: backend autodetection failed - did you properly install AnyEvent?\n"; |
1297 | } |
1285 | } |
1298 | } |
1286 | } |
1299 | |
1287 | |
1300 | @models = (); # free probe data |
1288 | @models = (); # free probe data |
1301 | |
1289 | |
1302 | push @{"$MODEL\::ISA"}, "AnyEvent::Base"; |
1290 | push @{"$MODEL\::ISA"}, "AnyEvent::Base"; |
1303 | unshift @ISA, $MODEL; |
1291 | unshift @ISA, $MODEL; |
1304 | |
1292 | |
1305 | # now nuke some methods that are overriden by the backend. |
1293 | # now nuke some methods that are overridden by the backend. |
1306 | # SUPER is not allowed. |
1294 | # SUPER is not allowed. |
1307 | for (qw(time signal child idle)) { |
1295 | for (qw(time signal child idle)) { |
1308 | undef &{"AnyEvent::Base::$_"} |
1296 | undef &{"AnyEvent::Base::$_"} |
1309 | if defined &{"$MODEL\::$_"}; |
1297 | if defined &{"$MODEL\::$_"}; |
1310 | } |
1298 | } |
1311 | |
1299 | |
1312 | require AnyEvent::Strict if $ENV{PERL_ANYEVENT_STRICT}; |
1300 | if ($ENV{PERL_ANYEVENT_STRICT}) { |
|
|
1301 | eval { require AnyEvent::Strict }; |
|
|
1302 | warn "AnyEvent: cannot load AnyEvent::Strict: $@" |
|
|
1303 | if $@ && $VERBOSE; |
|
|
1304 | } |
1313 | |
1305 | |
1314 | (shift @post_detect)->() while @post_detect; |
1306 | (shift @post_detect)->() while @post_detect; |
1315 | |
1307 | |
1316 | *post_detect = sub(&) { |
1308 | *post_detect = sub(&) { |
1317 | shift->(); |
1309 | shift->(); |
… | |
… | |
1624 | # default implementation for ->child |
1616 | # default implementation for ->child |
1625 | |
1617 | |
1626 | our %PID_CB; |
1618 | our %PID_CB; |
1627 | our $CHLD_W; |
1619 | our $CHLD_W; |
1628 | our $CHLD_DELAY_W; |
1620 | our $CHLD_DELAY_W; |
1629 | our $WNOHANG; |
|
|
1630 | |
1621 | |
1631 | # used by many Impl's |
1622 | # used by many Impl's |
1632 | sub _emit_childstatus($$) { |
1623 | sub _emit_childstatus($$) { |
1633 | my (undef, $rpid, $rstatus) = @_; |
1624 | my (undef, $rpid, $rstatus) = @_; |
1634 | |
1625 | |
… | |
… | |
1641 | eval q{ # poor man's autoloading {} |
1632 | eval q{ # poor man's autoloading {} |
1642 | *_sigchld = sub { |
1633 | *_sigchld = sub { |
1643 | my $pid; |
1634 | my $pid; |
1644 | |
1635 | |
1645 | AnyEvent->_emit_childstatus ($pid, $?) |
1636 | AnyEvent->_emit_childstatus ($pid, $?) |
1646 | while ($pid = waitpid -1, $WNOHANG) > 0; |
1637 | while ($pid = waitpid -1, WNOHANG) > 0; |
1647 | }; |
1638 | }; |
1648 | |
1639 | |
1649 | *child = sub { |
1640 | *child = sub { |
1650 | my (undef, %arg) = @_; |
1641 | my (undef, %arg) = @_; |
1651 | |
1642 | |
1652 | defined (my $pid = $arg{pid} + 0) |
1643 | defined (my $pid = $arg{pid} + 0) |
1653 | or Carp::croak "required option 'pid' is missing"; |
1644 | or Carp::croak "required option 'pid' is missing"; |
1654 | |
1645 | |
1655 | $PID_CB{$pid}{$arg{cb}} = $arg{cb}; |
1646 | $PID_CB{$pid}{$arg{cb}} = $arg{cb}; |
1656 | |
|
|
1657 | # WNOHANG is almost cetrainly 1 everywhere |
|
|
1658 | $WNOHANG ||= $^O =~ /^(?:openbsd|netbsd|linux|freebsd|cygwin|MSWin32)$/ |
|
|
1659 | ? 1 |
|
|
1660 | : eval { local $SIG{__DIE__}; require POSIX; &POSIX::WNOHANG } || 1; |
|
|
1661 | |
1647 | |
1662 | unless ($CHLD_W) { |
1648 | unless ($CHLD_W) { |
1663 | $CHLD_W = AE::signal CHLD => \&_sigchld; |
1649 | $CHLD_W = AE::signal CHLD => \&_sigchld; |
1664 | # child could be a zombie already, so make at least one round |
1650 | # child could be a zombie already, so make at least one round |
1665 | &_sigchld; |
1651 | &_sigchld; |
… | |
… | |
1726 | } |
1712 | } |
1727 | |
1713 | |
1728 | package AnyEvent::CondVar; |
1714 | package AnyEvent::CondVar; |
1729 | |
1715 | |
1730 | our @ISA = AnyEvent::CondVar::Base::; |
1716 | our @ISA = AnyEvent::CondVar::Base::; |
|
|
1717 | |
|
|
1718 | # only to be used for subclassing |
|
|
1719 | sub new { |
|
|
1720 | my $class = shift; |
|
|
1721 | bless AnyEvent->condvar (@_), $class |
|
|
1722 | } |
1731 | |
1723 | |
1732 | package AnyEvent::CondVar::Base; |
1724 | package AnyEvent::CondVar::Base; |
1733 | |
1725 | |
1734 | #use overload |
1726 | #use overload |
1735 | # '&{}' => sub { my $self = shift; sub { $self->send (@_) } }, |
1727 | # '&{}' => sub { my $self = shift; sub { $self->send (@_) } }, |
… | |
… | |
2654 | pronounced). |
2646 | pronounced). |
2655 | |
2647 | |
2656 | |
2648 | |
2657 | =head1 SEE ALSO |
2649 | =head1 SEE ALSO |
2658 | |
2650 | |
|
|
2651 | Tutorial/Introduction: L<AnyEvent::Intro>. |
|
|
2652 | |
|
|
2653 | FAQ: L<AnyEvent::FAQ>. |
|
|
2654 | |
2659 | Utility functions: L<AnyEvent::Util>. |
2655 | Utility functions: L<AnyEvent::Util>. |
2660 | |
2656 | |
2661 | Event modules: L<EV>, L<EV::Glib>, L<Glib::EV>, L<Event>, L<Glib::Event>, |
2657 | Event modules: L<EV>, L<EV::Glib>, L<Glib::EV>, L<Event>, L<Glib::Event>, |
2662 | L<Glib>, L<Tk>, L<Event::Lib>, L<Qt>, L<POE>. |
2658 | L<Glib>, L<Tk>, L<Event::Lib>, L<Qt>, L<POE>. |
2663 | |
2659 | |
… | |
… | |
2669 | Non-blocking file handles, sockets, TCP clients and |
2665 | Non-blocking file handles, sockets, TCP clients and |
2670 | servers: L<AnyEvent::Handle>, L<AnyEvent::Socket>, L<AnyEvent::TLS>. |
2666 | servers: L<AnyEvent::Handle>, L<AnyEvent::Socket>, L<AnyEvent::TLS>. |
2671 | |
2667 | |
2672 | Asynchronous DNS: L<AnyEvent::DNS>. |
2668 | Asynchronous DNS: L<AnyEvent::DNS>. |
2673 | |
2669 | |
2674 | Coroutine support: L<Coro>, L<Coro::AnyEvent>, L<Coro::EV>, |
2670 | Thread support: L<Coro>, L<Coro::AnyEvent>, L<Coro::EV>, L<Coro::Event>. |
2675 | L<Coro::Event>, |
|
|
2676 | |
2671 | |
2677 | Nontrivial usage examples: L<AnyEvent::GPSD>, L<AnyEvent::XMPP>, |
2672 | Nontrivial usage examples: L<AnyEvent::GPSD>, L<AnyEvent::IRC>, |
2678 | L<AnyEvent::HTTP>. |
2673 | L<AnyEvent::HTTP>. |
2679 | |
2674 | |
2680 | |
2675 | |
2681 | =head1 AUTHOR |
2676 | =head1 AUTHOR |
2682 | |
2677 | |