| 1 |
root |
1.1 |
=head1 NAME |
| 2 |
|
|
|
| 3 |
|
|
AE - simpler/faster/newer/cooler AnyEvent API |
| 4 |
|
|
|
| 5 |
|
|
=head1 SYNOPSIS |
| 6 |
|
|
|
| 7 |
root |
1.2 |
use AnyEvent; # not AE |
| 8 |
root |
1.1 |
|
| 9 |
|
|
=head1 DESCRIPTION |
| 10 |
|
|
|
| 11 |
root |
1.2 |
This module documents the new simpler AnyEvent API. |
| 12 |
root |
1.1 |
|
| 13 |
|
|
The rationale for the new API is that experience with L<EV> shows that |
| 14 |
root |
1.2 |
this API actually "works", despite it's lack of extensibility. |
| 15 |
root |
1.1 |
|
| 16 |
root |
1.2 |
The main difference to AnyEvent is that instead of method calls, function |
| 17 |
|
|
calls are used, and that no named arguments are used. |
| 18 |
|
|
|
| 19 |
|
|
This makes calls to watcher creation functions really short, which can |
| 20 |
|
|
make a program more readable, despite the lack of named parameters. |
| 21 |
|
|
Function calls also allow more static type checking than method calls, so |
| 22 |
|
|
many mistakes are caught at compiletime with this API. |
| 23 |
|
|
|
| 24 |
|
|
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 |
| 26 |
|
|
six-fold, with Perl the method call overhead is about a factor of two). |
| 27 |
|
|
|
| 28 |
|
|
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 |
| 30 |
|
|
still. |
| 31 |
|
|
|
| 32 |
|
|
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 |
| 34 |
|
|
no plans to "switch", so if in doubt, use L<AnyEvent>'s API. |
| 35 |
|
|
|
| 36 |
|
|
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, |
| 38 |
|
|
you should not C<use AE> directly, C<use AnyEvent> will provide the AE |
| 39 |
|
|
namespace. |
| 40 |
|
|
|
| 41 |
|
|
=head2 FUNCTIONS |
| 42 |
|
|
|
| 43 |
|
|
This section briefly describes the alternative watcher |
| 44 |
|
|
constructors. Semantics and any methods are not described here, please |
| 45 |
|
|
refer to the L<AnyEvent> manpage for the details. |
| 46 |
|
|
|
| 47 |
|
|
=over 4 |
| 48 |
root |
1.1 |
|
| 49 |
|
|
=cut |
| 50 |
|
|
|
| 51 |
|
|
package AE; |
| 52 |
|
|
|
| 53 |
|
|
use AnyEvent (); # BEGIN { AnyEvent::common_sense } |
| 54 |
|
|
|
| 55 |
root |
1.2 |
=item $w = AE::io $fh_or_fd, $watch_write, $cb |
| 56 |
|
|
|
| 57 |
|
|
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 |
| 59 |
|
|
file descriptor C<$fh_or_fd>. |
| 60 |
|
|
|
| 61 |
|
|
The callback C<$cb> is invoked as soon and as long as I/O of the type |
| 62 |
|
|
specified by C<$watch_write>) can be done on the file handle/descriptor. |
| 63 |
|
|
|
| 64 |
|
|
Example: wait until STDIN becomes readable. |
| 65 |
|
|
|
| 66 |
|
|
$stdin_ready = AE::io *STDIN, 0, sub { scalar <STDIN> }; |
| 67 |
|
|
|
| 68 |
|
|
Example. wait until STDOUT becomes writable and print something. |
| 69 |
|
|
|
| 70 |
|
|
$stdout_ready = AE::io *STDOUT, 1, sub { print STDOUT "woaw\n" }; |
| 71 |
|
|
|
| 72 |
|
|
=item $w = AE::timer $after, $interval, $cb |
| 73 |
|
|
|
| 74 |
|
|
Creates a timer watcher that invokes the callback C<$cb> after at least |
| 75 |
|
|
C<$after> second have passed (C<$after> can be negative or C<0>). |
| 76 |
|
|
|
| 77 |
|
|
If C<$interval> is C<0>, then the clalback will only be invoked once, |
| 78 |
|
|
otherwise it must be a positive number of seconds that specified the |
| 79 |
|
|
interval between successive invocations of the callback. |
| 80 |
|
|
|
| 81 |
|
|
Example: print "too late" after at least one second has passed. |
| 82 |
|
|
|
| 83 |
|
|
$timer_once = AE::timer 1, 0, sub { print "too late\n" }; |
| 84 |
|
|
|
| 85 |
|
|
Example: print "blubb" once a second, starting as soon as possible. |
| 86 |
|
|
|
| 87 |
|
|
$timer_repeated = AE::timer 0, 1, sub { print "blubb\n" }; |
| 88 |
|
|
|
| 89 |
|
|
=item $w = AE::signal $signame, $cb |
| 90 |
|
|
|
| 91 |
|
|
Invoke the callback c<$cb> each time one or more occurences of the named |
| 92 |
|
|
signal C<$signame> are detected. |
| 93 |
|
|
|
| 94 |
|
|
=item $w = AE::child $pid, $cb |
| 95 |
|
|
|
| 96 |
|
|
Invokes the callbakc C<$cb> when the child with the given C<$pid> exits |
| 97 |
|
|
(or all children, when C<$pid> is zero). |
| 98 |
|
|
|
| 99 |
|
|
The callback will get the actual pid and exit status as arguments. |
| 100 |
|
|
|
| 101 |
|
|
=item $w = AE::idle $cb |
| 102 |
|
|
|
| 103 |
|
|
Invoke the callback C<$cb> each time the event loop is "idle" (has no |
| 104 |
|
|
events outstanding), but do not prevent the event loop from polling for |
| 105 |
|
|
more events. |
| 106 |
|
|
|
| 107 |
|
|
=item $cv = AE::cv |
| 108 |
|
|
|
| 109 |
|
|
=item $cv = AE::cv { BLOCK } |
| 110 |
|
|
|
| 111 |
|
|
Create a new condition variable. The first form is identical to C<< |
| 112 |
|
|
AnyEvent->condvar >>, the second form additionally sets the callback (as |
| 113 |
|
|
if the C<cb> method is called on the condition variable). |
| 114 |
|
|
|
| 115 |
|
|
=item AE::now |
| 116 |
|
|
|
| 117 |
|
|
Returns the current event loop time (may be cached by the event loop). |
| 118 |
|
|
|
| 119 |
|
|
=item AE::now_update |
| 120 |
|
|
|
| 121 |
|
|
Ensures that the current event loop time is up to date. |
| 122 |
|
|
|
| 123 |
|
|
=item AE::time |
| 124 |
|
|
|
| 125 |
|
|
Return the current time (not cached, always consults a hardware clock). |
| 126 |
|
|
|
| 127 |
|
|
=back |
| 128 |
root |
1.1 |
|
| 129 |
|
|
=head1 AUTHOR |
| 130 |
|
|
|
| 131 |
|
|
Marc Lehmann <schmorp@schmorp.de> |
| 132 |
|
|
http://home.schmorp.de/ |
| 133 |
|
|
|
| 134 |
|
|
=cut |
| 135 |
|
|
|
| 136 |
root |
1.2 |
1 |
| 137 |
|
|
|