… | |
… | |
152 | =head2 I/O WATCHERS |
152 | =head2 I/O WATCHERS |
153 | |
153 | |
154 | You can create an I/O watcher by calling the C<< AnyEvent->io >> method |
154 | You can create an I/O watcher by calling the C<< AnyEvent->io >> method |
155 | with the following mandatory key-value pairs as arguments: |
155 | with the following mandatory key-value pairs as arguments: |
156 | |
156 | |
157 | C<fh> the Perl I<file handle> (I<not> file descriptor) to watch |
157 | C<fh> the Perl I<file handle> (I<not> file descriptor) to watch for events |
158 | for events. C<poll> must be a string that is either C<r> or C<w>, |
158 | (AnyEvent might or might not keep a reference to this file handle). C<poll> |
159 | which creates a watcher waiting for "r"eadable or "w"ritable events, |
159 | must be a string that is either C<r> or C<w>, which creates a watcher |
160 | respectively. C<cb> is the callback to invoke each time the file handle |
160 | waiting for "r"eadable or "w"ritable events, respectively. C<cb> is the |
161 | becomes ready. |
161 | callback to invoke each time the file handle becomes ready. |
162 | |
162 | |
163 | Although the callback might get passed parameters, their value and |
163 | Although the callback might get passed parameters, their value and |
164 | presence is undefined and you cannot rely on them. Portable AnyEvent |
164 | presence is undefined and you cannot rely on them. Portable AnyEvent |
165 | callbacks cannot use arguments passed to I/O watcher callbacks. |
165 | callbacks cannot use arguments passed to I/O watcher callbacks. |
166 | |
166 | |
… | |
… | |
193 | Although the callback might get passed parameters, their value and |
193 | Although the callback might get passed parameters, their value and |
194 | presence is undefined and you cannot rely on them. Portable AnyEvent |
194 | presence is undefined and you cannot rely on them. Portable AnyEvent |
195 | callbacks cannot use arguments passed to time watcher callbacks. |
195 | callbacks cannot use arguments passed to time watcher callbacks. |
196 | |
196 | |
197 | The callback will normally be invoked once only. If you specify another |
197 | The callback will normally be invoked once only. If you specify another |
198 | parameter, C<interval>, as a positive number, then the callback will be |
198 | parameter, C<interval>, as a strictly positive number (> 0), then the |
199 | invoked regularly at that interval (in fractional seconds) after the first |
199 | callback will be invoked regularly at that interval (in fractional |
200 | invocation. |
200 | seconds) after the first invocation. If C<interval> is specified with a |
|
|
201 | false value, then it is treated as if it were missing. |
201 | |
202 | |
202 | The callback will be rescheduled before invoking the callback, but no |
203 | The callback will be rescheduled before invoking the callback, but no |
203 | attempt is done to avoid timer drift in most backends, so the interval is |
204 | attempt is done to avoid timer drift in most backends, so the interval is |
204 | only approximate. |
205 | only approximate. |
205 | |
206 | |
… | |
… | |
302 | =back |
303 | =back |
303 | |
304 | |
304 | =head2 SIGNAL WATCHERS |
305 | =head2 SIGNAL WATCHERS |
305 | |
306 | |
306 | You can watch for signals using a signal watcher, C<signal> is the signal |
307 | You can watch for signals using a signal watcher, C<signal> is the signal |
307 | I<name> without any C<SIG> prefix, C<cb> is the Perl callback to |
308 | I<name> in uppercase and without any C<SIG> prefix, C<cb> is the Perl |
308 | be invoked whenever a signal occurs. |
309 | callback to be invoked whenever a signal occurs. |
309 | |
310 | |
310 | Although the callback might get passed parameters, their value and |
311 | Although the callback might get passed parameters, their value and |
311 | presence is undefined and you cannot rely on them. Portable AnyEvent |
312 | presence is undefined and you cannot rely on them. Portable AnyEvent |
312 | callbacks cannot use arguments passed to signal watcher callbacks. |
313 | callbacks cannot use arguments passed to signal watcher callbacks. |
313 | |
314 | |
… | |
… | |
937 | $MODEL |
938 | $MODEL |
938 | or die "No event module selected for AnyEvent and autodetect failed. Install any one of these modules: EV, Event or Glib."; |
939 | or die "No event module selected for AnyEvent and autodetect failed. Install any one of these modules: EV, Event or Glib."; |
939 | } |
940 | } |
940 | } |
941 | } |
941 | |
942 | |
942 | unshift @ISA, $MODEL; |
|
|
943 | push @{"$MODEL\::ISA"}, "AnyEvent::Base"; |
943 | push @{"$MODEL\::ISA"}, "AnyEvent::Base"; |
|
|
944 | |
|
|
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; |
|
|
950 | } |
944 | |
951 | |
945 | (shift @post_detect)->() while @post_detect; |
952 | (shift @post_detect)->() while @post_detect; |
946 | } |
953 | } |
947 | |
954 | |
948 | $MODEL |
955 | $MODEL |
… | |
… | |
1114 | |
1121 | |
1115 | # undocumented/compatibility with pre-3.4 |
1122 | # undocumented/compatibility with pre-3.4 |
1116 | *broadcast = \&send; |
1123 | *broadcast = \&send; |
1117 | *wait = \&_wait; |
1124 | *wait = \&_wait; |
1118 | |
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 | |
1119 | =head1 SUPPLYING YOUR OWN EVENT MODEL INTERFACE |
1244 | =head1 SUPPLYING YOUR OWN EVENT MODEL INTERFACE |
1120 | |
1245 | |
1121 | This is an advanced topic that you do not normally need to use AnyEvent in |
1246 | This is an advanced topic that you do not normally need to use AnyEvent in |
1122 | a module. This section is only of use to event loop authors who want to |
1247 | a module. This section is only of use to event loop authors who want to |
1123 | provide AnyEvent compatibility. |
1248 | provide AnyEvent compatibility. |
… | |
… | |
1175 | conditions, such as not being able to load the event model specified by |
1300 | conditions, such as not being able to load the event model specified by |
1176 | C<PERL_ANYEVENT_MODEL>. |
1301 | C<PERL_ANYEVENT_MODEL>. |
1177 | |
1302 | |
1178 | When set to C<2> or higher, cause AnyEvent to report to STDERR which event |
1303 | When set to C<2> or higher, cause AnyEvent to report to STDERR which event |
1179 | model it chooses. |
1304 | model it chooses. |
|
|
1305 | |
|
|
1306 | =item C<PERL_ANYEVENT_STRICT> |
|
|
1307 | |
|
|
1308 | AnyEvent does not do much argument checking by default, as thorough |
|
|
1309 | argument checking is very costly. Setting this variable to a true value |
|
|
1310 | will cause AnyEvent to thoroughly check the arguments passed to most |
|
|
1311 | method calls and croaks if it finds any problems. In other words, enables |
|
|
1312 | "strict" mode. Unlike C<use strict> it is definitely recommended ot keep |
|
|
1313 | it off in production. |
1180 | |
1314 | |
1181 | =item C<PERL_ANYEVENT_MODEL> |
1315 | =item C<PERL_ANYEVENT_MODEL> |
1182 | |
1316 | |
1183 | This can be used to specify the event model to be used by AnyEvent, before |
1317 | This can be used to specify the event model to be used by AnyEvent, before |
1184 | auto detection and -probing kicks in. It must be a string consisting |
1318 | auto detection and -probing kicks in. It must be a string consisting |
… | |
… | |
1681 | |
1815 | |
1682 | use AnyEvent; |
1816 | use AnyEvent; |
1683 | |
1817 | |
1684 | Similar considerations apply to $ENV{PERL_ANYEVENT_VERBOSE}, as that can |
1818 | Similar considerations apply to $ENV{PERL_ANYEVENT_VERBOSE}, as that can |
1685 | be used to probe what backend is used and gain other information (which is |
1819 | be used to probe what backend is used and gain other information (which is |
1686 | probably even less useful to an attacker than PERL_ANYEVENT_MODEL). |
1820 | probably even less useful to an attacker than PERL_ANYEVENT_MODEL), and |
|
|
1821 | $ENV{PERL_ANYEGENT_STRICT}. |
1687 | |
1822 | |
1688 | |
1823 | |
1689 | =head1 BUGS |
1824 | =head1 BUGS |
1690 | |
1825 | |
1691 | Perl 5.8 has numerous memleaks that sometimes hit this module and are hard |
1826 | Perl 5.8 has numerous memleaks that sometimes hit this module and are hard |