Util(3pm)
π Util(3pm) β Coro::Util π
okay so bestie, we're reading the manual page for Util from the Coro fam β it's a Perl user-contributed doc, section 3pm, and it's genuinely so much less scary than it looks, i promise π«Ά
β¨ NAME β¨
Coro::Util β various utility functions. that's it, that's the whole vibe π
β¨ SYNOPSIS β¨
use Coro::Util;
literally one line. iconic. no cap. π
β¨ DESCRIPTION β¨
okay so this module is a little grab-bag of utility functions, and its whole thing is that it mostly takes perl functions you already know and love and swaps them out for non-blocking counterparts. so your coroutines don't all just sit there frozen like π§ while one of them waits on a lookup.
and ngl, a LOT of these functions exist for one reason only: to emulate existing interfaces. and likeβ¦ no matter how bad or limited those interfaces are. the manpage literally says so, bestie. (e.g. no IPv6 support π). we don't make the rules.
also important: this module is an AnyEvent user. so if you wanna know how to fold it into your own programs, go read the AnyEvent documentation. it's giving crossover episode β¨
π« $ipn = Coro::Util::inet_aton $hostname || $ip
this one works almost exactly like its Socket::inet_aton counterpart β same energy, same shape β except it does not block other coroutines. slay.
but heads up: it does not handle multihomed hosts, and it does not do IPv6. if you need that, consider using AnyEvent::Socket::resolve_sockaddr together with the Coro rouse functions instead. periodt.
π« gethostbyname, gethostbyaddr
these work similarly to their Perl counterparts, but they do not block π
ββοΈ. internally they use AnyEvent::Util::inet_aton.
same disclaimer though: no multihomed hosts, no IPv6. instead, considerAnyEvent::Socket::resolve_sockaddrorAnyEvent::DNS::reverse_lookup, used with the Coro rouse functions. iykyk πΈ
π« @result = Coro::Util::fork_eval { ... }, @args
okay THIS one is the main character π. it takes the code block or code reference you give it, plus the arguments you give it, and executes them in a separate process, then returns the results.
- the return values must be serialisable with
Coro::Storable. no funny business. - and yeah, it may of course block. that's kinda the point.
β οΈ note that using event handling in the sub is not usually a good idea, because you'll inherit a mixed set of watchers from the parent. messy. π
good news: exceptions get correctly forwarded to the caller, so you're not left guessing π«Ά
so when is this useful? when you wanna push cpu-intensive computations into a different process β for example to take advantage of multiple CPU's. it's also useful if you just wanna run some blocking functions (like system()) and genuinely do not care about the overhead enough to go code your own pid watcher etc. valid honestly.
also it might keep a pool of processes in some future version, because fork can be rather slow in large processes. no promises, it's giving "future plans" energy.
and you should ALSO look at AnyEvent::Util::fork_eval, which is newer and more compatible with totally broken Perl implementations such as the one from ActiveState. π
Example: execute some external program (convert an image to rgba raw form) and add a long computation (extract the alpha channel) in a separate process β while making sure that never more than $NUMCPUS processes are being run. π
my $cpulock = new Coro::Semaphore $NUMCPUS;
sub do_it {
my ($path) = @_;
my $guard = $cpulock->guard;
Coro::Util::fork_eval {
open my $fh, "convert -depth 8 \Q$path\E rgba:"
or die "$path: $!";
local $/;
# make my eyes hurt
pack "C*", unpack "(xxxC)*", <$fh>
}
}
my $alphachannel = do_it "/tmp/img.png";
(the # make my eyes hurt comment is in the original and honestly? felt π₯²)
β¨ AUTHOR/SUPPORT/CONTACT β¨
- Marc A. Lehmann
<schmorp@schmorp.de> http://software.schmorp.de/pkg/Coro.html
and that's the whole page bestie, we ate and left no crumbs ππ¦β¨
this was written against perl v5.40.1, dated 2025-03-21, and it's Util(3pm) in the Coro collection β so if you're running the version in Coro, you're exactly where you need to be π
take care of your coroutines, hydrate, and don't block the event loop ππ
π¦ where this page comes from
- debian trixie: libcoro-perl 6.570-4 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libcoro-perl 6.570-4 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms