July 2024 Archives

Using Coro and AnyEvent Interactively

Problem

I have not been able to figure out how to run an async thread in the background while using a REPL like reply. The moment I run the main loop, it takes over the input from the REPL. Here's what a typical failed REPL session might look like.

❯ rlwrap reply
use AnyEvent
use Coro
use Coro::AnyEvent
my ($i, $loop)
$res[0] = [
undef,
undef
]

$i = 0
$res[1] = 0

$loop = async { while ($i < 10) { say $i++; sleep 1 } }
$res[2] = bless( {}, 'Coro' )

AE::cv->recv
0
1
2
3
4
5
6
7
8
9
^C

I don't know how to get back to the REPL, so I hit ^C but that exits the REPL.

Solution

A workaround I discovered is vividsnow/perl-live which is a combination of perl + elisp that provides an interactive Perl environment that is running an event loop. However, instead of a REPL, you get an emacs buffer in cperl-mode from where you can send lines of perl to a running perl process.

It's not quite what I was looking for, but I can work with this. It does solve the problem of letting me interact with async threads in a running perl process.

Demo

perl live coding video

About gg

user-pic I blog about Perl.