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.6 by root, Wed Mar 24 21:22:57 2010 UTC vs.
Revision 1.14 by root, Wed Oct 5 23:12:07 2011 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines