… | |
… | |
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 |
… | |
… | |
38 | =head1 DESCRIPTION |
38 | =head1 DESCRIPTION |
39 | |
39 | |
40 | This module documents the new simpler AnyEvent API. |
40 | This module documents the new simpler AnyEvent API. |
41 | |
41 | |
42 | 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 |
43 | this API actually "works", despite it's lack of extensibility, leading to |
43 | this API actually "works", despite its lack of extensibility, leading to |
44 | a shorter, easier and faster API. |
44 | a shorter, easier and faster API. |
45 | |
45 | |
46 | The main difference to AnyEvent is that instead of method calls, function |
46 | The main differences from AnyEvent is that function calls are used |
47 | calls are used, and that no named arguments are used. |
47 | instead of method calls, and that no named arguments are used. |
48 | |
48 | |
49 | This makes calls to watcher creation functions really short, which can |
49 | This makes calls to watcher creation functions really short, which can |
50 | make a program more readable, despite the lack of named parameters. |
50 | make a program more readable despite the lack of named parameters. |
51 | 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 |
52 | many mistakes are caught at compiletime with this API. |
52 | many mistakes are caught at compile-time with this API. |
53 | |
53 | |
54 | 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 |
55 | overhead is very noticeable (with EV it increases the execution time five- |
55 | overhead is very noticeable (with EV it increases the execution time five- |
56 | to 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). |
57 | |
57 | |
58 | At the moment, there will be no checking (L<AnyEvent::Strict> does not |
58 | At the moment, there is no checking (L<AnyEvent::Strict> does not |
59 | affect his API), so the L<AnyEvent> API has a definite advantage here |
59 | affect this API), so the L<AnyEvent> API still has a definite advantage |
60 | still. |
60 | here. |
61 | |
61 | |
62 | 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, |
63 | the AnyEvent API. Both APIs can be used interchangably and and there are |
63 | the AnyEvent API. Both APIs can be used interchangeably and and there are |
64 | no plans to "switch", so if in doubt, feel free to use the L<AnyEvent> |
64 | no plans to "switch", so if in doubt, feel free to use the L<AnyEvent> |
65 | API in new code. |
65 | API in new code. |
66 | |
66 | |
67 | 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 |
68 | available, so you still need to use AnyEvent for the finer stuff. Also, |
68 | available, and you still need to use AnyEvent for the finer stuff. Also, |
69 | 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 |
70 | namespace. |
70 | namespace. |
71 | |
71 | |
72 | =head2 FUNCTIONS |
72 | =head2 FUNCTIONS |
73 | |
73 | |
74 | This section briefly describes the alternative watcher |
74 | This section briefly describes the alternative watcher |
75 | constructors. Semantics and any methods are not described here, please |
75 | constructors. Semantics are not described here; please |
76 | refer to the L<AnyEvent> manpage for the details. |
76 | refer to the L<AnyEvent> manpage for the details. |
77 | |
77 | |
78 | =over 4 |
78 | =over 4 |
79 | |
79 | |
80 | =cut |
80 | =cut |
… | |
… | |
96 | |
96 | |
97 | Example: wait until STDIN becomes readable. |
97 | Example: wait until STDIN becomes readable. |
98 | |
98 | |
99 | $stdin_ready = AE::io *STDIN, 0, sub { scalar <STDIN> }; |
99 | $stdin_ready = AE::io *STDIN, 0, sub { scalar <STDIN> }; |
100 | |
100 | |
101 | Example. wait until STDOUT becomes writable and print something. |
101 | Example: wait until STDOUT becomes writable and print something. |
102 | |
102 | |
103 | $stdout_ready = AE::io *STDOUT, 1, sub { print STDOUT "woaw\n" }; |
103 | $stdout_ready = AE::io *STDOUT, 1, sub { print STDOUT "woaw\n" }; |
104 | |
104 | |
105 | =item $w = AE::timer $after, $interval, $cb |
105 | =item $w = AE::timer $after, $interval, $cb |
106 | |
106 | |
107 | Creates a timer watcher that invokes the callback C<$cb> after at least |
107 | Creates a timer watcher that invokes the callback C<$cb> after at least |
108 | C<$after> second have passed (C<$after> can be negative or C<0>). |
108 | C<$after> second have passed (C<$after> can be negative or C<0>). |
109 | |
109 | |
110 | If C<$interval> is C<0>, then the clalback will only be invoked once, |
110 | If C<$interval> is C<0>, then the callback will only be invoked once, |
111 | otherwise it must be a positive number of seconds that specified the |
111 | otherwise it must be a positive number of seconds that specifies the |
112 | interval between successive invocations of the callback. |
112 | interval between successive invocations of the callback. |
113 | |
113 | |
114 | Example: print "too late" after at least one second has passed. |
114 | Example: print "too late" after at least one second has passed. |
115 | |
115 | |
116 | $timer_once = AE::timer 1, 0, sub { print "too late\n" }; |
116 | $timer_once = AE::timer 1, 0, sub { print "too late\n" }; |
… | |
… | |
119 | |
119 | |
120 | $timer_repeated = AE::timer 0, 1, sub { print "blubb\n" }; |
120 | $timer_repeated = AE::timer 0, 1, sub { print "blubb\n" }; |
121 | |
121 | |
122 | =item $w = AE::signal $signame, $cb |
122 | =item $w = AE::signal $signame, $cb |
123 | |
123 | |
124 | Invoke the callback c<$cb> each time one or more occurences of the named |
124 | Invoke the callback C<$cb> each time one or more occurrences of the |
125 | signal C<$signame> are detected. |
125 | named signal C<$signame> are detected. |
126 | |
126 | |
127 | =item $w = AE::child $pid, $cb |
127 | =item $w = AE::child $pid, $cb |
128 | |
128 | |
129 | Invokes the callbakc C<$cb> when the child with the given C<$pid> exits |
129 | Invokes the callback C<$cb> when the child with the given C<$pid> exits |
130 | (or all children, when C<$pid> is zero). |
130 | (or all children, when C<$pid> is zero). |
131 | |
131 | |
132 | The callback will get the actual pid and exit status as arguments. |
132 | The callback will get the actual pid and exit status as arguments. |
133 | |
133 | |
134 | =item $w = AE::idle $cb |
134 | =item $w = AE::idle $cb |