… | |
… | |
4 | |
4 | |
5 | =head1 SYNOPSIS |
5 | =head1 SYNOPSIS |
6 | |
6 | |
7 | use AnyEvent; # not AE |
7 | use AnyEvent; # not AE |
8 | |
8 | |
|
|
9 | # file handle or descriptor readable |
|
|
10 | my $w = AE::io $fh, 0, sub { ... }; |
|
|
11 | |
|
|
12 | # one-shot or repeating timers |
|
|
13 | my $w = AE::timer $seconds, 0, sub { ... }; # once |
|
|
14 | my $w = AE::timer $seconds, interval, sub { ... }; # repeated |
|
|
15 | |
|
|
16 | print AE::now; # prints current event loop time |
|
|
17 | print AE::time; # think Time::HiRes::time or simply CORE::time. |
|
|
18 | |
|
|
19 | # POSIX signal |
|
|
20 | my $w = AE::signal TERM => sub { ... }; |
|
|
21 | |
|
|
22 | # child process exit |
|
|
23 | my $w = AE::child $pid, sub { |
|
|
24 | my ($pid, $status) = @_; |
|
|
25 | ... |
|
|
26 | }; |
|
|
27 | |
|
|
28 | # called when event loop idle (if applicable) |
|
|
29 | my $w = AE::idle { ... }; |
|
|
30 | |
|
|
31 | my $w = AE::cv; # stores whether a condition was flagged |
|
|
32 | $w->send; # wake up current and all future recv's |
|
|
33 | $w->recv; # enters "main loop" till $condvar gets ->send |
|
|
34 | # use a condvar in callback mode: |
|
|
35 | $w->cb (sub { $_[0]->recv }); |
|
|
36 | |
|
|
37 | |
9 | =head1 DESCRIPTION |
38 | =head1 DESCRIPTION |
10 | |
39 | |
11 | This module documents the new simpler AnyEvent API. |
40 | This module documents the new simpler AnyEvent API. |
12 | |
41 | |
13 | The rationale for the new API is that experience with L<EV> shows that |
42 | The rationale for the new API is that experience with L<EV> shows that |
14 | this API actually "works", despite it's lack of extensibility. |
43 | this API actually "works", despite it's lack of extensibility, leading to |
|
|
44 | a shorter, easier and faster API. |
15 | |
45 | |
16 | The main difference to AnyEvent is that instead of method calls, function |
46 | The main difference to AnyEvent is that instead of method calls, function |
17 | calls are used, and that no named arguments are used. |
47 | calls are used, and that no named arguments are used. |
18 | |
48 | |
19 | This makes calls to watcher creation functions really short, which can |
49 | This makes calls to watcher creation functions really short, which can |
20 | make a program more readable, despite the lack of named parameters. |
50 | make a program more readable, despite the lack of named parameters. |
21 | Function calls also allow more static type checking than method calls, so |
51 | Function calls also allow more static type checking than method calls, so |
22 | many mistakes are caught at compiletime with this API. |
52 | many mistakes are caught at compiletime with this API. |
23 | |
53 | |
24 | Also, some backends (Perl and EV) are so fast that the method call |
54 | Also, some backends (Perl and EV) are so fast that the method call |
25 | overhead is very noticable (with EV it increases the time five- to |
55 | overhead is very noticeable (with EV it increases the execution time five- |
26 | six-fold, with Perl the method call overhead is about a factor of two). |
56 | to six-fold, with Perl the method call overhead is about a factor of two). |
27 | |
57 | |
28 | At the moment, there will be no checking (L<AnyEvent::Strict> does not |
58 | At the moment, there will be no checking (L<AnyEvent::Strict> does not |
29 | affect his API), so the L<AnyEvent> API has a definite advantage here |
59 | affect his API), so the L<AnyEvent> API has a definite advantage here |
30 | still. |
60 | still. |
31 | |
61 | |
32 | Note that the C<AE> API is an alternative to, not the future version of, |
62 | Note that the C<AE> API is an alternative to, not the future version of, |
33 | the AnyEvent API. Both APIs can be used interchangably and and there are |
63 | the AnyEvent API. Both APIs can be used interchangably and and there are |
34 | no plans to "switch", so if in doubt, use L<AnyEvent>'s API. |
64 | no plans to "switch", so if in doubt, feel free to use the L<AnyEvent> |
|
|
65 | API in new code. |
35 | |
66 | |
36 | As the AE API is complementary, not everything in the AnyEvent API is |
67 | As the AE API is complementary, not everything in the AnyEvent API is |
37 | available, so you still need to use AnyEvent for the finer stuff. Also, |
68 | available, so you still need to use AnyEvent for the finer stuff. Also, |
38 | you should not C<use AE> directly, C<use AnyEvent> will provide the AE |
69 | you should not C<use AE> directly, C<use AnyEvent> will provide the AE |
39 | namespace. |
70 | namespace. |
… | |
… | |
49 | =cut |
80 | =cut |
50 | |
81 | |
51 | package AE; |
82 | package AE; |
52 | |
83 | |
53 | use AnyEvent (); # BEGIN { AnyEvent::common_sense } |
84 | use AnyEvent (); # BEGIN { AnyEvent::common_sense } |
|
|
85 | |
|
|
86 | our $VERSION = $AnyEvent::VERSION; |
54 | |
87 | |
55 | =item $w = AE::io $fh_or_fd, $watch_write, $cb |
88 | =item $w = AE::io $fh_or_fd, $watch_write, $cb |
56 | |
89 | |
57 | Creates an I/O watcher that listens for read events (C<$watch_write> |
90 | Creates an I/O watcher that listens for read events (C<$watch_write> |
58 | false) or write events (C<$watch_write> is true) on the file handle or |
91 | false) or write events (C<$watch_write> is true) on the file handle or |