Declare/Object.pm

filter: Statementstmt 78.6% 129 / 164 Branchbran 57.1% 32 / 56 Conditioncond 33.3% 5 / 15 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 82.5% 33 / 40 Podpod 100% 19 / 19 totaltotal 72.1% 218 / 302 CC 36 SCAR 15.3
CC
36
Cov
71%
MODIFY_CODE_ATTRIBUTES
10.8
new
8.8
_CALLBACK
7.7
CRAP 4.6
↑ Summary Declare.pm »
1 package POE::Declare::Object;
2  
3 =pod
4  
5 =head1 NAME
6  
7 POE::Declare::Object - Base class for all POE::Declare classes
8  
9 =head1 DESCRIPTION
10  
11 L<POE::Declare::Object> provides the base package that delivers core
12 functionality for all instantiated L<POE::Declare> objects.
13  
14 Functionality and methods defined here are available in all L<POE::Declare>
15 classes.
16  
17 =head1 METHODS
18  
19 =cut
20  
21 5 ▶use 5.008007;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 5 ▶use attributes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
25 5 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
26 5 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
27 5 ▶use Params::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
28 5 ▶use POE;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 5 ▶use POE::Session ();
Subroutine BEGIN: called CC=1 SCAR=0.0
30 5 ▶use POE::Declare ();
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 5 ▶use vars qw{$VERSION};
Subroutine BEGIN: called CC=1 SCAR=0.0
33 5 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
34 5 $VERSION = '0.59';
35 }
36  
37 # Inside-out storage of internal values
38 5 my %ID = ();
39  
40 # Set default attributes
41 5 POE::Declare::declare( Alias => 'Param' );
42  
43  
44  
45  
46  
47 #####################################################################
48 # Attribute Hooks
49  
50 # Only events are supported for now
51 19 ▶sub MODIFY_CODE_ATTRIBUTES {
Subroutine MODIFY_CODE_ATTRIBUTES: called CC=8 SCAR=23.8
52 19 my ($class, $code, $name, @params) = @_;
53  
54 # Can't declare events for classes that are already compiled
55 19 ▶ if ( $POE::Declare::META{$class} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($POE::Declare::META{$class}) 0 19 19
56 0 Carp::croak("Can't declare event for finalized class $class");
57 }
58  
59 # Register an event
60 19 ▶ if ( $name eq 'Event' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "Event") 18 1 19
61 # Add to the coderef event register
62 18 $POE::Declare::EVENT{Scalar::Util::refaddr($code)} = [
63 'POE::Declare::Meta::Event',
64 ];
65 18 return ();
66 }
67  
68 # Register a timeout
69 1 ▶ if ( $name =~ /^Timeout\b/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^Timeout\b/) 1 0 1
70 1 ▶ unless ( $name =~ /^Timeout\((.+)\)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($name =~ /^Timeout\((.+)\)$/) 0 1 1
71 0 Carp::croak("Missing or invalid timeout");
72 }
73 1 my $delay = $1;
74 1 my $variance = 0;
75 1 ▶ if ( defined Params::Util::_STRING($delay) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined &Params::Util::_STRING($delay)) 1 0 1
76 1 ▶ if ( $delay =~ /^(.+?)\+\-(.+?)\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($delay =~ /^(.+?)\+\-(.+?)\z/) 0 1 1
77 0 $delay = $1;
78 0 $variance = $2;
79 }
80 }
81 1 ▶ unless ( Params::Util::_POSINT($delay) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&Params::Util::_POSINT($delay)) 0 1 1
82 0 Carp::croak("Missing or invalid timeout");
83 }
84 1 $POE::Declare::EVENT{Scalar::Util::refaddr($code)} = [
85 'POE::Declare::Meta::Timeout',
86 delay => $delay,
87 ];
88 1 return ();
89 }
90  
91 # Unknown method type
92 0 Carp::croak("Unknown or unsupported attribute $name");
93 }
94  
95 =pod
96  
97 =head2 meta
98  
99 The C<meta> method can be run on either a class or instances of that class,
100 and returns the L<POE::Declare::Meta> metadata object for that class.
101  
102 =cut
103  
104 # Moved to code generation
105 # sub meta ($) {
106 # POE::Declare::meta( ref $_[0] || $_[0] );
107 # }
108  
109  
110  
111  
112  
113 #####################################################################
114 # Constructor
115  
116 =pod
117  
118 =head2 new
119  
120 # Create an object, but do not spawn it
121 my $object = My::Class->new(
122 Param1 => 'value',
123 Param2 => 'value',
124 );
125  
126 The C<new> constructor is used to create a L<POE::Declare> component
127 B<WITHOUT> immediately starting it up.
128  
129 This is typically assemble to build heirachies of interlinked
130 components and services, without the need to start all of them
131 simultaneously.
132  
133 Instead, a startup routine in the top object of the heirachy can
134 undertake a controlled startup process, bootstrapping each piece of
135 the overall application.
136  
137 All constructors take a series of named params and return a new instance,
138 or throw an exception on error.
139  
140 =cut
141  
142 4 ▶sub new {
Subroutine new: called CC=8 SCAR=21.8
Pod documented
143 4 my $class = shift;
144 4 my $meta = $class->meta;
145 4 my $self = bless { }, $class;
146 4 my %param = @_;
147  
148 # Check the Alias
149 4 ▶ if ( exists $param{Alias} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $param{'Alias'}) { } 0 4 4
150 0 ▶ unless ( Params::Util::_STRING($param{Alias}) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&Params::Util::_STRING($param{"Alias"})) 0 0 0
151 0 Carp::croak("Did not provide a valid Alias param, must be a string");
152 }
153 0 $self->{Alias} = delete $param{Alias};
154 } else {
155 4 $self->{Alias} = $meta->next_alias;
156 }
157  
158 # Check and default params
159 4 foreach ( $meta->_params ) {
160 11 ▶ next unless exists $param{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $param{$_} 7 4 11
161 4 $self->{$_} = delete $param{$_};
162 }
163  
164 # Check for unsupported params
165 4 ▶ if ( %param ) {
Branch: 1 branch
BranchTrueFalseTotal
if (%param) 1 3 4
166 1 my $names = join ', ', sort keys %param;
167 1 die("Unknown or unsupported $class param(s) $names");
168 }
169  
170 # Check and normalize message registration
171 3 foreach ( $meta->_messages ) {
172 3 ▶ next unless exists $self->{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{$_} 0 3 3
173 3 $self->{$_} = _CALLBACK($self->{$_});
174 }
175  
176 # Clear out any accidentally set internal values
177 3 delete $ID{Scalar::Util::refaddr($self)};
178  
179 3 $self;
180 }
181  
182 # Check the validity of a provided message handler.
183 3 ▶sub _CALLBACK {
Subroutine _CALLBACK: called CC=6 SCAR=20.5
184 3 my $it = $_[0];
185  
186 # The callback is an anonymous subroutine
187 3 ▶ return $it if Params::Util::_CODE($it);
Branch: 1 branch
BranchTrueFalseTotal
if &Params::Util::_CODE($it) 2 1 3
188  
189 # Otherwise, we also allow a reference to an array,
190 # which contains two identifiers (like foo_bar).
191 # This will be converted to a call to the relevant
192 # POE session.event
193 1 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (&Params::Util::_ARRAY0($it) and scalar @$it == 2 and _ALIAS($it->[0]) and &Params::Util::_IDENTIFIER($it->[1])) 1 0 1
Condition33%3/9
&Params::Util::_ARRAY0($it) and scalar @$it == 2 and _ALIAS($it->[0]) and &Params::Util::_IDENTIFIER($it->[1])
!ll&&!rl&&r
001
&Params::Util::_ARRAY0($it) and scalar @$it == 2 and _ALIAS($it->[0])
!ll&&!rl&&r
001
&Params::Util::_ARRAY0($it) and scalar @$it == 2
!ll&&!rl&&r
001
MC/DC0%0/4
&Params::Util::_ARRAY0($it) and scalar @$it == 2 and _ALIAS($it->[0]) and &Params::Util::_IDENTIFIER($it->[1])
&Params::Util::_ARRAY0($it) scalar @$it == 2 _ALIAS($it->[0]) &Params::Util::_IDENTIFIER($it->[1])
Truth table20%1/5
A and B and C and D
A= &Params::Util::_ARRAY0($it)B= scalar @$it == 2C= _ALIAS($it->[0])D= &Params::Util::_IDENTIFIER($it->[1])
ABCDresult
0XXX0
10XX0
110X0
11100
11111
194 Params::Util::_ARRAY0($it)
195 and
196 scalar(@$it) == 2
197 and
198 _ALIAS($it->[0])
199 and
200 Params::Util::_IDENTIFIER($it->[1])
201 ) {
202 # Create a closure for the call
203 1 my $session = $it->[0];
204 1 my $event = $it->[1];
205 1 ▶ my $closure = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
206 1 $poe_kernel->call( $session, $event, @_ );
207 1 };
208 1 return $closure;
209 }
210  
211 # Otherwise, not valid
212 0 Carp::croak('Invalid message event handler');
213 }
214  
215 # Check the format of an alias
216 1 ▶sub _ALIAS {
Subroutine _ALIAS: called CC=4 SCAR=17.9
217 1 ▶ Params::Util::_IDENTIFIER($_[0])
Branch: 2 branches
BranchTrueFalseTotal
&Params::Util::_IDENTIFIER($_[0]) or defined &Params::Util::_STRING($_[0]) && $_[0] =~ /\.\d+$/ ? $_[0] : undef 0 1 1
defined &Params::Util::_STRING($_[0]) && $_[0] =~ /\.\d+$/ ? : 1 0 1
Condition33%1/3
defined &Params::Util::_STRING($_[0]) and $_[0] =~ /\.\d+$/
!ll&&!rl&&r
001
MC/DC0%0/2
defined &Params::Util::_STRING($_[0]) and $_[0] =~ /\.\d+$/
defined &Params::Util::_STRING($_[0]) $_[0] =~ /\.\d+$/
Truth table33%1/3
A and B
A= defined &Params::Util::_STRING($_[0])B= $_[0] =~ /\.\d+$/
ABresult
0X0
100
111
218 or (
219 defined(Params::Util::_STRING($_[0]))
220 and
221 $_[0] =~ /\.\d+$/
222 ) ? $_[0] : undef;
223 }
224  
225 =pod
226  
227 =head2 Alias
228  
229 The C<Alias> method returns the L<POE::Session> alias that will be used with
230 this object instance.
231  
232 These will typically be of the form C<'My::Class.123'> but may be a different
233 value if a custom C<Alias> param has been explicitly passed to the constructor.
234  
235 =cut
236  
237 # This is auto-generated
238 # sub Alias {
239 # $_[0]->{Alias};
240 # }
241  
242 =pod
243  
244 =head2 spawn
245  
246 # Spawn (i.e. startup) an existing object
247 $object->spawn;
248
249 # Create the start the object in one call
250 my $alias = My::Class->spawn(
251 Param1 => 'value',
252 Param2 => 'value',
253 );
254  
255 The C<spawn> method is used to create the L<POE::Session> for this object.
256  
257 It returns the session alias as a convenience, or throws an exception on error.
258  
259 When called on the class instead of an object, it provides a shortcut method
260 for a one-shot construction and spawning of an object, returning the object
261 instead of the session alias.
262  
263 Throws an exception on error.
264  
265 =cut
266  
267 3 ▶sub spawn {
Subroutine spawn: called CC=2 SCAR=8.7
Pod documented
268 # Handle the class context
269 3 ▶ unless ( ref $_[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $_[0]) 0 3 3
270 0 my $class = shift;
271 0 my $self = $class->new( @_ );
272 0 $self->spawn;
273 0 return $self;
274 }
275  
276 # Create the session
277 3 my $self = shift;
278 3 my $meta = $self->meta;
279 3 POE::Session->create(
280 heap => $self,
281 package_states => [
282 $meta->name => [ $meta->_package_states ],
283 ],
284 )->ID;
285  
286 # Return the alias
287 3 $self->Alias;
288 }
289  
290 =pod
291  
292 =head2 spawned
293  
294 The C<spawned> method returns true if the L<POE::Session> for a B<POE::Declare>
295 object has been created, or false if not.
296  
297 =cut
298  
299 2 ▶sub spawned {
Subroutine spawned: called CC=1 SCAR=0.0
Pod documented
300 2 !! $ID{Scalar::Util::refaddr($_[0])};
301  
302 }
303  
304 =pod
305  
306 =head2 session_id
307  
308 The C<session_id> accessor finds and returns the internal L<POE::Session>
309 id for this instance, or C<undef> if the object has not been spawned.
310  
311 =cut
312  
313 2 ▶sub session_id {
Subroutine session_id: called CC=1 SCAR=0.0
Pod documented
314 2 $ID{Scalar::Util::refaddr($_[0])};
315 }
316  
317 =pod
318  
319 =head2 session
320  
321 The C<session> accessor finds and returns the internal L<POE::Session>
322 object for this instance, or C<undef> if the object has not been spawned.
323  
324 =cut
325  
326 4 ▶sub session {
Subroutine session: called CC=3 SCAR=11.1
Pod documented
327 4 ▶ my $id = $ID{Scalar::Util::refaddr($_[0])} or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $id = $ID{builtin::refaddr($_[0])} or return undef 3 1 4
328 3 ▶ $poe_kernel->ID_id_to_session($id) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$POE::Declare::Object::poe_kernel->ID_id_to_session($id) or return undef 3 0 3
329 }
330  
331 =pod
332  
333 =head2 kernel
334  
335 The C<kernel> method is provided as a convenience. It returns the
336 L<POE::Kernel> object that objects of this class will run in.
337  
338 =cut
339  
340 5 ▶use constant kernel => $poe_kernel;
Subroutine BEGIN: called CC=1 SCAR=0.0
341  
342  
343  
344  
345  
346 #####################################################################
347 # POE::Session Wrappers
348  
349 =pod
350  
351 =head2 ID
352  
353 The C<ID> is a wrapper for the equivalent L<POE::Session> method, and
354 returns the id number for the L<POE::Session>.
355  
356 Returns an integer, or C<undef> if the heap object has not spawned.
357  
358 =cut
359  
360 2 ▶sub ID {
Subroutine ID: called CC=1 SCAR=0.0
Pod documented
361 2 $ID{Scalar::Util::refaddr($_[0])};
362 }
363  
364 =pod
365  
366 =head2 postback
367  
368 my $handler = $object->postback(
369 'event_name',
370 $first_param,
371 'second_param',
372 );
373 $handler->( $third_param, $first_param );
374  
375 The C<postback> method is a wrapper for the equivalent L<POE::Session>
376 method, and creates an anonymous subroutine that triggers a C<post> for
377 a named event of the heap object.
378  
379 Returns a C<CODE> reference, or dies if the heap object has not been
380 spawned.
381  
382 =cut
383  
384 1 ▶sub postback {
Subroutine postback: called CC=1 SCAR=0.0
Pod documented
385 1 shift->session->postback(@_);
386 }
387  
388 =pod
389  
390 =head2 callback
391  
392 my $handler = $object->callback(
393 'event_name',
394 $first_param,
395 'second_param',
396 );
397 $handler->( $third_param, $first_param );
398  
399 The C<callback> method is a wrapper for the equivalent L<POE::Session>
400 method, and creates an anonymous subroutine that triggers a C<post> for
401 a named event of the heap object.
402  
403 Please don't confuse this for a method relating to "callback events"
404 mentioned earlier, it is not related to them.
405  
406 Returns a C<CODE> reference, or dies if the heap object has not been
407 spawned.
408  
409 =cut
410  
411 1 ▶sub callback {
Subroutine callback: called CC=1 SCAR=0.0
Pod documented
412 1 shift->session->callback(@_);
413 }
414  
415 =pod
416  
417 =head2 lookback
418  
419 sub create_foo {
420 my $self = shift;
421 my $thing = Other::Class->new(
422 ConnectEvent => $self->lookback('it_connected'),
423 ConnectError => $self->lookback('it_failed'),
424 );
425
426 ...
427 }
428  
429 The C<lookback> method is a safe alias for C< [ $self-E<gt>Alias, 'event_name' ] >.
430  
431 When creating the lookback, the name will be double checked to verify that
432 the handler actually exists and is registered.
433  
434 Returns a reference to an C<ARRAY> containing the heap object's alias and
435 the event name.
436  
437 =cut
438  
439 1 ▶sub lookback {
Subroutine lookback: called CC=4 SCAR=15.8
Pod documented
440 1 my $self = shift;
441 1 my $class = ref($self);
442 1 my $name = Params::Util::_IDENTIFIER($_[0]);
443 1 ▶ unless ( $name ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($name) 0 1 1
444 0 Carp::croak("Invalid identifier name '$_[0]'");
445 }
446  
447 # Does the event exist?
448 1 my $attr = $self->meta->attr($name);
449 1 ▶ unless ( $attr and $attr->isa('POE::Declare::Meta::Event') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($attr and $attr->isa("POE::Declare::Meta::Event")) 0 1 1
Condition33%1/3
$attr and $attr->isa("POE::Declare::Meta::Event")
!ll&&!rl&&r
001
MC/DC0%0/2
$attr and $attr->isa("POE::Declare::Meta::Event")
$attr $attr->isa("POE::Declare::Meta::Event")
Truth table33%1/3
A and B
A= $attrB= $attr->isa("POE::Declare::Meta::Event")
ABresult
0X0
100
111
450 0 Carp::croak("$class does not have the event '$name'");
451 }
452  
453 1 return [ $self->Alias, $name ];
454 }
455  
456  
457  
458  
459  
460 #####################################################################
461 # POE::Kernel Wrappers
462  
463 =pod
464  
465 =head2 post
466  
467 The C<post> method runs a POE kernel C<post> for a named event for the
468 heap object's session.
469  
470 Returns void.
471  
472 =cut
473  
474 3 ▶sub post {
Subroutine post: called CC=1 SCAR=0.0
Pod documented
475 3 $poe_kernel->post( shift->Alias, @_ );
476 }
477  
478 =pod
479  
480 =head2 call
481  
482 The C<call> method runs a POE kernel C<call> for a named event for the
483 heap object's session.
484  
485 Returns as for the particular event handler, but generally returns void.
486  
487 =cut
488  
489 2 ▶sub call {
Subroutine call: called CC=1 SCAR=0.0
Pod documented
490 2 $poe_kernel->call( shift->Alias, @_ );
491 }
492  
493 ### Wrapper for the (new) POE timer API
494  
495 =pod
496  
497 =head2 alarm_set
498  
499 The C<alarm_set> method is equivalent to the L<POE::Kernel> method
500 of the same name, setting an alarm for a named event of the heap object's
501 session.
502  
503 =cut
504  
505 0 ▶sub alarm_set {
Subroutine alarm_set: not called CC=1 SCAR=6.9
Pod documented
506 0 shift;
507 0 $poe_kernel->alarm_set( @_ );
508 }
509  
510 =pod
511  
512 =head2 alarm_adjust
513  
514 The C<alarm_adjust> method is equivalent to the L<POE::Kernel> method
515 of the same name, adjusting an alarm for a named event of the heap
516 object's session.
517  
518 =cut
519  
520 0 ▶sub alarm_adjust {
Subroutine alarm_adjust: not called CC=1 SCAR=6.9
Pod documented
521 0 shift;
522 0 $poe_kernel->alarm_adjust( @_ );
523 }
524  
525 =pod
526  
527 =head2 alarm_clear
528  
529 The C<alarm_clear> method is a convenience method. It takes the name of
530 a hash key for the object, containing a timer id. If the ID is set, it
531 is cleared. If not, the method shortcuts.
532  
533 =cut
534  
535 0 ▶sub alarm_clear {
Subroutine alarm_clear: not called CC=2 SCAR=17.9
Pod documented
536 0 ▶ $_[0]->{$_[1]} or return 1;
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{$_[1]} or return 1 0 0 0
537 0 $_[0]->alarm_remove(delete $_[0]->{$_[1]});
538 }
539  
540 =pod
541  
542 =head2 alarm_remove
543  
544 The C<alarm_remove> method is equivalent to the L<POE::Kernel> method
545 of the same name, removing an alarm for a named event of the heap
546 object's session.
547  
548 =cut
549  
550 0 ▶sub alarm_remove {
Subroutine alarm_remove: not called CC=1 SCAR=6.9
Pod documented
551 0 shift;
552 0 $poe_kernel->alarm_remove( @_ );
553 }
554  
555 =pod
556  
557 =head2 alarm_remove_all
558  
559 The C<alarm_remove_all> method is equivalent to the L<POE::Kernel> method
560 of the same name, removing all alarms for the heap object's session.
561  
562 =cut
563  
564 0 ▶sub alarm_remove_all {
Subroutine alarm_remove_all: not called CC=1 SCAR=6.9
Pod documented
565 0 shift;
566 0 $poe_kernel->alarm_remove_all( @_ );
567 }
568  
569  
570 =pod
571  
572 =head2 delay_set
573  
574 The C<delay_set> method is equivalent to the L<POE::Kernel> method
575 of the same name, setting a delayed alarm for a named event of the
576 heap object's session.
577  
578 =cut
579  
580 0 ▶sub delay_set {
Subroutine delay_set: not called CC=1 SCAR=6.9
Pod documented
581 0 shift;
582 0 $poe_kernel->delay_set( @_ );
583 }
584  
585 =pod
586  
587 =head2 delay_adjust
588  
589 The C<delay_adjust> method is equivalent to the L<POE::Kernel> method
590 of the same name, adjusting a delayed alarm for a named event of the
591 heap object's session.
592  
593 =cut
594  
595 0 ▶sub delay_adjust {
Subroutine delay_adjust: not called CC=1 SCAR=6.9
Pod documented
596 0 shift;
597 0 $poe_kernel->delay_adjust( @_ );
598 }
599  
600  
601  
602  
603  
604 #####################################################################
605 # Events
606  
607 =pod
608  
609 =head1 EVENTS
610  
611 The following POE events are provided for all classes
612  
613 =head2 _start
614  
615 The default C<_start> implementation is used to register the alias for
616 the heap object with the kernel. As such, if you need to do your own
617 tasks in C<_start> you MUST call it first.
618  
619 sub _start {
620 my $self = $_[HEAP];
621 $_[0]->SUPER::_start(@_[1..$#_]);
622
623 # Additional tasks here
624 ...
625 }
626  
627 Please note though that the super call will break @_ in the current
628 subroutine, and so you should not use C<$_[KERNEL]> style expressions
629 after the SUPER call.
630  
631 =cut
632  
633 3 ▶sub _start : Event {
Subroutine _start: called CC=3 SCAR=11.7
634 # Set the session alias in the POE kernel.
635 # Check to see if there is an accidental clash between
636 # this session's desired alias and any existing alias.
637 3 my $alias = $_[HEAP]->Alias;
638 3 ▶ if ( defined $poe_kernel->alias_resolve($alias) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $POE::Declare::Object::poe_kernel->alias_resolve($alias)) 0 3 3
639 0 die("Fatal alias name clash, '$alias' already in use");
640 }
641 3 ▶ if ( $poe_kernel->alias_set($alias) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($POE::Declare::Object::poe_kernel->alias_set($alias)) 0 3 3
642 # Failed to set alias
643 0 die("Failed to set alias '$alias'");
644 }
645  
646 # Register our session id with the session index
647 3 $ID{Scalar::Util::refaddr($_[HEAP])} = $_[SESSION]->ID;
648  
649 # Because POE::Declare maintains its own session start/stop
650 # management, the default POE parent/child feature will just
651 # get in the way.
652 # For each created session, ensure it will never have a parent
653 # in the eyes of the POE::Kernel.
654 SCOPE: {
655 3 local $!;
656 3 $poe_kernel->detach_myself;
657 }
658  
659 3 return;
660 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
661  
662 =pod
663  
664 =head2 _stop
665  
666 The default C<_stop> implementation is used to clean up our resources
667 and aliases in the kernel. As such, if you need to do your own
668 tasks in C<_stop> you should always do them first and then call the
669 SUPER last.
670  
671 sub _stop {
672 my $self = $_[HEAP];
673
674 # Additional tasks here
675 ...
676
677 $_[0]->SUPER::_stop(@_[1..$#_]);
678 }
679  
680 =cut
681  
682 2 ▶sub _stop : Event {
Subroutine _stop: called CC=1 SCAR=0.0
683 2 delete $ID{Scalar::Util::refaddr($_[HEAP])};
684 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
685  
686 =pod
687  
688 =head2 finish
689  
690 The C<finish> method is a convenience provided to simplify the process of
691 shutting down the current object/session.
692  
693 It will automatically clean up as many things as possible from your
694 session, leaving it in a state where the session will shut down as
695 soon as the final outstanding event is processed.
696  
697 Currently, this consists of removing any pending alarms and removing
698 the session alias.
699  
700 =cut
701  
702 3 ▶sub finish {
Subroutine finish: called CC=5 SCAR=16.8
Pod documented
703 3 my $self = shift;
704  
705 # Are we running
706 3 my $alias = $self->Alias;
707 3 my $self_id = $ID{Scalar::Util::refaddr($self)};
708 3 my $session = $poe_kernel->alias_resolve($alias);
709 3 ▶ unless ( $self_id ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self_id) 0 3 3
710 # Trying to finish a session when we aren't even spawned in
711 # POE::Declare terms should be treated strictly.
712 0 Carp::croak("Called 'finish' for $alias on unspawned session");
713 }
714 3 ▶ unless ( $session ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($session) 1 2 3
715 # Show some lenience for now and allow double-finishing of an
716 # active POE session (to allow a class to be sure it has
717 # finished everything if there is any doubt).
718 1 return;
719 }
720  
721 # Check we are in the correct session
722 2 my $current = $poe_kernel->get_active_session;
723 2 my $session_id = $session->ID;
724 2 my $current_id = $current->ID;
725 2 ▶ unless ( $session_id == $current_id ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($session_id == $current_id) 0 2 2
726 0 Carp::croak("Called 'finish' for $alias from a different session");
727 }
728  
729 # Remove all timers
730 2 $poe_kernel->alarm_remove_all;
731  
732 # Remove the session alias.
733 2 ▶ unless ( $session_id == $self_id ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($session_id == $self_id) 0 2 2
734 0 die("Session id mismatch error");
735 }
736  
737 2 $poe_kernel->alias_remove($alias);
738 }
739  
740  
741  
742  
743  
744 #####################################################################
745 # Compile the POE::Declare form of POE::Declare::Object itself
746  
747 5 POE::Declare::compile;
748  
749 =pod
750  
751 =head1 SUPPORT
752  
753 Bugs should be always be reported via the CPAN bug tracker at
754  
755 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=POE-Declare>
756  
757 For other issues, or commercial enhancement or support, contact the author.
758  
759 =head1 AUTHORS
760  
761 Adam Kennedy E<lt>adamk@cpan.orgE<gt>
762  
763 =head1 SEE ALSO
764  
765 L<POE>, L<POE::Declare>, L<http://ali.as/>
766  
767 =head1 COPYRIGHT
768  
769 Copyright 2006 - 2012 Adam Kennedy.
770  
771 This program is free software; you can redistribute
772 it and/or modify it under the same terms as Perl itself.
773  
774 The full text of the license can be found in the
775 LICENSE file included with this module.
776  
777 =cut
↑ Summary Declare.pm »