Declarative.pm

filter: Statementstmt 98.1% 104 / 106 Branchbran 75.0% 21 / 28 Conditioncond 55.2% 21 / 38 MC/DCmcdc 20.8% 5 / 24 Subroutinesub 95.8% 23 / 24 Podpod 100% 8 / 8 totaltotal 79.8% 182 / 228 CC 32 SCAR 18.3
CC
32
Cov
85%
on
13.9
_handle_state
9.7
setup
6.3
CRAP 6.2
↑ Summary Declarative/Mixin.pm »
1 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package POE::Declarative;
5 12 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
6 12 $POE::Declarative::VERSION = '0.09';
7 }
8  
9 12 require Exporter;
10 12 our @ISA = qw( Exporter );
11  
12 12 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 12 ▶use POE;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 12 ▶use Scalar::Util qw/ blessed reftype /;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 12 our @EXPORT = qw(
17 call delay post yield
18  
19 get
20  
21 on run
22 );
23  
24 =head1 NAME
25  
26 POE::Declarative - write POE applications without the mess
27  
28 =head1 VERSION
29  
30 version 0.09
31  
32 =head1 SYNOPSIS
33  
34 use POE;
35 use POE::Declarative;
36  
37 on _start => run {
38 yield 'count_to_10';
39 };
40  
41 on count_to_10 => run {
42 for ( 1 .. 10 ) {
43 yield say => $_;
44 }
45 };
46  
47 on say => run {
48 print get(ARG0);
49 };
50  
51 POE::Declarative->setup;
52 POE::Kernel->run;
53  
54 =head1 DESCRIPTION
55  
56 Taking the lessons learned from writing dispatchers and templates in L<Jifty> and L<Template::Declare>, I've applied the same declarative language to L<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.
57  
58 =head1 DECLARATIONS
59  
60 =head2 on STATE => CODE
61  
62 =head2 on [ STATE1, STATE2, ... ] => CODE
63  
64 Use the C<on> rule to specify what code to run on a given state (or states). The usual way to say this is:
65  
66 on _start => run { ... };
67  
68 But you could also say:
69  
70 on _start => sub { ... };
71  
72 or:
73  
74 on _start => run _start_handler;
75  
76 or:
77  
78 on _start => \&_start_handler;
79  
80 =head3 MULTIPLE STATES FOR A SINGLE HANDLER
81  
82 You can also specify multiple states for a single subroutine:
83  
84 on [ 'say', 'yell', 'whisper' ] => run { ... };
85  
86 This has the same behavior as setting the same subroutine for each of these individually.
87  
88 =head3 STATE HANDLER METHODS
89  
90 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:
91  
92 sub _start { ... }
93  
94 on _start => \&_start;
95  
96 This will then result in an additional method named "C<_poe_declarative__start>" being added to your package. These method names are then passed as state handlers to the L<POE::Session>.
97  
98 =head3 MULTIPLE HANDLERS PER STATE
99  
100 You may have multiple handlers for each state with L<POE::Declarative>. If you have two calls to C<on()> for the same state name, both of those handlers will be run when that state is entered by L<POE>. If you are using L<POE::Declarative::Mixin> your mixin classes and your main class may all define a handler for a given state and all handlers will be run.
101  
102 package X;
103 use base qw/ POE::Declarative::Mixin /;
104  
105 use POE::Declarative;
106  
107 on foo => run { print "X" };
108  
109 package Y;
110 use base qw/ POE::Declarative::Mixin /;
111  
112 use POE::Declarative;
113  
114 on foo => run { print "Y" };
115  
116 package Z;
117 use POE::Declarative;
118 use X;
119 use Y;
120  
121 on foo => run { print "Z\n" };
122 on _start => run { yield 'foo' };
123  
124 POE::Declarative->setup;
125 POE::Kernel->run;
126  
127 In the example above, the output could be:
128  
129 XYZ
130  
131 The order multiple handlers will run in is not (as of this writing) completely explicit. However, the primary package's handlers will be run last after all mixins have run. Also, the order the handlers is defined within a package will be preserved. Thus, if you define two handlers for the same state within the same package, the one defined first will be run first and the one defined second will be run second.
132  
133 Because of these, the output from the previous example might also be:
134  
135 YXZ
136  
137 If you use L</call> to synchronously activate a state and use the return value. It will be set to the return value of the last handler run in the main package.
138  
139 =cut
140  
141 54 ▶sub on($$) {
Subroutine on: called CC=12 SCAR=26.4
Pod documented
142 54 my $state = shift;
143 54 my $code = shift;
144  
145 54 ▶ croak qq{"on" expects a code reference as the second argument, }
Branch: 1 branch
BranchTrueFalseTotal
unless ref $code and builtin::reftype($code) eq "CODE" 0 54 54
Condition33%1/3
ref $code and builtin::reftype($code) eq "CODE"
!ll&&!rl&&r
0054
MC/DC0%0/2
ref $code and builtin::reftype($code) eq "CODE"
ref $code builtin::reftype($code) eq "CODE"
Truth table33%1/3
A and B
A= ref $codeB= builtin::reftype($code) eq "CODE"
ABresult
0X0
100
111
146 .qq{found $code instead}
147 unless ref $code and reftype $code eq 'CODE';
148  
149 54 my $session = POE::Kernel->get_active_session;
150 54 my $package;
151  
152 # The kernel is not yet running/not in an active session
153 54 ▶ if ($session->isa('POE::Kernel')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($session->isa('POE::Kernel')) { } 40 14 54
154  
155 # Normally, the caller is good enough
156 40 $package = caller;
157  
158 # DEEP MAGIC!!! BEWARE OF THE DWIMMERY!!!
159 #
160 # However, if we're in a mixin declaring a state using some sort of
161 # fancy helper subroutine, we need to try and put that declared state
162 # into the calling class not in the mixin where it fouls the mixin's
163 # declaration and doesn't make it into the session configuration
164 # properly. See t/dynamic-late-mixin-states.t for an example of the
165 # kind of situation where this comes up.
166 40 my $caller = 1;
167 40 ▶ while (defined $package && $package->isa('POE::Declarative::Mixin')) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $package and $package->isa("POE::Declarative::Mixin")) 9 40 49
Condition66%2/3
defined $package and $package->isa("POE::Declarative::Mixin")
!ll&&!rl&&r
0409
MC/DC50%1/2
defined $package and $package->isa("POE::Declarative::Mixin")
defined $package $package->isa("POE::Declarative::Mixin")
Truth table66%2/3
A and B
A= defined $packageB= $package->isa("POE::Declarative::Mixin")
ABresult
0X0
100
111
168 9 $package = caller($caller++);
169 }
170  
171 # Fallback position in case we get confused
172 40 ▶ $package = caller unless defined $package;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $package 0 40 40
173 }
174  
175 # The POE kernel is running and in a session
176 else {
177 # Try to guess the package from the session if in a POE::Declarative
178 # handler, or fallback to the caller if not, which may be bad. By using
179 # the state's OBJECT, this should magically handle this work in mixins
180 # as well!
181 14 ▶ my $object = get(OBJECT) || caller;
Condition33%1/3
&get(0) || caller
l!l&&r!l&&!r
1400
MC/DC0%0/2
&get(0) || caller
&get(0) caller
Truth table33%1/3
A || B
A= &get(0)B= caller
ABresult
1X1
011
000
182 14 ▶ $package = ref $object || $object;
Condition33%1/3
ref $object || $object
l!l&&r!l&&!r
0140
MC/DC0%0/2
ref $object || $object
ref $object $object
Truth table33%1/3
A || B
A= ref $objectB= $object
ABresult
1X1
011
000
183 }
184  
185 # Using on [ qw/ x y z / ] => ... syntax
186 54 ▶ if (ref $state and reftype $state eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $state and builtin::reftype($state) eq 'ARRAY') { } 1 53 54
Condition66%2/3
ref $state and builtin::reftype($state) eq "ARRAY"
!ll&&!rl&&r
5301
MC/DC50%1/2
ref $state and builtin::reftype($state) eq "ARRAY"
ref $state builtin::reftype($state) eq "ARRAY"
Truth table66%2/3
A and B
A= ref $stateB= builtin::reftype($state) eq "ARRAY"
ABresult
0X0
100
111
187 1 for my $individual_state (@$state) {
188 3 _declare_method($package, $individual_state, $code);
189 }
190 }
191  
192 # Using on x => ... syntax
193 else {
194 53 _declare_method($package, $state, $code);
195 }
196 }
197  
198 56 ▶sub _declare_method {
Subroutine _declare_method: called CC=2 SCAR=6.9
199 56 my $package = shift;
200 56 my $state = shift;
201 56 my $code = shift;
202  
203 56 my $states = _states($package);
204 56 my $handlers = _handlers($package);
205  
206 56 my $method = '_poe_declarative_' . $state;
207 56 $states->{ $state } = $method;
208 56 push @{ $handlers->{ $state }{ $package } }, $code;
209  
210 {
211 12 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
212 12 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
213 56 ▶ *{ $package . '::' . $method } = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
214 172 _args(@_);
215 172 _handle_state(@_);
216 56 };
217 }
218  
219 # Check if the system is running or not
220 56 my $session = POE::Kernel->get_active_session;
221  
222 # We're in an active session, make sure the kernel is updated
223 56 ▶ unless ($session->isa('POE::Kernel')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($session->isa("POE::Kernel")) 14 42 56
224 14 POE::Kernel->state($state, $package, $method);
225 }
226 }
227  
228 172 ▶sub _handle_state {
Subroutine _handle_state: called CC=9 SCAR=22.7
229 172 my $self = $_[OBJECT];
230 172 my $state = $_[STATE];
231 172 ▶ my $package = ref $self || $self;
Condition66%2/3
ref $self || $self
l!l&&r!l&&!r
11710
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table66%2/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
232  
233 172 my $all_handlers = _handlers($package);
234 172 my $my_handlers = $all_handlers->{ $state };
235  
236 172 my ($result, @result);
237 my @handler_packages
238 0 ▶ = sort { $a eq $package ? 1 # put this package at the end
Branch: 2 branches
BranchTrueFalseTotal
$a eq $package ? : 0 0 0
$b eq $package ? : 0 0 0
239 : $b eq $package ? -1
240 : 0 } keys %$my_handlers;
241 172 for my $handler_package (@handler_packages) {
242 172 my $codes = $my_handlers->{ $handler_package };
243  
244 172 for my $code (@$codes) {
245 183 ▶ if (wantarray) {
Branch: 2 branches
BranchTrueFalseTotal
if (wantarray) { } 10 173 183
elsif (defined wantarray) { } 27 146 173
246 10 @result = $code->(@_);
247 }
248 elsif (defined wantarray) {
249 27 $result = $code->(@_);
250 }
251 else {
252 146 $code->(@_);
253 }
254 }
255 }
256  
257 172 ▶ return wantarray ? @result : $result;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 10 162 172
258 }
259  
260 =head2 run CODE
261  
262 This is mostly a replacement keyword for C<sub> because:
263  
264 on _start => run { ... };
265  
266 reads better than:
267  
268 on _start => sub { ... };
269  
270 =cut
271  
272 54 ▶sub run(&) { $_[0] }
Subroutine run: called CC=1 SCAR=0.0
Pod documented
273  
274 =head1 HELPERS
275  
276 In addition to providing the declarative syntax the system also provides some helpers to shorten up the guts of your POE applications as well.
277  
278 =head2 get INDEX
279  
280 Rather than doing this (which you can still do inside your handlers):
281  
282 my ($kernel, $heap, $session, $flam, $floob, $flib)
283 = @_[KERNEL, HEAP, SESSION, ARG0, ARG1, ARG2];
284  
285 You can use the C<get> subroutine for a short hand, like:
286  
287 my $kernel = get KERNEL;
288 get(HEAP)->{flubber} = 'doo';
289  
290 If you don't like C<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.
291  
292 =cut
293  
294 142 ▶sub get($) {
Subroutine get: called CC=1 SCAR=0.0
Pod documented
295 142 my $pos = shift;
296 142 my $package = caller;
297 142 return _args()->[ $pos ];
298 }
299  
300 =head2 call SESSION, STATE, ARGS
301  
302 This is just a shorthand for L<POE::Kernel/call>.
303  
304 =cut
305  
306 21 ▶sub call($$;@) {
Subroutine call: called CC=1 SCAR=0.0
Pod documented
307 21 POE::Kernel->call( @_ );
308 }
309  
310 =head2 delay STATE, SECONDS, ARGS
311  
312 This is just a shorthand for L<POE::Kernel/delay>.
313  
314 =cut
315  
316 12 ▶sub delay($;$@) {
Subroutine delay: called CC=1 SCAR=0.0
Pod documented
317 12 POE::Kernel->delay( @_ );
318 }
319  
320 =head2 post SESSION, STATE, ARGS
321  
322 This is just a shorthand for L<POE::Kernel/post>.
323  
324 =cut
325  
326 0 ▶sub post($$;@) {
Subroutine post: not called CC=1 SCAR=6.9
Pod documented
327 0 POE::Kernel->post( @_ );
328 }
329  
330 =head2 yield STATE, ARGS
331  
332 This is just a shorthand for L<POE::Kernel/yield>.
333  
334 =cut
335  
336 125 ▶sub yield($;@) {
Subroutine yield: called CC=1 SCAR=0.0
Pod documented
337 125 POE::Kernel->yield( @_ );
338 }
339  
340 =head1 SETUP METHODS
341  
342 The setup methods setup your session and such and generally get your session read for the POE kernel to do its thing.
343  
344 =head2 setup [ CLASS [ , HEAP ] ]
345  
346 Typically, this is called via:
347  
348 POE::Declarative->setup;
349  
350 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:
351  
352 POE::Declarative->setup('MyPOEApp::Component::FlabbyBo');
353  
354 And finally, the third form is to pass a blessed reference of that class in, which will become the C<OBJECT> argument to all your states (rather than it just being the name of the class).
355  
356 my $flabby_bo = MyPOEApp::Component::FlabbyBo->new;
357 POE::Declarative->setup($flabby_bo);
358  
359 You may also specify a second argument that will be used to setup the L<POE::Session> heap. If not given, the C<HEAP> argument defaults to an empty hash reference.
360  
361 =cut
362  
363 12 our $_POE_DECLARATIVE_ARGS;
364 314 ▶sub _args {
Subroutine _args: called CC=3 SCAR=11.1
365 314 ▶ $_POE_DECLARATIVE_ARGS = [ @_ ] if scalar(@_) > 0;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ > 0 172 142 314
366 314 ▶ return $_POE_DECLARATIVE_ARGS || [];
Condition50%1/2
$POE::Declarative::_POE_DECLARATIVE_ARGS || []
l!l
3140
MC/DC0%0/1
$POE::Declarative::_POE_DECLARATIVE_ARGS || []
$POE::Declarative::_POE_DECLARATIVE_ARGS
Truth table50%1/2
A
A= $POE::Declarative::_POE_DECLARATIVE_ARGS
Aresult
11
00
367 }
368  
369 233 ▶sub _handlers {
Subroutine _handlers: called CC=3 SCAR=11.2
370 233 ▶ my $package = shift || caller(1);
Condition33%1/3
shift() || caller 1
l!l&&r!l&&!r
23300
MC/DC0%0/2
shift() || caller 1
shift() caller 1
Truth table33%1/3
A || B
A= shift()B= caller 1
ABresult
1X1
011
000
371  
372 12 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
373 233 ▶ return scalar (${ $package . '::_POE_DECLARATIVE_HANDLERS' } ||= {});
Condition100%2/2
${$package . '::_POE_DECLARATIVE_HANDLERS';} ||= {}
l!l
21518
MC/DC100%1/1
${$package . '::_POE_DECLARATIVE_HANDLERS';} ||= {}
${$package . '::_POE_DECLARATIVE_HANDLERS';}
Truth table100%2/2
A
A= ${$package . '::_POE_DECLARATIVE_HANDLERS';}
Aresult
11
00
374 }
375  
376 74 ▶sub _states {
Subroutine _states: called CC=3 SCAR=11.2
377 74 ▶ my $package = shift || caller(1);
Condition33%1/3
shift() || caller 1
l!l&&r!l&&!r
7400
MC/DC0%0/2
shift() || caller 1
shift() caller 1
Truth table33%1/3
A || B
A= shift()B= caller 1
ABresult
1X1
011
000
378  
379 12 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
380 74 ▶ return scalar (${ $package . '::_POE_DECLARATIVE_STATES' } ||= {});
Condition100%2/2
${$package . '::_POE_DECLARATIVE_STATES';} ||= {}
l!l
5618
MC/DC100%1/1
${$package . '::_POE_DECLARATIVE_STATES';} ||= {}
${$package . '::_POE_DECLARATIVE_STATES';}
Truth table100%2/2
A
A= ${$package . '::_POE_DECLARATIVE_STATES';}
Aresult
11
00
381 }
382  
383 13 ▶sub setup {
Subroutine setup: called CC=6 SCAR=18.5
Pod documented
384 13 my $class = shift;
385  
386 13 ▶ unshift @_, $class if defined $class and $class ne __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
if defined $class and $class ne __PACKAGE__ 0 13 13
Condition33%1/3
defined $class and $class ne __PACKAGE__
!ll&&!rl&&r
0130
MC/DC0%0/2
defined $class and $class ne __PACKAGE__
defined $class $class ne __PACKAGE__
Truth table33%1/3
A and B
A= defined $classB= $class ne __PACKAGE__
ABresult
0X0
100
111
387  
388 13 ▶ my $package = shift || caller;
Condition66%2/3
shift() || caller
l!l&&r!l&&!r
580
MC/DC0%0/2
shift() || caller
shift() caller
Truth table66%2/3
A || B
A= shift()B= caller
ABresult
1X1
011
000
389 13 ▶ my $heap = shift || {};
Condition100%2/2
shift() || {}
l!l
112
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
390  
391 # Use object states
392 13 ▶ if (blessed $package) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($package)) { } 1 12 13
393 1 POE::Session->create(
394 object_states => [ $package => _states(blessed $package) ],
395 heap => $heap,
396 );
397 }
398  
399 # Use package states
400 else {
401 12 POE::Session->create(
402 package_states => [ $package => _states($package) ],
403 heap => $heap,
404 );
405 }
406 }
407  
408 =head1 SEE ALSO
409  
410 L<POE>
411  
412 =head1 AUTHORS
413  
414 Andrew Sterling Hanenkamp C<< <hanenkamp@cpan.org> >>
415  
416 =head1 COPYRIGHT AND LICENSE
417  
418 Copyright 2007 Boomer Consulting, Inc. All Rights Reserved.
419  
420 This program is free software and may be modified and distributed under the same terms as Perl itself.
421  
422 =cut
423  
424 12 1;
↑ Summary Declarative/Mixin.pm »