1AE(3) User Contributed Perl Documentation AE(3)
2
3
4
6 AE - simpler/faster/newer/cooler AnyEvent API
7
9 use AnyEvent; # not AE
10
11 # file handle or descriptor readable
12 my $w = AE::io $fh, 0, sub { ... };
13
14 # one-shot or repeating timers
15 my $w = AE::timer $seconds, 0, sub { ... }; # once
16 my $w = AE::timer $seconds, interval, sub { ... }; # repeated
17
18 print AE::now; # prints current event loop time
19 print AE::time; # think Time::HiRes::time or simply CORE::time.
20
21 # POSIX signal
22 my $w = AE::signal TERM => sub { ... };
23
24 # child process exit
25 my $w = AE::child $pid, sub {
26 my ($pid, $status) = @_;
27 ...
28 };
29
30 # called when event loop idle (if applicable)
31 my $w = AE::idle { ... };
32
33 my $w = AE::cv; # stores whether a condition was flagged
34 $w->send; # wake up current and all future recv's
35 $w->recv; # enters "main loop" till $condvar gets ->send
36 # use a condvar in callback mode:
37 $w->cb (sub { $_[0]->recv });
38
40 This module documents the new simpler AnyEvent API.
41
42 The rationale for the new API is that experience with EV shows that
43 this API actually "works", despite it's lack of extensibility, leading
44 to a shorter, easier and faster API.
45
46 The main difference to AnyEvent is that instead of method calls,
47 function calls are used, and that no named arguments are used.
48
49 This makes calls to watcher creation functions really short, which can
50 make a program more readable, despite the lack of named parameters.
51 Function calls also allow more static type checking than method calls,
52 so many mistakes are caught at compiletime with this API.
53
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
56 five- to six-fold, with Perl the method call overhead is about a factor
57 of two).
58
59 At the moment, there will be no checking (AnyEvent::Strict does not
60 affect his API), so the AnyEvent API has a definite advantage here
61 still.
62
63 Note that the "AE" API is an alternative to, not the future version of,
64 the AnyEvent API. Both APIs can be used interchangably and and there
65 are no plans to "switch", so if in doubt, feel free to use the AnyEvent
66 API in new code.
67
68 As the AE API is complementary, not everything in the AnyEvent API is
69 available, so you still need to use AnyEvent for the finer stuff. Also,
70 you should not "use AE" directly, "use AnyEvent" will provide the AE
71 namespace.
72
73 FUNCTIONS
74 This section briefly describes the alternative watcher constructors.
75 Semantics and any methods are not described here, please refer to the
76 AnyEvent manpage for the details.
77
78 $w = AE::io $fh_or_fd, $watch_write, $cb
79 Creates an I/O watcher that listens for read events ($watch_write
80 false) or write events ($watch_write is true) on the file handle or
81 file descriptor $fh_or_fd.
82
83 The callback $cb is invoked as soon and as long as I/O of the type
84 specified by $watch_write) can be done on the file
85 handle/descriptor.
86
87 Example: wait until STDIN becomes readable.
88
89 $stdin_ready = AE::io *STDIN, 0, sub { scalar <STDIN> };
90
91 Example. wait until STDOUT becomes writable and print something.
92
93 $stdout_ready = AE::io *STDOUT, 1, sub { print STDOUT "woaw\n" };
94
95 $w = AE::timer $after, $interval, $cb
96 Creates a timer watcher that invokes the callback $cb after at
97 least $after second have passed ($after can be negative or 0).
98
99 If $interval is 0, then the clalback will only be invoked once,
100 otherwise it must be a positive number of seconds that specified
101 the interval between successive invocations of the callback.
102
103 Example: print "too late" after at least one second has passed.
104
105 $timer_once = AE::timer 1, 0, sub { print "too late\n" };
106
107 Example: print "blubb" once a second, starting as soon as possible.
108
109 $timer_repeated = AE::timer 0, 1, sub { print "blubb\n" };
110
111 $w = AE::signal $signame, $cb
112 Invoke the callback c<$cb> each time one or more occurences of the
113 named signal $signame are detected.
114
115 $w = AE::child $pid, $cb
116 Invokes the callbakc $cb when the child with the given $pid exits
117 (or all children, when $pid is zero).
118
119 The callback will get the actual pid and exit status as arguments.
120
121 $w = AE::idle $cb
122 Invoke the callback $cb each time the event loop is "idle" (has no
123 events outstanding), but do not prevent the event loop from polling
124 for more events.
125
126 $cv = AE::cv
127 $cv = AE::cv { BLOCK }
128 Create a new condition variable. The first form is identical to
129 "AnyEvent->condvar", the second form additionally sets the callback
130 (as if the "cb" method is called on the condition variable).
131
132 AE::now
133 Returns the current event loop time (may be cached by the event
134 loop).
135
136 AE::now_update
137 Ensures that the current event loop time is up to date.
138
139 AE::time
140 Return the current time (not cached, always consults a hardware
141 clock).
142
144 Marc Lehmann <schmorp@schmorp.de>
145 http://home.schmorp.de/
146
147
148
149perl v5.12.1 2010-03-24 AE(3)