… | |
… | |
33 | |
33 | |
34 | Executive Summary: AnyEvent is I<compatible>, AnyEvent is I<free of |
34 | Executive Summary: AnyEvent is I<compatible>, AnyEvent is I<free of |
35 | policy> and AnyEvent is I<small and efficient>. |
35 | policy> and AnyEvent is I<small and efficient>. |
36 | |
36 | |
37 | First and foremost, I<AnyEvent is not an event model> itself, it only |
37 | First and foremost, I<AnyEvent is not an event model> itself, it only |
38 | interfaces to whatever event model the main program happens to use in a |
38 | interfaces to whatever event model the main program happens to use, in a |
39 | pragmatic way. For event models and certain classes of immortals alike, |
39 | pragmatic way. For event models and certain classes of immortals alike, |
40 | the statement "there can only be one" is a bitter reality: In general, |
40 | the statement "there can only be one" is a bitter reality: In general, |
41 | only one event loop can be active at the same time in a process. AnyEvent |
41 | only one event loop can be active at the same time in a process. AnyEvent |
42 | helps hiding the differences between those event loops. |
42 | cannot change this, but it can hide the differences between those event |
|
|
43 | loops. |
43 | |
44 | |
44 | The goal of AnyEvent is to offer module authors the ability to do event |
45 | The goal of AnyEvent is to offer module authors the ability to do event |
45 | programming (waiting for I/O or timer events) without subscribing to a |
46 | programming (waiting for I/O or timer events) without subscribing to a |
46 | religion, a way of living, and most importantly: without forcing your |
47 | religion, a way of living, and most importantly: without forcing your |
47 | module users into the same thing by forcing them to use the same event |
48 | module users into the same thing by forcing them to use the same event |
48 | model you use. |
49 | model you use. |
49 | |
50 | |
50 | For modules like POE or IO::Async (which is a total misnomer as it is |
51 | For modules like POE or IO::Async (which is a total misnomer as it is |
51 | actually doing all I/O I<synchronously>...), using them in your module is |
52 | actually doing all I/O I<synchronously>...), using them in your module is |
52 | like joining a cult: After you joined, you are dependent on them and you |
53 | like joining a cult: After you joined, you are dependent on them and you |
53 | cannot use anything else, as it is simply incompatible to everything that |
54 | cannot use anything else, as they are simply incompatible to everything |
54 | isn't itself. What's worse, all the potential users of your module are |
55 | that isn't them. What's worse, all the potential users of your |
55 | I<also> forced to use the same event loop you use. |
56 | module are I<also> forced to use the same event loop you use. |
56 | |
57 | |
57 | AnyEvent is different: AnyEvent + POE works fine. AnyEvent + Glib works |
58 | AnyEvent is different: AnyEvent + POE works fine. AnyEvent + Glib works |
58 | fine. AnyEvent + Tk works fine etc. etc. but none of these work together |
59 | fine. AnyEvent + Tk works fine etc. etc. but none of these work together |
59 | with the rest: POE + IO::Async? No go. Tk + Event? No go. Again: if |
60 | with the rest: POE + IO::Async? No go. Tk + Event? No go. Again: if |
60 | your module uses one of those, every user of your module has to use it, |
61 | your module uses one of those, every user of your module has to use it, |
61 | too. But if your module uses AnyEvent, it works transparently with all |
62 | too. But if your module uses AnyEvent, it works transparently with all |
62 | event models it supports (including stuff like POE and IO::Async, as long |
63 | event models it supports (including stuff like IO::Async, as long as those |
63 | as those use one of the supported event loops. It is trivial to add new |
64 | use one of the supported event loops. It is trivial to add new event loops |
64 | event loops to AnyEvent, too, so it is future-proof). |
65 | to AnyEvent, too, so it is future-proof). |
65 | |
66 | |
66 | In addition to being free of having to use I<the one and only true event |
67 | In addition to being free of having to use I<the one and only true event |
67 | model>, AnyEvent also is free of bloat and policy: with POE or similar |
68 | model>, AnyEvent also is free of bloat and policy: with POE or similar |
68 | modules, you get an enormous amount of code and strict rules you have to |
69 | modules, you get an enormous amount of code and strict rules you have to |
69 | follow. AnyEvent, on the other hand, is lean and up to the point, by only |
70 | follow. AnyEvent, on the other hand, is lean and up to the point, by only |
… | |
… | |
940 | } |
941 | } |
941 | } |
942 | } |
942 | |
943 | |
943 | push @{"$MODEL\::ISA"}, "AnyEvent::Base"; |
944 | push @{"$MODEL\::ISA"}, "AnyEvent::Base"; |
944 | |
945 | |
945 | if ($ENV{PERL_ANYEVENT_STRICT}) { |
|
|
946 | unshift @AnyEvent::Base::Strict::ISA, $MODEL; |
|
|
947 | unshift @ISA, AnyEvent::Base::Strict:: |
|
|
948 | } else { |
|
|
949 | unshift @ISA, $MODEL; |
946 | unshift @ISA, $MODEL; |
950 | } |
947 | |
|
|
948 | require AnyEvent::Strict if $ENV{PERL_ANYEVENT_STRICT}; |
951 | |
949 | |
952 | (shift @post_detect)->() while @post_detect; |
950 | (shift @post_detect)->() while @post_detect; |
953 | } |
951 | } |
954 | |
952 | |
955 | $MODEL |
953 | $MODEL |
… | |
… | |
1120 | } |
1118 | } |
1121 | |
1119 | |
1122 | # undocumented/compatibility with pre-3.4 |
1120 | # undocumented/compatibility with pre-3.4 |
1123 | *broadcast = \&send; |
1121 | *broadcast = \&send; |
1124 | *wait = \&_wait; |
1122 | *wait = \&_wait; |
1125 | |
|
|
1126 | package AnyEvent::Base::Strict; |
|
|
1127 | |
|
|
1128 | use Carp qw(croak); |
|
|
1129 | |
|
|
1130 | # supply checks for argument validity for many functions |
|
|
1131 | |
|
|
1132 | sub io { |
|
|
1133 | my $class = shift; |
|
|
1134 | my %arg = @_; |
|
|
1135 | |
|
|
1136 | ref $arg{cb} |
|
|
1137 | or croak "AnyEvent->io called with illegal cb argument '$arg{cb}'"; |
|
|
1138 | delete $arg{cb}; |
|
|
1139 | |
|
|
1140 | fileno $arg{fh} |
|
|
1141 | or croak "AnyEvent->io called with illegal fh argument '$arg{fh}'"; |
|
|
1142 | delete $arg{fh}; |
|
|
1143 | |
|
|
1144 | $arg{poll} =~ /^[rw]$/ |
|
|
1145 | or croak "AnyEvent->io called with illegal poll argument '$arg{poll}'"; |
|
|
1146 | delete $arg{poll}; |
|
|
1147 | |
|
|
1148 | croak "AnyEvent->io called with unsupported parameter(s) " . join ", ", keys %arg |
|
|
1149 | if keys %arg; |
|
|
1150 | |
|
|
1151 | $class->SUPER::io (@_) |
|
|
1152 | } |
|
|
1153 | |
|
|
1154 | sub timer { |
|
|
1155 | my $class = shift; |
|
|
1156 | my %arg = @_; |
|
|
1157 | |
|
|
1158 | ref $arg{cb} |
|
|
1159 | or croak "AnyEvent->timer called with illegal cb argument '$arg{cb}'"; |
|
|
1160 | delete $arg{cb}; |
|
|
1161 | |
|
|
1162 | exists $arg{after} |
|
|
1163 | or croak "AnyEvent->timer called without mandatory 'after' parameter"; |
|
|
1164 | delete $arg{after}; |
|
|
1165 | |
|
|
1166 | $arg{interval} > 0 || !$arg{interval} |
|
|
1167 | or croak "AnyEvent->timer called with illegal interval argument '$arg{interval}'"; |
|
|
1168 | delete $arg{interval}; |
|
|
1169 | |
|
|
1170 | croak "AnyEvent->timer called with unsupported parameter(s) " . join ", ", keys %arg |
|
|
1171 | if keys %arg; |
|
|
1172 | |
|
|
1173 | $class->SUPER::timer (@_) |
|
|
1174 | } |
|
|
1175 | |
|
|
1176 | sub signal { |
|
|
1177 | my $class = shift; |
|
|
1178 | my %arg = @_; |
|
|
1179 | |
|
|
1180 | ref $arg{cb} |
|
|
1181 | or croak "AnyEvent->signal called with illegal cb argument '$arg{cb}'"; |
|
|
1182 | delete $arg{cb}; |
|
|
1183 | |
|
|
1184 | eval "require POSIX; defined &POSIX::SIG$arg{signal}" |
|
|
1185 | or croak "AnyEvent->signal called with illegal signal name '$arg{signal}'"; |
|
|
1186 | delete $arg{signal}; |
|
|
1187 | |
|
|
1188 | croak "AnyEvent->signal called with unsupported parameter(s) " . join ", ", keys %arg |
|
|
1189 | if keys %arg; |
|
|
1190 | |
|
|
1191 | $class->SUPER::signal (@_) |
|
|
1192 | } |
|
|
1193 | |
|
|
1194 | sub child { |
|
|
1195 | my $class = shift; |
|
|
1196 | my %arg = @_; |
|
|
1197 | |
|
|
1198 | ref $arg{cb} |
|
|
1199 | or croak "AnyEvent->signal called with illegal cb argument '$arg{cb}'"; |
|
|
1200 | delete $arg{cb}; |
|
|
1201 | |
|
|
1202 | $arg{pid} =~ /^-?\d+$/ |
|
|
1203 | or croak "AnyEvent->signal called with illegal pid value '$arg{pid}'"; |
|
|
1204 | delete $arg{pid}; |
|
|
1205 | |
|
|
1206 | croak "AnyEvent->signal called with unsupported parameter(s) " . join ", ", keys %arg |
|
|
1207 | if keys %arg; |
|
|
1208 | |
|
|
1209 | $class->SUPER::child (@_) |
|
|
1210 | } |
|
|
1211 | |
|
|
1212 | sub condvar { |
|
|
1213 | my $class = shift; |
|
|
1214 | my %arg = @_; |
|
|
1215 | |
|
|
1216 | !exists $arg{cb} or ref $arg{cb} |
|
|
1217 | or croak "AnyEvent->condvar called with illegal cb argument '$arg{cb}'"; |
|
|
1218 | delete $arg{cb}; |
|
|
1219 | |
|
|
1220 | croak "AnyEvent->condvar called with unsupported parameter(s) " . join ", ", keys %arg |
|
|
1221 | if keys %arg; |
|
|
1222 | |
|
|
1223 | $class->SUPER::condvar (@_) |
|
|
1224 | } |
|
|
1225 | |
|
|
1226 | sub time { |
|
|
1227 | my $class = shift; |
|
|
1228 | |
|
|
1229 | @_ |
|
|
1230 | and croak "AnyEvent->time wrongly called with paramaters"; |
|
|
1231 | |
|
|
1232 | $class->SUPER::time (@_) |
|
|
1233 | } |
|
|
1234 | |
|
|
1235 | sub now { |
|
|
1236 | my $class = shift; |
|
|
1237 | |
|
|
1238 | @_ |
|
|
1239 | and croak "AnyEvent->now wrongly called with paramaters"; |
|
|
1240 | |
|
|
1241 | $class->SUPER::now (@_) |
|
|
1242 | } |
|
|
1243 | |
1123 | |
1244 | =head1 SUPPLYING YOUR OWN EVENT MODEL INTERFACE |
1124 | =head1 SUPPLYING YOUR OWN EVENT MODEL INTERFACE |
1245 | |
1125 | |
1246 | This is an advanced topic that you do not normally need to use AnyEvent in |
1126 | This is an advanced topic that you do not normally need to use AnyEvent in |
1247 | a module. This section is only of use to event loop authors who want to |
1127 | a module. This section is only of use to event loop authors who want to |