Advent-based Programming

Beam::Emitter - 2013-12-16

Oh, sorry, seasonal slip of the tongue. I meant Event-based programming. A programming paradigm that is used quite exensively in some languages (hellooo JavaScript), but perhaps slightly less so in Perl. And while we see it used most often for asynchronous tasks — GUIs, server stuff — it also offers advantages for more typical settings.

For example, let's consider a Christmas supper. We can all agree that it's a task that involves a lot of details. If we were to tackle the task the conventional way, we'd create a master class that would organize everything, orchestrate the activities of everybody in the household, and probably go mad before the end of the evening from the sheer complexity of the affair. Not appealing. Alternatively, we could imitate what generations of families have done to get through such an operation: holler across the room each time something happens or is needed, and let everybody concerned in the crowd deal with it.

As you may suspect, many modules exist to deal with event-based programming. Here, I'll use Beam::Emitter, a very light-weight Moo-based system.

As mentioned above, the goal here is to have a very simple core system, and have it broadcast everything that need to be acted upon. So we'll do just that and create our house, which will act like a (warm and cozy) echo chamber:

All the magic of the party will reside in its guests (but then, isn't that always the case?). Each person invited, via $_->register_events is invited to keep their ears open for things that pertain to them. So what we need to have their class implement is that listening, and of course whatever they have to do when the stuff happens.

The system is very flexible. Have some events only some people can act on? No problem, just have those special ones listen to the events. For example, only a bona fide cook should be trusted with taking control of the kitchen:

Need to pass on some additional information with an event, like what type of trimming needs to be attended to? With Beam::Emitter, the events are passed objects, so you can create a a sub-class containing the data you want and it's all good:

1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16:

packageTrimming;

usestrict;usewarnings;

useMoose;

extends'Beam::Event';

has"type"=>(isa=>'Str',is=>'ro',required=>1,);

1;

And with that... believe it or not, you're all set to go. Well, almost. We still have to send our invitations and launch the festivities: