… | |
… | |
7 | |
7 | |
8 | =head1 SYNOPSIS |
8 | =head1 SYNOPSIS |
9 | |
9 | |
10 | use AnyEvent; |
10 | use AnyEvent; |
11 | |
11 | |
|
|
12 | # if you prefer function calls, look at the AE manpage for |
|
|
13 | # an alternative API. |
|
|
14 | |
12 | # file descriptor readable |
15 | # file handle or descriptor readable |
13 | my $w = AnyEvent->io (fh => $fh, poll => "r", cb => sub { ... }); |
16 | my $w = AnyEvent->io (fh => $fh, poll => "r", cb => sub { ... }); |
14 | |
17 | |
15 | # one-shot or repeating timers |
18 | # one-shot or repeating timers |
16 | my $w = AnyEvent->timer (after => $seconds, cb => sub { ... }); |
19 | my $w = AnyEvent->timer (after => $seconds, cb => sub { ... }); |
17 | my $w = AnyEvent->timer (after => $seconds, interval => $seconds, cb => ... |
20 | my $w = AnyEvent->timer (after => $seconds, interval => $seconds, cb => ... |
… | |
… | |
606 | eventually calls C<< -> send >>, and the "consumer side", which waits |
609 | eventually calls C<< -> send >>, and the "consumer side", which waits |
607 | for the send to occur. |
610 | for the send to occur. |
608 | |
611 | |
609 | Example: wait for a timer. |
612 | Example: wait for a timer. |
610 | |
613 | |
611 | # wait till the result is ready |
614 | # condition: "wait till the timer is fired" |
612 | my $result_ready = AnyEvent->condvar; |
615 | my $timer_fired = AnyEvent->condvar; |
613 | |
616 | |
614 | # do something such as adding a timer |
617 | # create the timer - we could wait for, say |
615 | # or socket watcher the calls $result_ready->send |
618 | # a handle becomign ready, or even an |
616 | # when the "result" is ready. |
619 | # AnyEvent::HTTP request to finish, but |
617 | # in this case, we simply use a timer: |
620 | # in this case, we simply use a timer: |
618 | my $w = AnyEvent->timer ( |
621 | my $w = AnyEvent->timer ( |
619 | after => 1, |
622 | after => 1, |
620 | cb => sub { $result_ready->send }, |
623 | cb => sub { $timer_fired->send }, |
621 | ); |
624 | ); |
622 | |
625 | |
623 | # this "blocks" (while handling events) till the callback |
626 | # this "blocks" (while handling events) till the callback |
624 | # calls ->send |
627 | # calls ->send |
625 | $result_ready->recv; |
628 | $timer_fired->recv; |
626 | |
629 | |
627 | Example: wait for a timer, but take advantage of the fact that condition |
630 | Example: wait for a timer, but take advantage of the fact that condition |
628 | variables are also callable directly. |
631 | variables are also callable directly. |
629 | |
632 | |
630 | my $done = AnyEvent->condvar; |
633 | my $done = AnyEvent->condvar; |
… | |
… | |
1054 | =head1 OTHER MODULES |
1057 | =head1 OTHER MODULES |
1055 | |
1058 | |
1056 | The following is a non-exhaustive list of additional modules that use |
1059 | The following is a non-exhaustive list of additional modules that use |
1057 | AnyEvent as a client and can therefore be mixed easily with other AnyEvent |
1060 | AnyEvent as a client and can therefore be mixed easily with other AnyEvent |
1058 | modules and other event loops in the same program. Some of the modules |
1061 | modules and other event loops in the same program. Some of the modules |
1059 | come with AnyEvent, most are available via CPAN. |
1062 | come as part of AnyEvent, the others are available via CPAN. |
1060 | |
1063 | |
1061 | =over 4 |
1064 | =over 4 |
1062 | |
1065 | |
1063 | =item L<AnyEvent::Util> |
1066 | =item L<AnyEvent::Util> |
1064 | |
1067 | |
… | |
… | |
1079 | |
1082 | |
1080 | =item L<AnyEvent::DNS> |
1083 | =item L<AnyEvent::DNS> |
1081 | |
1084 | |
1082 | Provides rich asynchronous DNS resolver capabilities. |
1085 | Provides rich asynchronous DNS resolver capabilities. |
1083 | |
1086 | |
|
|
1087 | =item L<AnyEvent::HTTP>, L<AnyEvent::IRC>, L<AnyEvent::XMPP>, L<AnyEvent::GPSD>, L<AnyEvent::IGS>, L<AnyEvent::FCP> |
|
|
1088 | |
|
|
1089 | Implement event-based interfaces to the protocols of the same name (for |
|
|
1090 | the curious, IGS is the International Go Server and FCP is the Freenet |
|
|
1091 | Client Protocol). |
|
|
1092 | |
|
|
1093 | =item L<AnyEvent::Handle::UDP> |
|
|
1094 | |
|
|
1095 | Here be danger! |
|
|
1096 | |
|
|
1097 | As Pauli would put it, "Not only is it not right, it's not even wrong!" - |
|
|
1098 | there are so many things wrong with AnyEvent::Handle::UDP, most notably |
|
|
1099 | it's use of a stream-based API with a protocol that isn't streamable, that |
|
|
1100 | the only way to improve it is to delete it. |
|
|
1101 | |
|
|
1102 | It features data corruption (but typically only under load) and general |
|
|
1103 | confusion. On top, the author is not only clueless about UDP but also |
|
|
1104 | fact-resistant - some gems of his understanding: "connect doesn't work |
|
|
1105 | with UDP", "UDP packets are not IP packets", "UDP only has datagrams, not |
|
|
1106 | packets", "I don't need to implement proper error checking as UDP doesn't |
|
|
1107 | support error checking" and so on - he doesn't even understand what's |
|
|
1108 | wrong with his module when it is explained to him. |
|
|
1109 | |
1084 | =item L<AnyEvent::HTTP> |
1110 | =item L<AnyEvent::DBI> |
1085 | |
1111 | |
1086 | A simple-to-use HTTP library that is capable of making a lot of concurrent |
1112 | Executes L<DBI> requests asynchronously in a proxy process for you, |
1087 | HTTP requests. |
1113 | notifying you in an event-bnased way when the operation is finished. |
|
|
1114 | |
|
|
1115 | =item L<AnyEvent::AIO> |
|
|
1116 | |
|
|
1117 | Truly asynchronous (as opposed to non-blocking) I/O, should be in the |
|
|
1118 | toolbox of every event programmer. AnyEvent::AIO transparently fuses |
|
|
1119 | L<IO::AIO> and AnyEvent together, giving AnyEvent access to event-based |
|
|
1120 | file I/O, and much more. |
1088 | |
1121 | |
1089 | =item L<AnyEvent::HTTPD> |
1122 | =item L<AnyEvent::HTTPD> |
1090 | |
1123 | |
1091 | Provides a simple web application server framework. |
1124 | A simple embedded webserver. |
1092 | |
1125 | |
1093 | =item L<AnyEvent::FastPing> |
1126 | =item L<AnyEvent::FastPing> |
1094 | |
1127 | |
1095 | The fastest ping in the west. |
1128 | The fastest ping in the west. |
1096 | |
|
|
1097 | =item L<AnyEvent::DBI> |
|
|
1098 | |
|
|
1099 | Executes L<DBI> requests asynchronously in a proxy process. |
|
|
1100 | |
|
|
1101 | =item L<AnyEvent::AIO> |
|
|
1102 | |
|
|
1103 | Truly asynchronous I/O, should be in the toolbox of every event |
|
|
1104 | programmer. AnyEvent::AIO transparently fuses L<IO::AIO> and AnyEvent |
|
|
1105 | together. |
|
|
1106 | |
|
|
1107 | =item L<AnyEvent::BDB> |
|
|
1108 | |
|
|
1109 | Truly asynchronous Berkeley DB access. AnyEvent::BDB transparently fuses |
|
|
1110 | L<BDB> and AnyEvent together. |
|
|
1111 | |
|
|
1112 | =item L<AnyEvent::GPSD> |
|
|
1113 | |
|
|
1114 | A non-blocking interface to gpsd, a daemon delivering GPS information. |
|
|
1115 | |
|
|
1116 | =item L<AnyEvent::IRC> |
|
|
1117 | |
|
|
1118 | AnyEvent based IRC client module family (replacing the older Net::IRC3). |
|
|
1119 | |
|
|
1120 | =item L<AnyEvent::XMPP> |
|
|
1121 | |
|
|
1122 | AnyEvent based XMPP (Jabber protocol) module family (replacing the older |
|
|
1123 | Net::XMPP2>. |
|
|
1124 | |
|
|
1125 | =item L<AnyEvent::IGS> |
|
|
1126 | |
|
|
1127 | A non-blocking interface to the Internet Go Server protocol (used by |
|
|
1128 | L<App::IGS>). |
|
|
1129 | |
|
|
1130 | =item L<Net::FCP> |
|
|
1131 | |
|
|
1132 | AnyEvent-based implementation of the Freenet Client Protocol, birthplace |
|
|
1133 | of AnyEvent. |
|
|
1134 | |
|
|
1135 | =item L<Event::ExecFlow> |
|
|
1136 | |
|
|
1137 | High level API for event-based execution flow control. |
|
|
1138 | |
1129 | |
1139 | =item L<Coro> |
1130 | =item L<Coro> |
1140 | |
1131 | |
1141 | Has special support for AnyEvent via L<Coro::AnyEvent>. |
1132 | Has special support for AnyEvent via L<Coro::AnyEvent>. |
1142 | |
1133 | |
… | |
… | |
1156 | |
1147 | |
1157 | BEGIN { AnyEvent::common_sense } |
1148 | BEGIN { AnyEvent::common_sense } |
1158 | |
1149 | |
1159 | use Carp (); |
1150 | use Carp (); |
1160 | |
1151 | |
1161 | our $VERSION = '5.251'; |
1152 | our $VERSION = '5.261'; |
1162 | our $MODEL; |
1153 | our $MODEL; |
1163 | |
1154 | |
1164 | our $AUTOLOAD; |
1155 | our $AUTOLOAD; |
1165 | our @ISA; |
1156 | our @ISA; |
1166 | |
1157 | |
… | |
… | |
1342 | |
1333 | |
1343 | =head1 SIMPLIFIED AE API |
1334 | =head1 SIMPLIFIED AE API |
1344 | |
1335 | |
1345 | Starting with version 5.0, AnyEvent officially supports a second, much |
1336 | Starting with version 5.0, AnyEvent officially supports a second, much |
1346 | simpler, API that is designed to reduce the calling, typing and memory |
1337 | simpler, API that is designed to reduce the calling, typing and memory |
1347 | overhead. |
1338 | overhead by using function call syntax and a fixed number of parameters. |
1348 | |
1339 | |
1349 | See the L<AE> manpage for details. |
1340 | See the L<AE> manpage for details. |
1350 | |
1341 | |
1351 | =cut |
1342 | =cut |
1352 | |
1343 | |
1353 | package AE; |
1344 | package AE; |
1354 | |
1345 | |
1355 | our $VERSION = $AnyEvent::VERSION; |
1346 | our $VERSION = $AnyEvent::VERSION; |
|
|
1347 | |
|
|
1348 | # fall back to the main API by default - backends and AnyEvent::Base |
|
|
1349 | # implementations can overwrite these. |
1356 | |
1350 | |
1357 | sub io($$$) { |
1351 | sub io($$$) { |
1358 | AnyEvent->io (fh => $_[0], poll => $_[1] ? "w" : "r", cb => $_[2]) |
1352 | AnyEvent->io (fh => $_[0], poll => $_[1] ? "w" : "r", cb => $_[2]) |
1359 | } |
1353 | } |
1360 | |
1354 | |
… | |
… | |
2075 | |
2069 | |
2076 | The actual code goes further and collects all errors (C<die>s, exceptions) |
2070 | The actual code goes further and collects all errors (C<die>s, exceptions) |
2077 | that occurred during request processing. The C<result> method detects |
2071 | that occurred during request processing. The C<result> method detects |
2078 | whether an exception as thrown (it is stored inside the $txn object) |
2072 | whether an exception as thrown (it is stored inside the $txn object) |
2079 | and just throws the exception, which means connection errors and other |
2073 | and just throws the exception, which means connection errors and other |
2080 | problems get reported tot he code that tries to use the result, not in a |
2074 | problems get reported to the code that tries to use the result, not in a |
2081 | random callback. |
2075 | random callback. |
2082 | |
2076 | |
2083 | All of this enables the following usage styles: |
2077 | All of this enables the following usage styles: |
2084 | |
2078 | |
2085 | 1. Blocking: |
2079 | 1. Blocking: |