File Coverage

blib/lib/POE/Declarative/Mixin.pm
Criterion Covered Total %
statement 18 21 85.7
branch n/a
condition 1 2 50.0
subroutine 6 6 100.0
pod 1 1 100.0
total 26 30 86.6


line stmt bran cond sub pod time code
1 3     3   7658 use strict;
  3         7  
  3         98  
2 3     3   16 use warnings;
  3         6  
  3         126  
3              
4             package POE::Declarative::Mixin;
5             BEGIN {
6 3     3   49 $POE::Declarative::Mixin::VERSION = '0.09';
7             }
8              
9 3     3   17 use POE::Declarative ();
  3         4  
  3         584  
10              
11             =head1 NAME
12              
13             POE::Declarative::Mixin - use different declarative POE packages together
14              
15             =head1 VERSION
16              
17             version 0.09
18              
19             =head1 SYNOPSIS
20              
21             This is a really poor producer/consumer example, but it shows how the states of each mixin get pulled into the second class.
22              
23             package Producer;
24             use base qw/ POE::Declarative::Mixin /;
25              
26             use POE;
27             use POE::Declarative;
28              
29             on produce => run {
30             push @{ get(HEAP)->{store} }, get ARG0;
31             };
32              
33             package Consumer;
34             use base qw/ POE::Declarative::Mixin /;
35              
36             use POE;
37             use POE::Declarative;
38              
39             on consume => run {
40             print "Consuming ", shift @{ get(HEAP)->{store} }, "\n";
41              
42             yield 'consume' if scalar @{ get(HEAP)->{store} };
43             };
44              
45             package ProducerConsumer;
46              
47             use POE;
48             use POE::Declarative;
49              
50             # Our mixins
51             use Consumer;
52             use Producer;
53              
54             on _start => run {
55             for (1 .. 10) {
56             yield produce => $_;
57             }
58              
59             yield 'consume';
60             };
61              
62             =head1 DESCRIPTION
63              
64             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 C<@ISA> for the class.
65              
66             =head1 METHODS
67              
68             =head2 import
69              
70             This provides the basic magic to make this happen. If you are creating a mixin class that needs to further customize C, you'll probably want to see L.
71              
72             =cut
73              
74             sub import {
75 4     4   30 my $class = shift;
76              
77 4         21 $class->export_poe_declarative_to_level;
78             }
79              
80             =head2 export_poe_declarative_to_level LEVEL
81              
82             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:
83              
84             sub import {
85             my $class = shift;
86              
87             # Do other custom import tasks
88              
89             $class->export_poe_declarative_to_level(1);
90             }
91              
92             If you do not need to define a custom L method, you probably should ignore this method.
93              
94             =cut
95              
96             sub export_poe_declarative_to_level {
97 5     5 1 95 my $class = shift;
98 5   50     24 my $level = shift || 1;
99 5         8 my $package = caller($level);
100              
101 5         16 my $states = POE::Declarative::_states($class);
102 5         15 my $handlers = POE::Declarative::_handlers($class);
103              
104 5         2570 for my $state (keys %$states) {
105 0           for my $code (@{ $handlers->{ $state }{ $class } }) {
  0            
106 0           POE::Declarative::_declare_method($package, $state, $code);
107             }
108             }
109             }
110              
111             =head1 SEE ALSO
112              
113             L
114              
115             =head1 AUTHORS
116              
117             Andrew Sterling Hanenkamp C<< >>
118              
119             =head1 COPYRIGHT AND LICENSE
120              
121             Copyright 2007 Boomer Consulting, Inc. All Rights Reserved.
122              
123             This program is free software and may be modified and distributed under the same terms as Perl itself.
124              
125             =cut
126              
127             1;