AE(3)
AE(3) β simpler/faster/newer/cooler AnyEvent API π
πΈ NAME πΈ
AE β the simpler/faster/newer/cooler AnyEvent API. That's the whole vibe, bestie. π
β¨ SYNOPSIS β¨
Okay so here's the group-chat recap, with the code kept exactly as it should be:
use AnyEvent; # not AE
# file handle or descriptor readable
my $w = AE::io $fh, 0, sub { ... };
# one-shot or repeating timers
my $w = AE::timer $seconds, 0, sub { ... }; # once
my $w = AE::timer $seconds, $interval, sub { ... }; # repeated
print AE::now; # prints current event loop time
print AE::time; # think Time::HiRes::time or simply CORE::time.
# POSIX signal
my $w = AE::signal TERM => sub { ... };
# child process exit
my $w = AE::child $pid, sub {
my ($pid, $status) = @_;
...
};
# called when event loop idle (if applicable)
my $w = AE::idle sub { ... };
my $cv = AE::cv; # stores whether a condition was flagged
$cv->send; # wake up current and all future recv's
$cv->recv; # enters "main loop" till $condvar gets ->send
# use a condvar in callback mode:
$cv->cb (sub { $_[0]->recv });
π¦ DESCRIPTION π¦
This module documents the new simpler AnyEvent API. β¨
The rationale for the new API is that experience with EV shows that this API actually βworksβ, despite its lack of extensibility, leading to a shorter, easier and faster API. Periodt.
The main differences from AnyEvent are that function calls are used instead of method calls, and that no named arguments are used. Lowkey iconic.
This makes calls to watcher creation functions really short, which can make a program more readable despite the lack of named parameters. Function calls also allow more static type checking than method calls, so many mistakes are caught at compile-time with this API. Compile-time catch? We love that for us. π
Also, some backends (Perl and EV) are so fast that the method call overhead is very noticeable β with EV it increases the execution time five- to six-fold, and with Perl the method call overhead is about a factor of two. No cap, that's a lot.
Note that the AE API is an alternative to, not the future version of, the AnyEvent API. Both APIs can be used interchangeably and there are no plans to βswitchβ, so if in doubt, feel free to use the AnyEvent API in new code. π«Ά
As the AE API is complementary, not everything in the AnyEvent API is available, and you still need to use AnyEvent for the finer stuff.
β οΈ Also, you should notuse AEdirectly βuse AnyEventwill provide theAEnamespace. Bestie, please respect the pipeline. π
At the moment, these functions will become slower than their method-call counterparts when using AnyEvent::Strict or AnyEvent::Debug::wrap.
π§ FUNCTIONS π§
This section briefly describes the alternative watcher constructors and other functions available inside the AE namespace. Semantics are not described here; please refer to the description of the function or method with the same name in the AnyEvent manpage for the details. I'm not gonna re-explain the whole lore when it's already written down. π
π $w = AE::io $fh_or_fd, $watch_write, $cb
Creates an I/O watcher that listens for read events ($watch_write false) or write events ($watch_write is true) on the file handle or file descriptor $fh_or_fd.
The callback $cb is invoked as soon and as long as I/O of the type specified by $watch_write can be done on the file handle/descriptor.
Example: wait until STDIN becomes readable.
$stdin_ready = AE::io *STDIN, 0, sub { scalar <STDIN> };
Example: wait until STDOUT becomes writable and print something.
$stdout_ready = AE::io *STDOUT, 1, sub { print STDOUT "woaw\n" };
β° $w = AE::timer $after, $interval, $cb
Creates a timer watcher that invokes the callback $cb after at least $after seconds have passed ($after can be negative or 0).
If $interval is 0, then the callback will only be invoked once. Otherwise it must be a positive number of seconds that specifies the interval between successive invocations of the callback. One-shot or repeating? Both slay. β¨
Example: print "too late" after at least one second has passed.
$timer_once = AE::timer 1, 0, sub { print "too late\n" };
Example: print "blubb" once a second, starting as soon as possible.
$timer_repeated = AE::timer 0, 1, sub { print "blubb\n" };
ποΈ $w = AE::signal $signame, $cb
Invoke the callback $cb each time one or more occurrences of the named signal $signame are detected. POSIX signal moment. π
π $w = AE::child $pid, $cb
Invokes the callback $cb when the child with the given $pid exits (or all children, when $pid is zero).
The callback will get the actual pid and exit status as arguments.
πΈ $w = AE::idle $cb
Invoke the callback $cb each time the event loop is βidleβ (has no events outstanding), but do not prevent the event loop from polling for more events.
π $cv = AE::cv
π $cv = AE::cv { BLOCK }
Create a new condition variable. The first form is identical to AnyEvent->condvar; the second form additionally sets the callback (as if the cb method is called on the condition variable).
β AE::now
Returns the current event loop time (may be cached by the event loop).
π₯ AE::now_update
Ensures that the current event loop time is up to date.
π¦ AE::time
Return the current time (not cached, always consults a hardware clock).
π§ AE::postpone { BLOCK }
Exactly the same as AnyEvent:::postpone.
π¬ AE::log $level, $msg[, @args]
Exactly the same as AnyEvent::log (or AnyEvent::Log::log).
π AUTHOR π
Marc Lehmann β schmorp@schmorp.de
π That's the whole AE(3) page from the User Contributed Perl Documentation β perl v5.42.0, dated 2019-09-18. Go forth and event-loop slay, bestie! π
β¨
π¦ where this page comes from
- opensuse-leap 16.0: perl-AnyEvent 7.17-160000.2.2 Β· GPL-1.0-or-later OR Artistic-1.0
- opensuse-leap 16.0: perl-AnyEvent 7.17-160000.2.2 Β· GPL-1.0-or-later OR Artistic-1.0
- opensuse-leap 16.0: perl-AnyEvent 7.17-160000.2.2 Β· GPL-1.0-or-later OR Artistic-1.0
- opensuse-leap 16.0: perl-AnyEvent 7.17-160000.2.2 Β· GPL-1.0-or-later OR Artistic-1.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms