develooper Front page | perl.perl5.porters | Postings from March 2020

Thoughts in praise of PL_keyword_plugin, and Future::AsyncAwaitbeing core'able

Paul "LeoNerd" Evans
March 5, 2020 19:00
Thoughts in praise of PL_keyword_plugin, and Future::AsyncAwaitbeing core'able
Message ID:
While I'm on the subject of some of my syntax modules and core, I think
now is an interesting time to bring up the subject of

In brief for those not familiar with the concept, this module introduces
two new keywords `async` and `await`, similar to those found in many
other languages (e.g. C#, Python3, JavaScript, Dart) which allow
functions to be suspended and resume at a later time. This allows one to
write much neater code than would otherwise be possible with e.g.

  async sub do_a_thing
     my $first = await do_first_thing();
     my $second = await do_second_thing();

     return combine_things( $first, $second );


Part of one of the discussions at P5H was whether new syntax features
such as these should be developed as CPAN modules, or directly as
in-core experiments.

This raises an interesting question on how, if at all, such a feature
would be created. Looking at the specifics of the Future::AsyncAwait

  *) It depends on the CPAN module `Future` to provide the future-type
     objects on which the `await` keyword operates, and are returned by
     `async sub`s. Future is not a core module.

  *) Future itself is an abstract idea of deferred values, and doesn't
     in itself provide any useful asynchronous semantics in real
     programs. For that, users tend to use IO::Async::Future (as
     provided by IO::Async-based programs), Mojo::Future (similar),
     Future::IO (which may be backed by one of the above), or a whole
     host of other possibles. Neither IO::Async nor Mojo are core

Thus, it would seem to be impossible to provide the async/await
keywords purely in core, without Future. But Future itself is an
abstracted empty shell and not real-world useful without a concrete
event system like IO::Async or Mojo. It would have been a heavy burden
indeed to ask core to adopt (and thus endorse) one of these event
systems into core, purely to justify having a real-world-useful Future,
purely to then support the addition of async/await syntax.

While on the one hand it would be nice to see such example code as

  use feature 'async_await';

  async sub x { ... }

I remain unconvinced in practice how easily such a thing can be ported
to pure core, because of the large amount of additional support
infrastructure, beyond the pure surface-level syntax parsing, would
need to be imported.

It is for this reason I am currently extremely grateful that the
keyword plugin mechanism exists and allows this kind of thing to be
possible. Without it, I highly doubt that the Perl community would have
been able to get this kind of syntax - which now allows it to be listed
alongside C#, Python 3 and JavaScript in having real support for this
kind of future-based asynchronous programming.

Paul "LeoNerd" Evans Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About