ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/lib/AE.pm
(Generate patch)

Comparing AnyEvent/lib/AE.pm (file contents):
Revision 1.7 by root, Wed Mar 24 23:28:06 2010 UTC vs.
Revision 1.12 by root, Wed Aug 17 22:03:02 2011 UTC

8 8
9 # file handle or descriptor readable 9 # file handle or descriptor readable
10 my $w = AE::io $fh, 0, sub { ... }; 10 my $w = AE::io $fh, 0, sub { ... };
11 11
12 # one-shot or repeating timers 12 # one-shot or repeating timers
13 my $w = AE::timer $seconds, 0, sub { ... }; # once 13 my $w = AE::timer $seconds, 0, sub { ... }; # once
14 my $w = AE::timer $seconds, interval, sub { ... }; # repeated 14 my $w = AE::timer $seconds, $interval, sub { ... }; # repeated
15 15
16 print AE::now; # prints current event loop time 16 print AE::now; # prints current event loop time
17 print AE::time; # think Time::HiRes::time or simply CORE::time. 17 print AE::time; # think Time::HiRes::time or simply CORE::time.
18 18
19 # POSIX signal 19 # POSIX signal
24 my ($pid, $status) = @_; 24 my ($pid, $status) = @_;
25 ... 25 ...
26 }; 26 };
27 27
28 # called when event loop idle (if applicable) 28 # called when event loop idle (if applicable)
29 my $w = AE::idle { ... }; 29 my $w = AE::idle sub { ... };
30 30
31 my $w = AE::cv; # stores whether a condition was flagged 31 my $w = AE::cv; # stores whether a condition was flagged
32 $w->send; # wake up current and all future recv's 32 $w->send; # wake up current and all future recv's
33 $w->recv; # enters "main loop" till $condvar gets ->send 33 $w->recv; # enters "main loop" till $condvar gets ->send
34 # use a condvar in callback mode: 34 # use a condvar in callback mode:
38=head1 DESCRIPTION 38=head1 DESCRIPTION
39 39
40This module documents the new simpler AnyEvent API. 40This module documents the new simpler AnyEvent API.
41 41
42The rationale for the new API is that experience with L<EV> shows that 42The rationale for the new API is that experience with L<EV> shows that
43this API actually "works", despite it's lack of extensibility, leading to 43this API actually "works", despite its lack of extensibility, leading to
44a shorter, easier and faster API. 44a shorter, easier and faster API.
45 45
46The main difference to AnyEvent is that instead of method calls, function 46The main differences from AnyEvent is that function calls are used
47calls are used, and that no named arguments are used. 47instead of method calls, and that no named arguments are used.
48 48
49This makes calls to watcher creation functions really short, which can 49This makes calls to watcher creation functions really short, which can
50make a program more readable, despite the lack of named parameters. 50make a program more readable despite the lack of named parameters.
51Function calls also allow more static type checking than method calls, so 51Function calls also allow more static type checking than method calls, so
52many mistakes are caught at compiletime with this API. 52many mistakes are caught at compile-time with this API.
53 53
54Also, some backends (Perl and EV) are so fast that the method call 54Also, some backends (Perl and EV) are so fast that the method call
55overhead is very noticeable (with EV it increases the execution time five- 55overhead is very noticeable (with EV it increases the execution time five-
56to six-fold, with Perl the method call overhead is about a factor of two). 56to six-fold, with Perl the method call overhead is about a factor of two).
57 57
58At the moment, there will be no checking (L<AnyEvent::Strict> does not
59affect his API), so the L<AnyEvent> API has a definite advantage here
60still.
61
62Note that the C<AE> API is an alternative to, not the future version of, 58Note that the C<AE> API is an alternative to, not the future version of,
63the AnyEvent API. Both APIs can be used interchangably and and there are 59the AnyEvent API. Both APIs can be used interchangeably and there are
64no plans to "switch", so if in doubt, feel free to use the L<AnyEvent> 60no plans to "switch", so if in doubt, feel free to use the L<AnyEvent>
65API in new code. 61API in new code.
66 62
67As the AE API is complementary, not everything in the AnyEvent API is 63As the AE API is complementary, not everything in the AnyEvent API is
68available, so you still need to use AnyEvent for the finer stuff. Also, 64available, and you still need to use AnyEvent for the finer stuff. Also,
69you should not C<use AE> directly, C<use AnyEvent> will provide the AE 65you should not C<use AE> directly, C<use AnyEvent> will provide the AE
70namespace. 66namespace.
67
68At the moment, these functions will become slower then their method-call
69counterparts when using L<AnyEvent::Strict> or L<AnyEvent::Debug>::wrap.
71 70
72=head2 FUNCTIONS 71=head2 FUNCTIONS
73 72
74This section briefly describes the alternative watcher 73This section briefly describes the alternative watcher
75constructors. Semantics and any methods are not described here, please 74constructors. Semantics are not described here; please
76refer to the L<AnyEvent> manpage for the details. 75refer to the L<AnyEvent> manpage for the details.
77 76
78=over 4 77=over 4
79 78
80=cut 79=cut
96 95
97Example: wait until STDIN becomes readable. 96Example: wait until STDIN becomes readable.
98 97
99 $stdin_ready = AE::io *STDIN, 0, sub { scalar <STDIN> }; 98 $stdin_ready = AE::io *STDIN, 0, sub { scalar <STDIN> };
100 99
101Example. wait until STDOUT becomes writable and print something. 100Example: wait until STDOUT becomes writable and print something.
102 101
103 $stdout_ready = AE::io *STDOUT, 1, sub { print STDOUT "woaw\n" }; 102 $stdout_ready = AE::io *STDOUT, 1, sub { print STDOUT "woaw\n" };
104 103
105=item $w = AE::timer $after, $interval, $cb 104=item $w = AE::timer $after, $interval, $cb
106 105
107Creates a timer watcher that invokes the callback C<$cb> after at least 106Creates a timer watcher that invokes the callback C<$cb> after at least
108C<$after> second have passed (C<$after> can be negative or C<0>). 107C<$after> second have passed (C<$after> can be negative or C<0>).
109 108
110If C<$interval> is C<0>, then the clalback will only be invoked once, 109If C<$interval> is C<0>, then the callback will only be invoked once,
111otherwise it must be a positive number of seconds that specified the 110otherwise it must be a positive number of seconds that specifies the
112interval between successive invocations of the callback. 111interval between successive invocations of the callback.
113 112
114Example: print "too late" after at least one second has passed. 113Example: print "too late" after at least one second has passed.
115 114
116 $timer_once = AE::timer 1, 0, sub { print "too late\n" }; 115 $timer_once = AE::timer 1, 0, sub { print "too late\n" };
119 118
120 $timer_repeated = AE::timer 0, 1, sub { print "blubb\n" }; 119 $timer_repeated = AE::timer 0, 1, sub { print "blubb\n" };
121 120
122=item $w = AE::signal $signame, $cb 121=item $w = AE::signal $signame, $cb
123 122
124Invoke the callback c<$cb> each time one or more occurences of the named 123Invoke the callback C<$cb> each time one or more occurrences of the
125signal C<$signame> are detected. 124named signal C<$signame> are detected.
126 125
127=item $w = AE::child $pid, $cb 126=item $w = AE::child $pid, $cb
128 127
129Invokes the callbakc C<$cb> when the child with the given C<$pid> exits 128Invokes the callback C<$cb> when the child with the given C<$pid> exits
130(or all children, when C<$pid> is zero). 129(or all children, when C<$pid> is zero).
131 130
132The callback will get the actual pid and exit status as arguments. 131The callback will get the actual pid and exit status as arguments.
133 132
134=item $w = AE::idle $cb 133=item $w = AE::idle $cb
155 154
156=item AE::time 155=item AE::time
157 156
158Return the current time (not cached, always consults a hardware clock). 157Return the current time (not cached, always consults a hardware clock).
159 158
159=item AE::postpone { BLOCK }
160
161Exactly the same as C<AnyEvent:::postpone>.
162
163=item AE::log $level, $msg[, @args]
164
165Exactly the same as C<AnyEvent::log> (or C<AnyEvent::Log::log>).
166
160=back 167=back
161 168
162=head1 AUTHOR 169=head1 AUTHOR
163 170
164 Marc Lehmann <schmorp@schmorp.de> 171 Marc Lehmann <schmorp@schmorp.de>

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines