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.171 by root, Wed Jul 16 21:17:59 2008 UTC

33 33
34Executive Summary: AnyEvent is I<compatible>, AnyEvent is I<free of 34Executive Summary: AnyEvent is I<compatible>, AnyEvent is I<free of
35policy> and AnyEvent is I<small and efficient>. 35policy> and AnyEvent is I<small and efficient>.
36 36
37First and foremost, I<AnyEvent is not an event model> itself, it only 37First and foremost, I<AnyEvent is not an event model> itself, it only
38interfaces to whatever event model the main program happens to use in a 38interfaces to whatever event model the main program happens to use, in a
39pragmatic way. For event models and certain classes of immortals alike, 39pragmatic way. For event models and certain classes of immortals alike,
40the statement "there can only be one" is a bitter reality: In general, 40the statement "there can only be one" is a bitter reality: In general,
41only one event loop can be active at the same time in a process. AnyEvent 41only one event loop can be active at the same time in a process. AnyEvent
42helps hiding the differences between those event loops. 42cannot change this, but it can hide the differences between those event
43loops.
43 44
44The goal of AnyEvent is to offer module authors the ability to do event 45The goal of AnyEvent is to offer module authors the ability to do event
45programming (waiting for I/O or timer events) without subscribing to a 46programming (waiting for I/O or timer events) without subscribing to a
46religion, a way of living, and most importantly: without forcing your 47religion, a way of living, and most importantly: without forcing your
47module users into the same thing by forcing them to use the same event 48module users into the same thing by forcing them to use the same event
48model you use. 49model you use.
49 50
50For modules like POE or IO::Async (which is a total misnomer as it is 51For modules like POE or IO::Async (which is a total misnomer as it is
51actually doing all I/O I<synchronously>...), using them in your module is 52actually doing all I/O I<synchronously>...), using them in your module is
52like joining a cult: After you joined, you are dependent on them and you 53like joining a cult: After you joined, you are dependent on them and you
53cannot use anything else, as it is simply incompatible to everything that 54cannot use anything else, as they are simply incompatible to everything
54isn't itself. What's worse, all the potential users of your module are 55that isn't them. What's worse, all the potential users of your
55I<also> forced to use the same event loop you use. 56module are I<also> forced to use the same event loop you use.
56 57
57AnyEvent is different: AnyEvent + POE works fine. AnyEvent + Glib works 58AnyEvent is different: AnyEvent + POE works fine. AnyEvent + Glib works
58fine. AnyEvent + Tk works fine etc. etc. but none of these work together 59fine. AnyEvent + Tk works fine etc. etc. but none of these work together
59with the rest: POE + IO::Async? No go. Tk + Event? No go. Again: if 60with the rest: POE + IO::Async? No go. Tk + Event? No go. Again: if
60your module uses one of those, every user of your module has to use it, 61your module uses one of those, every user of your module has to use it,
61too. But if your module uses AnyEvent, it works transparently with all 62too. But if your module uses AnyEvent, it works transparently with all
62event models it supports (including stuff like POE and IO::Async, as long 63event models it supports (including stuff like IO::Async, as long as those
63as those use one of the supported event loops. It is trivial to add new 64use one of the supported event loops. It is trivial to add new event loops
64event loops to AnyEvent, too, so it is future-proof). 65to AnyEvent, too, so it is future-proof).
65 66
66In addition to being free of having to use I<the one and only true event 67In addition to being free of having to use I<the one and only true event
67model>, AnyEvent also is free of bloat and policy: with POE or similar 68model>, AnyEvent also is free of bloat and policy: with POE or similar
68modules, you get an enormous amount of code and strict rules you have to 69modules, you get an enormous amount of code and strict rules you have to
69follow. AnyEvent, on the other hand, is lean and up to the point, by only 70follow. AnyEvent, on the other hand, is lean and up to the point, by only
152=head2 I/O WATCHERS 153=head2 I/O WATCHERS
153 154
154You can create an I/O watcher by calling the C<< AnyEvent->io >> method 155You can create an I/O watcher by calling the C<< AnyEvent->io >> method
155with the following mandatory key-value pairs as arguments: 156with the following mandatory key-value pairs as arguments:
156 157
157C<fh> the Perl I<file handle> (I<not> file descriptor) to watch 158C<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>, 159(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, 160must 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 161waiting for "r"eadable or "w"ritable events, respectively. C<cb> is the
161becomes ready. 162callback to invoke each time the file handle becomes ready.
162 163
163Although the callback might get passed parameters, their value and 164Although the callback might get passed parameters, their value and
164presence is undefined and you cannot rely on them. Portable AnyEvent 165presence is undefined and you cannot rely on them. Portable AnyEvent
165callbacks cannot use arguments passed to I/O watcher callbacks. 166callbacks cannot use arguments passed to I/O watcher callbacks.
166 167
193Although the callback might get passed parameters, their value and 194Although the callback might get passed parameters, their value and
194presence is undefined and you cannot rely on them. Portable AnyEvent 195presence is undefined and you cannot rely on them. Portable AnyEvent
195callbacks cannot use arguments passed to time watcher callbacks. 196callbacks cannot use arguments passed to time watcher callbacks.
196 197
197The callback will normally be invoked once only. If you specify another 198The callback will normally be invoked once only. If you specify another
198parameter, C<interval>, as a positive number, then the callback will be 199parameter, C<interval>, as a strictly positive number (> 0), then the
199invoked regularly at that interval (in fractional seconds) after the first 200callback will be invoked regularly at that interval (in fractional
200invocation. 201seconds) after the first invocation. If C<interval> is specified with a
202false value, then it is treated as if it were missing.
201 203
202The callback will be rescheduled before invoking the callback, but no 204The callback will be rescheduled before invoking the callback, but no
203attempt is done to avoid timer drift in most backends, so the interval is 205attempt is done to avoid timer drift in most backends, so the interval is
204only approximate. 206only approximate.
205 207
302=back 304=back
303 305
304=head2 SIGNAL WATCHERS 306=head2 SIGNAL WATCHERS
305 307
306You can watch for signals using a signal watcher, C<signal> is the signal 308You 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 309I<name> in uppercase and without any C<SIG> prefix, C<cb> is the Perl
308be invoked whenever a signal occurs. 310callback to be invoked whenever a signal occurs.
309 311
310Although the callback might get passed parameters, their value and 312Although the callback might get passed parameters, their value and
311presence is undefined and you cannot rely on them. Portable AnyEvent 313presence is undefined and you cannot rely on them. Portable AnyEvent
312callbacks cannot use arguments passed to signal watcher callbacks. 314callbacks cannot use arguments passed to signal watcher callbacks.
313 315
824no warnings; 826no warnings;
825use strict; 827use strict;
826 828
827use Carp; 829use Carp;
828 830
829our $VERSION = 4.2; 831our $VERSION = 4.21;
830our $MODEL; 832our $MODEL;
831 833
832our $AUTOLOAD; 834our $AUTOLOAD;
833our @ISA; 835our @ISA;
834 836
937 $MODEL 939 $MODEL
938 or die "No event module selected for AnyEvent and autodetect failed. Install any one of these modules: EV, Event or Glib."; 940 or die "No event module selected for AnyEvent and autodetect failed. Install any one of these modules: EV, Event or Glib.";
939 } 941 }
940 } 942 }
941 943
944 push @{"$MODEL\::ISA"}, "AnyEvent::Base";
945
942 unshift @ISA, $MODEL; 946 unshift @ISA, $MODEL;
943 push @{"$MODEL\::ISA"}, "AnyEvent::Base"; 947
948 require AnyEvent::Strict if $ENV{PERL_ANYEVENT_STRICT};
944 949
945 (shift @post_detect)->() while @post_detect; 950 (shift @post_detect)->() while @post_detect;
946 } 951 }
947 952
948 $MODEL 953 $MODEL
956 961
957 detect unless $MODEL; 962 detect unless $MODEL;
958 963
959 my $class = shift; 964 my $class = shift;
960 $class->$func (@_); 965 $class->$func (@_);
966}
967
968# utility function to dup a filehandle. this is used by many backends
969# to support binding more than one watcher per filehandle (they usually
970# allow only one watcher per fd, so we dup it to get a different one).
971sub _dupfh($$$$) {
972 my ($poll, $fh, $r, $w) = @_;
973
974 require Fcntl;
975
976 # cygwin requires the fh mode to be matching, unix doesn't
977 my ($rw, $mode) = $poll eq "r" ? ($r, "<")
978 : $poll eq "w" ? ($w, ">")
979 : Carp::croak "AnyEvent->io requires poll set to either 'r' or 'w'";
980
981 open my $fh2, "$mode&" . fileno $fh
982 or die "cannot dup() filehandle: $!";
983
984 # we assume CLOEXEC is already set by perl in all important cases
985
986 ($fh2, $rw)
961} 987}
962 988
963package AnyEvent::Base; 989package AnyEvent::Base;
964 990
965# default implementation for now and time 991# default implementation for now and time
1175conditions, such as not being able to load the event model specified by 1201conditions, such as not being able to load the event model specified by
1176C<PERL_ANYEVENT_MODEL>. 1202C<PERL_ANYEVENT_MODEL>.
1177 1203
1178When set to C<2> or higher, cause AnyEvent to report to STDERR which event 1204When set to C<2> or higher, cause AnyEvent to report to STDERR which event
1179model it chooses. 1205model it chooses.
1206
1207=item C<PERL_ANYEVENT_STRICT>
1208
1209AnyEvent does not do much argument checking by default, as thorough
1210argument checking is very costly. Setting this variable to a true value
1211will cause AnyEvent to load C<AnyEvent::Strict> and then to thoroughly
1212check the arguments passed to most method calls. If it finds any problems
1213it will croak.
1214
1215In other words, enables "strict" mode.
1216
1217Unlike C<use strict> it is definitely recommended ot keep it off in
1218production.
1180 1219
1181=item C<PERL_ANYEVENT_MODEL> 1220=item C<PERL_ANYEVENT_MODEL>
1182 1221
1183This can be used to specify the event model to be used by AnyEvent, before 1222This 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 1223auto detection and -probing kicks in. It must be a string consisting
1681 1720
1682 use AnyEvent; 1721 use AnyEvent;
1683 1722
1684Similar considerations apply to $ENV{PERL_ANYEVENT_VERBOSE}, as that can 1723Similar considerations apply to $ENV{PERL_ANYEVENT_VERBOSE}, as that can
1685be used to probe what backend is used and gain other information (which is 1724be used to probe what backend is used and gain other information (which is
1686probably even less useful to an attacker than PERL_ANYEVENT_MODEL). 1725probably even less useful to an attacker than PERL_ANYEVENT_MODEL), and
1726$ENV{PERL_ANYEGENT_STRICT}.
1687 1727
1688 1728
1689=head1 BUGS 1729=head1 BUGS
1690 1730
1691Perl 5.8 has numerous memleaks that sometimes hit this module and are hard 1731Perl 5.8 has numerous memleaks that sometimes hit this module and are hard

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines