The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

POE::Declarative - write POE applications without the mess

SYNOPSIS

  use POE;
  use POE::Declarative;

  on _start => run {
      yield 'count_to_10';
  };

  on count_to_10 => run {
      for ( 1 .. 10 ) {
          yield say => $_;
      }
  };

  on say => run {
      print get(ARG0);
  };

  POE::Declarative->setup;
  POE::Kernel->run;

DESCRIPTION

Taking the lessons learned from writing dispatchers and templates in Jifty and Template::Declare, I've applied the same declarative language to POE. The goal is to make writing a POE application less painful so that I can concentrate on the more important aspects of my programming.

This module is still VERY EXPERIMENTAL. I just wrote it this evening and it needs lots of work.

DECLARATIONS

on STATE => CODE

on [ STATE1, STATE2, ... ] => CODE

Use the on rule to specify what code to run on a given state (or states). The usual way to say this is:

  on _start => run { ... };

But you could also say:

  on _start => sub { ... };

or:

  on _start => run _start_handler;

or:

  on _start => \&_start_handler;

You can also specify multiple states for a single subroutine:

  on [ 'say', 'yell', 'whisper' ] => run { ... };

This has the same behavior as setting the same subroutine for each of these individually.

Each state is also placed as a method within the current package. This method will be prefixed with "_poe_declarative_" to keep it from conflicting with any other methdos you've defined. So, you can define:

  sub _start { ... }

  on _start => \&_start;

This will then result in an additional method named "_poe_declarative__start" being added to your package. These method names are then passed as state handlers to the POE::Session.

run CODE

This is mostly a replacement keyword for sub because:

  on _start => run { ... };

reads better than:

  on _start => sub { ... };

HELPERS

In addition to providing the declarative syntax the system also provides some helpers to shorten up the guts of your POE applications as well.

get INDEX

Rather than doing this (which you can still do inside your handlers):

  my ($kernel, $heap, $session, $flam, $floob, $flib)
      = @_[KERNEL, HEAP, SESSION, ARG0, ARG1, ARG2];

You can use the get subroutine for a short hand, like:

  my $kernel = get KERNEL;
  get(HEAP)->{flubber} = 'doo';

If you don't like get, don't use it. As I said, the code above will run exactly as you're used to if you're used to writing regular POE applications.

call SESSION, STATE, ARGS

This is just a shorthand for "call" in POE::Kernel.

delay STATE, SECONDS, ARGS

This is just a shorthand for "delay" in POE::Kernel.

post SESSION, STATE, ARGS

This is just a shorthand for "post" in POE::Kernel.

yield STATE, ARGS

This is just a shorthand for "yield" in POE::Kernel.

SETUP METHODS

The setup methods setup your session and such and generally get your session read for the POE kernel to do its thing.

setup [ CLASS ]

Typically, this is called via:

  POE::Declarative->setup;

If called within the package defining the session, this should DWIM nicely. However, if you call it from outside the package (for example, you have several session packages that are then each set up from a central loader), you can also run:

  POE::Declarative->setup('MyPOEApp::Component::FlabbyBo');

And finally, the third form is to pass a blessed reference of that class in, which will become the OBJECT argument to all your states (rather than it just being the name of the class).

  my $flabby_bo = MyPOEApp::Component::FlabbyBo->new;
  POE::Declarative->setup($flabby_bo);

TODO

Lots and lots as of this writing. It doesn't handle much of the useful features of POE. I haven't tested it with other components. I haven't tested or made sure this properly supports inheritance.

There's probably more that I haven't thought of yet.

NONSENSE WORDS

My examples in this module feature some of my favorite nonsense words. I've picked up quite a few of them having a small child.

SEE ALSO

POE

AUTHORS

Andrew Sterling Hanenkamp <hanenkamp@cpan.org>

COPYRIGHT AND LICENSE

Copyright 2007 Boomer Consulting, Inc. All Rights Reserved.

This program is free software and may be modified and distributed under the same terms as Perl itself.