| 1 | 3 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 2 | 3 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 3 | |||||||||||||
| 4 | package POE::Declarative::Mixin; | ||||||||||||
| 5 | 3 | ▶ | BEGIN { | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 3 | $POE::Declarative::Mixin::VERSION = '0.09'; | |||||||||||
| 7 | } | ||||||||||||
| 8 | |||||||||||||
| 9 | 3 | ▶ | use POE::Declarative (); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 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<import>, you'll probably want to see L</export_poe_declarative_to_level>. | ||||||||||||
| 71 | |||||||||||||
| 72 | =cut | ||||||||||||
| 73 | |||||||||||||
| 74 | 4 | ▶ | sub import { | ||||||||||
|
Subroutine
import: called CC=1 SCAR=0.0
| |||||||||||||
| 75 | 4 | my $class = shift; | |||||||||||
| 76 | |||||||||||||
| 77 | 4 | $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</import> method, you probably should ignore this method. | ||||||||||||
| 93 | |||||||||||||
| 94 | =cut | ||||||||||||
| 95 | |||||||||||||
| 96 | 5 | ▶ | sub export_poe_declarative_to_level { | ||||||||||
|
Subroutine
export_poe_declarative_to_level: called CC=4 SCAR=15.6
Pod
documented
| |||||||||||||
| 97 | 5 | my $class = shift; | |||||||||||
| 98 | 5 | ▶ | my $level = shift || 1; | ||||||||||
|
Condition50%1/2
shift() || 1
MC/DC0%0/1
shift() || 1
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||
| 99 | 5 | my $package = caller($level); | |||||||||||
| 100 | |||||||||||||
| 101 | 5 | my $states = POE::Declarative::_states($class); | |||||||||||
| 102 | 5 | my $handlers = POE::Declarative::_handlers($class); | |||||||||||
| 103 | |||||||||||||
| 104 | 5 | for my $state (keys %$states) { | |||||||||||
| 105 | 0 | for my $code (@{ $handlers->{ $state }{ $class } }) { | |||||||||||
| 106 | 0 | POE::Declarative::_declare_method($package, $state, $code); | |||||||||||
| 107 | } | ||||||||||||
| 108 | } | ||||||||||||
| 109 | } | ||||||||||||
| 110 | |||||||||||||
| 111 | =head1 SEE ALSO | ||||||||||||
| 112 | |||||||||||||
| 113 | L<POE::Declarative> | ||||||||||||
| 114 | |||||||||||||
| 115 | =head1 AUTHORS | ||||||||||||
| 116 | |||||||||||||
| 117 | Andrew Sterling Hanenkamp C<< <hanenkamp@cpan.org> >> | ||||||||||||
| 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 | 3 | 1; | |||||||||||