NAME

WHAT IS REFLEX?

Reflex is a reactive programming library for Perl. It provides the basic (and some advanced) building blocks for event driven modules and programs.

Reflex uses Moose, a widely accepted standard for OO Perl, rather than concocting its own roles and classes.

Reflex was designed to unify many forms of reactive programming. It currently supports all of these, in the same program if necessary:

plain old callbacks, using coderefs or methods

promises, which are used inline in an imperative fashion

role-based composition of complex reactive classes

class-based composition using inheritance.

WHY IS REFLEX?

In a nutshell, there is more than one reactive programming model, and the use of one often precludes the use of others. This is sad because one model doesn't fit all use cases. Each has its strengths and weaknesses, and it should be possible to use any or all of them as needed.

WRITING REFLEX MODULES

Reflex's multi-model magic arises from a particular design convention. Moose roles are the basic building block of Reflex program. Classes (even the simple ones) are built from these roles. Classes may then be combined by inheritance (is-a composition) or containment (has-a with callbacks).

That's a mouthful, but the entire process is short and to the point. Most of the work is done in the roles, and the basic classes mainly wire roles together.

Writing A Reactive Role

We'll start with a simple role that provides a synchronous callback. This is a bit of a cheat. Callbacks are usually asynchronous. These synchronous ones let us cover callbacks first, by themselves.

Most Reflex roles are parameterized, which allows a single class to consume more than one instance of a particular role. One parameter acts as a primary key, or name for the role. Callbacks and methods are named after this key by default.

That's a bit of work, but roles are generally the biggest Reflex modules. The classes that use it are much smaller.

Creating Classes with Reflex Roles

Reflex roles aren't usable until they become part of a class. The simplest classes perform exactly what their roles do, but in a usable way. Here's AfterAwhileClass, which converts AfterAwileRole into a usable class.

All Reflex classes should be based on Reflex::Base. The class exposes "name" and "awhile" attributes so they may be set during object construction.

Subclassing

Reflex classes are plain Moose classes, which are generally plain Perl classes. Here's a subclass of AfterAwhileClass that internally consumes its on_done() callback. Even though Moose would makethis a bit shorter, we deliberately avoid it because we can.

Objects With Method Callbacks

Method callbacks are virtually identical to coderef callbacks. The anonymous subroutine is replaced with a cb_method() call. This example is substantially longer, however, because a dummy object needs to be created to handle the callback.

As mentioned earlier, most of the work is done in the role itself. This AsyncAwhileRole replaces the blocking sleep() with a non-blocking Reflex::Interval object. Later we'll introduce magic to make it more concise.