POE::Declarative::Mixin - use different declarative POE packages together


POE-Declarative documentation Contained in the POE-Declarative distribution.

Index


Code Index:

NAME

Top

POE::Declarative::Mixin - use different declarative POE packages together

VERSION

Top

version 0.09

SYNOPSIS

Top

This is a really poor producer/consumer example, but it shows how the states of each mixin get pulled into the second class.

  package Producer;
  use base qw/ POE::Declarative::Mixin /;

  use POE;
  use POE::Declarative;

  on produce => run {
      push @{ get(HEAP)->{store} }, get ARG0;
  };

  package Consumer;
  use base qw/ POE::Declarative::Mixin /;

  use POE;
  use POE::Declarative;

  on consume => run {
      print "Consuming ", shift @{ get(HEAP)->{store} }, "\n";

      yield 'consume' if scalar @{ get(HEAP)->{store} };
  };

  package ProducerConsumer;

  use POE;
  use POE::Declarative;

  # Our mixins
  use Consumer;
  use Producer;

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

      yield 'consume';
  };

DESCRIPTION

Top

Mixin classes provide a nice abstraction for joining multiple functions together into a single package. This is similar to multiple inheritance, but doesn't modify @ISA for the class.

METHODS

Top

import

This provides the basic magic to make this happen. If you are creating a mixin class that needs to further customize import, you'll probably want to see export_poe_declarative_to_level.

export_poe_declarative_to_level LEVEL

This exports the states defined in the mixin to the package specified by level. The most common case for use would be in your mixin:

  sub import {
      my $class = shift;

      # Do other custom import tasks

      $class->export_poe_declarative_to_level(1);
  }

If you do not need to define a custom import method, you probably should ignore this method.

SEE ALSO

Top

POE::Declarative

AUTHORS

Top

Andrew Sterling Hanenkamp <hanenkamp@cpan.org>

COPYRIGHT AND LICENSE

Top


POE-Declarative documentation Contained in the POE-Declarative distribution.
use strict;
use warnings;

package POE::Declarative::Mixin;
BEGIN {
  $POE::Declarative::Mixin::VERSION = '0.09';
}

use POE::Declarative ();

sub import {
    my $class = shift;

    $class->export_poe_declarative_to_level;
}

sub export_poe_declarative_to_level {
    my $class   = shift;
    my $level   = shift || 1;
    my $package = caller($level);

    my $states   = POE::Declarative::_states($class);
    my $handlers = POE::Declarative::_handlers($class);

    for my $state (keys %$states) {
        for my $code (@{ $handlers->{ $state }{ $class } }) {
            POE::Declarative::_declare_method($package, $state, $code);
        }
    }
}

1;