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.164 by root, Tue Jul 8 19:50:25 2008 UTC vs.
Revision 1.167 by root, Tue Jul 8 23:44:51 2008 UTC

152=head2 I/O WATCHERS 152=head2 I/O WATCHERS
153 153
154You can create an I/O watcher by calling the C<< AnyEvent->io >> method 154You can create an I/O watcher by calling the C<< AnyEvent->io >> method
155with the following mandatory key-value pairs as arguments: 155with the following mandatory key-value pairs as arguments:
156 156
157C<fh> the Perl I<file handle> (I<not> file descriptor) to watch 157C<fh> the Perl I<file handle> (I<not> file descriptor) to watch for events
158for 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>
159which creates a watcher waiting for "r"eadable or "w"ritable events, 159must be a string that is either C<r> or C<w>, which creates a watcher
160respectively. C<cb> is the callback to invoke each time the file handle 160waiting for "r"eadable or "w"ritable events, respectively. C<cb> is the
161becomes ready. 161callback to invoke each time the file handle becomes ready.
162 162
163Although the callback might get passed parameters, their value and 163Although the callback might get passed parameters, their value and
164presence is undefined and you cannot rely on them. Portable AnyEvent 164presence is undefined and you cannot rely on them. Portable AnyEvent
165callbacks cannot use arguments passed to I/O watcher callbacks. 165callbacks cannot use arguments passed to I/O watcher callbacks.
166 166
193Although the callback might get passed parameters, their value and 193Although the callback might get passed parameters, their value and
194presence is undefined and you cannot rely on them. Portable AnyEvent 194presence is undefined and you cannot rely on them. Portable AnyEvent
195callbacks cannot use arguments passed to time watcher callbacks. 195callbacks cannot use arguments passed to time watcher callbacks.
196 196
197The callback will normally be invoked once only. If you specify another 197The callback will normally be invoked once only. If you specify another
198parameter, C<interval>, as a positive number, then the callback will be 198parameter, C<interval>, as a strictly positive number (> 0), then the
199invoked regularly at that interval (in fractional seconds) after the first 199callback will be invoked regularly at that interval (in fractional
200invocation. 200seconds) after the first invocation. If C<interval> is specified with a
201false value, then it is treated as if it were missing.
201 202
202The callback will be rescheduled before invoking the callback, but no 203The callback will be rescheduled before invoking the callback, but no
203attempt is done to avoid timer drift in most backends, so the interval is 204attempt is done to avoid timer drift in most backends, so the interval is
204only approximate. 205only approximate.
205 206
302=back 303=back
303 304
304=head2 SIGNAL WATCHERS 305=head2 SIGNAL WATCHERS
305 306
306You can watch for signals using a signal watcher, C<signal> is the signal 307You can watch for signals using a signal watcher, C<signal> is the signal
307I<name> without any C<SIG> prefix, C<cb> is the Perl callback to 308I<name> in uppercase and without any C<SIG> prefix, C<cb> is the Perl
308be invoked whenever a signal occurs. 309callback to be invoked whenever a signal occurs.
309 310
310Although the callback might get passed parameters, their value and 311Although the callback might get passed parameters, their value and
311presence is undefined and you cannot rely on them. Portable AnyEvent 312presence is undefined and you cannot rely on them. Portable AnyEvent
312callbacks cannot use arguments passed to signal watcher callbacks. 313callbacks 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
1126package AnyEvent::Base::Strict;
1127
1128use Carp qw(croak);
1129
1130# supply checks for argument validity for many functions
1131
1132sub 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
1154sub 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
1176sub 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
1194sub 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
1212sub 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
1226sub time {
1227 my $class = shift;
1228
1229 @_
1230 and croak "AnyEvent->time wrongly called with paramaters";
1231
1232 $class->SUPER::time (@_)
1233}
1234
1235sub 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
1121This is an advanced topic that you do not normally need to use AnyEvent in 1246This is an advanced topic that you do not normally need to use AnyEvent in
1122a module. This section is only of use to event loop authors who want to 1247a module. This section is only of use to event loop authors who want to
1123provide AnyEvent compatibility. 1248provide AnyEvent compatibility.
1175conditions, such as not being able to load the event model specified by 1300conditions, such as not being able to load the event model specified by
1176C<PERL_ANYEVENT_MODEL>. 1301C<PERL_ANYEVENT_MODEL>.
1177 1302
1178When set to C<2> or higher, cause AnyEvent to report to STDERR which event 1303When set to C<2> or higher, cause AnyEvent to report to STDERR which event
1179model it chooses. 1304model it chooses.
1305
1306=item C<PERL_ANYEVENT_STRICT>
1307
1308AnyEvent does not do much argument checking by default, as thorough
1309argument checking is very costly. Setting this variable to a true value
1310will cause AnyEvent to thoroughly check the arguments passed to most
1311method 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
1313it off in production.
1180 1314
1181=item C<PERL_ANYEVENT_MODEL> 1315=item C<PERL_ANYEVENT_MODEL>
1182 1316
1183This can be used to specify the event model to be used by AnyEvent, before 1317This can be used to specify the event model to be used by AnyEvent, before
1184auto detection and -probing kicks in. It must be a string consisting 1318auto detection and -probing kicks in. It must be a string consisting
1681 1815
1682 use AnyEvent; 1816 use AnyEvent;
1683 1817
1684Similar considerations apply to $ENV{PERL_ANYEVENT_VERBOSE}, as that can 1818Similar considerations apply to $ENV{PERL_ANYEVENT_VERBOSE}, as that can
1685be used to probe what backend is used and gain other information (which is 1819be used to probe what backend is used and gain other information (which is
1686probably even less useful to an attacker than PERL_ANYEVENT_MODEL). 1820probably 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
1691Perl 5.8 has numerous memleaks that sometimes hit this module and are hard 1826Perl 5.8 has numerous memleaks that sometimes hit this module and are hard

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines