Events

An event is a notification that something has happened: a timer expired, an I/O operation finished, a thread sent a message to another thread, or the user pressed Ctrl-C to interrupt program execution.

What all of these events have in common is that they arrive asynchronously. It’s generally not safe to interrupt program flow at an arbitrary point and continue at a different position, so the event is placed in the interpreter’s task queue. The run-loop code regularly checks whether an event needs to be handled. Event handlers may be an internal piece of code or a user-defined event handler subroutine.

Events are still experimental in Parrot, so the implementation and design is subject to change.

Timers

Timer objects are the replacement for Perl 5’s alarm handlers. They are also a significant improvement. Timers can fire once or repeatedly, and multiple timers can run independently. The precision of a timer is limited by the operating system on which Parrot runs, but it is always more fine-grained then a whole second. The final syntax isn’t yet fixed, so please consult the documentation for examples.

Signals

Signal handling is related to events. When Parrot gets a signal it needs to handle from the operating system, it converts that signal into an event and broadcasts it to all running threads. Each thread independently decides if it’s interested in this signal and, if so, how to respond to it.

 newsub P20, .Exception_Handler, _handler set_eh P20 # establish signal handler print ...

Get Perl 6 and Parrot Essentials, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.