blib/lib/POE/Session/PlainCall.pm

filter: Statementstmt 86.8% 139 / 160 Branchbran 62.9% 39 / 62 Conditioncond 63.1% 12 / 19 MC/DCmcdc 33.3% 4 / 12 Subroutinesub 94.4% 34 / 36 Podpod 100% 4 / 4 totaltotal 79.1% 232 / 293 CC 40 SCAR 18.7
CC
40
Cov
79%
_invoke_state
19.9
create
9.2
__process_args
7.0
CRAP 6.5
↑ Summary
1 package POE::Session::PlainCall;
2  
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use POE;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 6 require Exporter;
11  
12 6 our $VERSION = '0.0301';
13  
14  
15 #######################################
16 6 our $POE_HOLDER;
17 6 $POE_HOLDER = bless [], 'POE::Session::PlainCall::Holder';
18  
19 131 ▶sub poe () { $POE_HOLDER }
Subroutine poe: called CC=1 SCAR=0.0
Pod documented
20  
21 #######################################
22 6 ▶use base qw( Exporter POE::Session );
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 6 our %EXPORT_TAGS = ( 'all' => [ qw( $poe_kernel poe ) ] );
25  
26 6 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
27  
28 6 our @EXPORT = qw( $poe_kernel poe );
29  
30 #######################################
31 6 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.1
32 6 *SE_NAMESPACE = \&POE::Session::SE_NAMESPACE;
33 6 *SE_OPTIONS = \&POE::Session::SE_OPTIONS;
34 6 *SE_STATES = \&POE::Session::SE_STATES;
35 6 ▶ if( POE::Session->can( 'SE_ID' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ('POE::Session'->can('SE_ID')) { } 6 0 6
36 # POE 1.300 +
37 6 *SE_ID = \&POE::Session::SE_ID;
38 6 eval '
39 *SE_RUNNING = sub () { POE::Session::SE_ID+1 };
40 *SE_MYSTATES = sub () { POE::Session::SE_ID+2 };
41 ';
42 }
43 else {
44 # POE 1.299-
45 0 *SE_RUNNING = sub () { POE::Session::SE_STATES+1 };
46 0 *SE_MYSTATES = sub () { POE::Session::SE_STATES+2 };
47 }
48  
49 6 *EN_SIGNAL = \&POE::Session::EN_SIGNAL;
50 6 *EN_DEFAULT = \&POE::Session::EN_DEFAULT;
51  
52 6 *OPT_TRACE = \&POE::Session::OPT_TRACE;
53 6 *OPT_DEBUG = \&POE::Session::OPT_DEBUG;
54 6 *OPT_DEFAULT = \&POE::Session::OPT_DEFAULT;
55 }
56  
57 ############################################################################
58 sub _loggable
59 0 ▶{
Subroutine _loggable: not called CC=2 SCAR=17.9
60 0 my( $self ) = @_;
61 0 ▶ return $self unless $self->[SE_RUNNING];
Branch: 1 branch
BranchTrueFalseTotal
unless $self->[4] 0 0 0
62 0 $POE::Kernel::poe_kernel->_data_alias_loggable( $_[0] );
63 }
64  
65 6 our %OURS;
66 sub instantiate
67 6 ▶{
Subroutine instantiate: called CC=3 SCAR=12.7
Pod documented
68 6 my $self = shift->SUPER::instantiate( @_ );
69 6 ▶ if( $self->[SE_RUNNING] or $self->[SE_MYSTATES] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[4] or $self->[5]) 0 6 6
Condition33%1/3
$self->[4] or $self->[5]
l!l&&r!l&&!r
006
MC/DC0%0/2
$self->[4] or $self->[5]
$self->[4] $self->[5]
Truth table33%1/3
A or B
A= $self->[4]B= $self->[5]
ABresult
1X1
011
000
70 0 die "Definition of POE::Session changed! $self needs to be modified.\n";
71 }
72 6 $self->__init;
73 6 return $self;
74 }
75  
76 sub __init
77 6 ▶{
Subroutine __init: called CC=2 SCAR=7.0
78 6 my( $self ) = @_;
79 6 $self->[SE_RUNNING] = 0;
80 # warn "keys = ", join ', ', keys %OURS;
81 6 $self->[SE_MYSTATES] = {%OURS};
82 6 ▶ $self->SUPER::__init if $self->can( 'SUPER::__init' );
Branch: 1 branch
BranchTrueFalseTotal
if $self->can("SUPER::__init") 0 6 6
83 }
84  
85 #######################################
86 sub create
87 6 ▶{
Subroutine create: called CC=9 SCAR=22.2
Pod documented
88 6 my( $package, @params ) = @_;
89  
90 6 ▶ if (@params & 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@params & 1) 0 6 6
91 0 croak "odd number of events/handlers (missing one or the other?)";
92 }
93 6 my %args = @params;
94  
95 6 my $obj = delete $args{object};
96 6 ▶ unless( $obj ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($obj) 6 0 6
97 6 my $package = delete $args{package};
98 6 ▶ if( $package ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($package) 5 1 6
99 5 my $args = delete $args{ctor_args};
100 5 ▶ $args ||= [];
Condition100%2/2
$args ||= []
l!l
32
MC/DC100%1/1
$args ||= []
$args
Truth table100%2/2
A
A= $args
Aresult
11
00
101 5 $obj = $package->new( @$args );
102 }
103 }
104  
105 6 ▶ if( $obj ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj) 5 1 6
106 my $events = delete $args{events} ||
107 5 ▶ delete $args{states};
Condition66%2/3
delete $args{'events'} || delete $args{'states'}
l!l&&r!l&&!r
230
MC/DC0%0/2
delete $args{'events'} || delete $args{'states'}
delete $args{'events'} delete $args{'states'}
Truth table66%2/3
A || B
A= delete $args{'events'}B= delete $args{'states'}
ABresult
1X1
011
000
108 5 ▶ croak "Parameters 'events' or 'states' are required if you supply 'object' or 'package'"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $events 0 5 5
109 unless defined $events;
110 5 ▶ $args{object_states} ||= [];
Condition100%2/2
$args{"object_states"} ||= []
l!l
14
MC/DC100%1/1
$args{"object_states"} ||= []
$args{"object_states"}
Truth table100%2/2
A
A= $args{"object_states"}
Aresult
11
00
111 5 push @{ $args{object_states} }, $obj, $events;
112 }
113  
114 6 local %OURS;
115 6 $package->__process_args( \%args );
116 # warn "keys = ", join ', ', keys %OURS;
117  
118 6 my $self = $package->SUPER::create( %args );
119 6 $self->[SE_RUNNING] = 1;
120 6 return $self;
121 }
122  
123 #######################################
124 ## Find all state names and mark them as ours
125 sub __process_args
126 6 ▶{
Subroutine __process_args: called CC=7 SCAR=19.5
127 6 my( $package, $args ) = @_;
128 6 foreach my $f ( qw( package_states object_states ) ) {
129 12 ▶ next unless $args->{$f};
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{$f} 5 7 12
130 7 my $L = $args->{$f};
131 7 ▶ for(my $off = 1; $off <= $#$L ; $off+=2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($off <= $L->$#*) 8 7 15
132 8 my $states = $args->{$f}[$off];
133 8 ▶ if( 'HASH' eq ref $states ) {
Branch: 2 branches
BranchTrueFalseTotal
if ('HASH' eq ref $states) { } 4 4 8
elsif ('ARRAY' eq ref $states) { } 4 0 4
134 4 $package->__process_ours( keys %$states );
135 }
136 elsif( 'ARRAY' eq ref $states ) {
137 4 $package->__process_ours( @$states );
138 }
139 }
140 }
141 6 ▶ return unless $args->{inline_states};
Branch: 1 branch
BranchTrueFalseTotal
unless $args->{"inline_states"} 5 1 6
142 1 $package->__process_ours( keys %{ $args->{inline_states} } );
143 }
144  
145 #######################################
146 sub __process_ours
147 9 ▶{
Subroutine __process_ours: called CC=1 SCAR=0.0
148 9 my( $package, @list ) = @_;
149 # warn "OURS=", join ', ', @list;
150 9 @OURS{ @list } = (1) x (0+@list);
151 }
152  
153 #######################################
154 sub _invoke_state
155 36 ▶{
Subroutine _invoke_state: called CC=12 SCAR=29.9
156 36 my( $self, $source_session, $state, $etc, $file, $line, $fromstate ) = @_;
157  
158 36 ▶ unless( $self->[SE_MYSTATES]{$state} or $self->[SE_MYSTATES]{+EN_DEFAULT} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->[5]{$state} or $self->[5]{"_default"}) 4 32 36
Condition100%3/3
$self->[5]{$state} or $self->[5]{"_default"}
l!l&&r!l&&!r
3024
MC/DC100%2/2
$self->[5]{$state} or $self->[5]{"_default"}
$self->[5]{$state} $self->[5]{"_default"}
Truth table100%3/3
A or B
A= $self->[5]{$state}B= $self->[5]{"_default"}
ABresult
1X1
011
000
159 4 ▶ if ($self->[SE_OPTIONS]->{+OPT_TRACE}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[1]{"trace"}) 0 4 4
160 0 POE::Kernel::_warn( $self->_loggable,
161 " -> $state (external event)\n"
162 );
163 }
164 ## Skip out if it's not our state
165 4 return shift->SUPER::_invoke_state( @_ );
166 }
167  
168 # Most of the following was lifted AS IS from POE::Session::_invoke_state
169 # and simply reformated because I'm a tad anal.
170 # But then, POE::Session overloading is like that...
171
172 # Trace the state invocation if tracing is enabled.
173  
174 32 ▶ if ($self->[SE_OPTIONS]->{+OPT_TRACE}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[1]{"trace"}) 0 32 32
175 0 POE::Kernel::_warn( $self->_loggable,
176 " -> $state (from $file at $line)\n"
177 );
178 }
179  
180 # The desired destination state doesn't exist in this session.
181 # Attempt to redirect the state transition to _default.
182  
183 32 ▶ unless (exists $self->[SE_STATES]->{$state}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->[2]{$state}) 2 30 32
184  
185 # There's no _default either; redirection's not happening today.
186 # Drop the state transition event on the floor, and optionally
187 # make some noise about it.
188  
189 2 ▶ unless (exists $self->[SE_STATES]->{+EN_DEFAULT}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->[2]{"_default"}) 0 2 2
190 0 ▶ $! = exists &Errno::ENOSYS ? &Errno::ENOSYS : &Errno::EIO;
Branch: 1 branch
BranchTrueFalseTotal
exists &Errno::ENOSYS ? : 0 0 0
191 0 ▶ if ($self->[SE_OPTIONS]->{+OPT_DEFAULT} and $state ne EN_SIGNAL) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[1]{"default"} and $state ne "_signal") 0 0 0
Condition0%0/3
$self->[1]{"default"} and $state ne "_signal"
!ll&&!rl&&r
000
MC/DC0%0/2
$self->[1]{"default"} and $state ne "_signal"
$self->[1]{"default"} $state ne "_signal"
Truth table0%0/3
A and B
A= $self->[1]{"default"}B= $state ne "_signal"
ABresult
0X0
100
111
192 0 my $loggable_self = $self->_loggable;
193 0 POE::Kernel::_warn(
194 "a '$state' event was sent from $file at $line to $loggable_self ",
195 "but $loggable_self has neither a handler for it ",
196 "nor one for _default\n"
197 );
198 }
199 0 return undef;
200 }
201  
202 # If we get this far, then there's a _default state to redirect
203 # the transition to. Trace the redirection.
204  
205 2 ▶ if ($self->[SE_OPTIONS]->{+OPT_TRACE}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[1]{"trace"}) 0 2 2
206 0 POE::Kernel::_warn( $self->_loggable,
207 " -> $state redirected to _default\n"
208 );
209 }
210  
211 # Transmogrify the original state transition into a corresponding
212 # _default invocation. ARG1 is copied from $etc so it can't be
213 # altered from a distance.
214  
215 2 $etc = [ $state, [@$etc] ];
216 2 $state = EN_DEFAULT;
217 }
218  
219 #####
220 ## The following is unique/specific to POE::Session::PlainCall
221  
222 # If we get this far, then the state can be invoked. So invoke it
223 # already!
224  
225 # Inline states are invoked this way.
226  
227 32 my $handler = $self->[SE_STATES]->{$state};
228 32 ▶ if( ref $handler eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $handler eq "CODE") 1 31 32
229 1 local $POE_HOLDER = bless [
230 ( undef, # object
231 $self, # session
232 $POE::Kernel::poe_kernel, # kernel
233 $self->[SE_NAMESPACE], # heap
234 $state, # state
235 $source_session, # sender
236 undef, # unused #6
237 $file, # caller file name
238 $line, # caller file line
239 $fromstate, # caller state
240 undef, # method
241 $etc # args
242 ) ], 'POE::Session::PlainCall::Holder';
243 1 return $handler->( @$etc );
244 }
245  
246 # Package and object states are invoked this way.
247 31 my ($object, $method) = @{$handler};
248 31 local $POE_HOLDER = bless [
249 ( $object, # object
250 $self, # session
251 $POE::Kernel::poe_kernel, # kernel
252 $self->[SE_NAMESPACE], # heap
253 $state, # state
254 $source_session, # sender
255 undef, # unused #6
256 $file, # caller file name
257 $line, # caller file line
258 $fromstate, # caller state
259 $method, # method
260 $etc, # args
261 ) ], 'POE::Session::PlainCall::Holder';
262  
263 31 return $object->$method( @$etc );
264  
265 }
266  
267 #######################################
268 sub _register_state
269 34 ▶{
Subroutine _register_state: called CC=4 SCAR=14.8
270 34 my( $self, $name, @definition ) = @_;
271 34 ▶ if( $self->[SE_MYSTATES]{$name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[5]{$name}) { } 28 6 34
272 28 return $self->state( $name, @definition );
273 }
274 else {
275 # states created via POE::Kernel->state
276 6 ▶ if( @definition ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@definition) { } 6 0 6
277 6 ▶ if ($self->[SE_OPTIONS]->{+OPT_TRACE}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[1]{"trace"}) 0 6 6
278 0 POE::Kernel::_warn( $self->_loggable, " -> $name is external\n" );
279 }
280 }
281 else {
282 # just in case something strange happened
283 0 delete $self->[SE_MYSTATES]{$name};
284 }
285 6 $self->SUPER::_register_state( $name, @definition );
286 }
287 }
288  
289 #######################################
290 sub state
291 30 ▶{
Subroutine state: called CC=5 SCAR=18.7
Pod documented
292 30 my( $self, $name, @definition ) = @_;
293 30 ▶ if( ! $self->[SE_MYSTATES]{$name} and
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->[5]{$name} and $self->[2]{$name}) 0 30 30
Condition66%2/3
not $self->[5]{$name} and $self->[2]{$name}
!ll&&!rl&&r
2910
MC/DC0%0/2
not $self->[5]{$name} and $self->[2]{$name}
not $self->[5]{$name} $self->[2]{$name}
Truth table66%2/3
not(A) and B
A= not $self->[5]{$name}B= $self->[2]{$name}
ABresult
0X0
100
111
294 $self->[SE_STATES]{$name} ) {
295 0 croak "You may not redefine an event handler defined by a wheel or other external module.";
296 }
297  
298 30 ▶ if( @definition ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@definition) { } 30 0 30
299 30 ▶ if ($self->[SE_OPTIONS]->{+OPT_TRACE}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[1]{"trace"}) 0 30 30
300 0 POE::Kernel::_warn( $self->_loggable, " -> $name is ours\n" );
301 }
302 30 $self->[SE_MYSTATES]{$name} = 1;
303 }
304 else {
305 0 delete $self->[SE_MYSTATES]{$name};
306 }
307 30 $self->SUPER::_register_state( $name, @definition );
308 }
309  
310 ############################################################################
311 package POE::Session::PlainCall::Holder;
312  
313 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
314 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
315  
316 # use POE::Session;
317 6 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
318  
319 12 ▶sub METHOD { POE::Session::ARG0 }
Subroutine METHOD: called CC=1 SCAR=0.0
320 2 ▶sub ETC { POE::Session::ARG1 }
Subroutine ETC: called CC=1 SCAR=0.0
321  
322 1 ▶sub object { $_[0]->[POE::Session::OBJECT] }
Subroutine object: called CC=1 SCAR=0.0
323 1 ▶sub package { my $r=$_[0]->[POE::Session::OBJECT]; blessed $r ? ref $r : $r }
Subroutine package: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($r) ? : 0 1 1
324 3 ▶sub session { $_[0]->[POE::Session::SESSION] }
Subroutine session: called CC=1 SCAR=0.0
325 37 ▶sub kernel { $POE::Kernel::poe_kernel }
Subroutine kernel: called CC=1 SCAR=0.0
326 0 ▶sub heap { $_[0]->[POE::Session::HEAP] }
Subroutine heap: not called CC=1 SCAR=6.9
327 14 ▶sub state { $_[0]->[POE::Session::STATE] }
Subroutine state: called CC=1 SCAR=0.0
328 2 ▶sub event { $_[0]->[POE::Session::STATE] }
Subroutine event: called CC=1 SCAR=0.0
329 12 ▶sub method { $_[0]->[METHOD] }
Subroutine method: called CC=1 SCAR=0.0
330 18 ▶sub sender { $_[0]->[POE::Session::SENDER]->ID }
Subroutine sender: called CC=1 SCAR=0.0
331 10 ▶sub SENDER () { $_[0]->[POE::Session::SENDER] }
Subroutine SENDER: called CC=1 SCAR=0.0
332 20 ▶sub caller_file { $_[0]->[POE::Session::CALLER_FILE] }
Subroutine caller_file: called CC=1 SCAR=0.0
333 10 ▶sub caller_line { $_[0]->[POE::Session::CALLER_LINE] }
Subroutine caller_line: called CC=1 SCAR=0.0
334 1 ▶sub caller_state { $_[0]->[POE::Session::CALLER_STATE] }
Subroutine caller_state: called CC=1 SCAR=0.0
335 2 ▶sub args {
Subroutine args: called CC=2 SCAR=6.9
336 2 my $etc = $_[0]->[ETC];
337 2 ▶ return wantarray ? @$etc : [ @$etc ]
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 1 2
338 }
339  
340  
341 # These would mess up ->caller_file and ->caller_line :-/
342 # sub post { shift->[KERNEL]->post( @_ ) }
343 # sub call { shift->[KERNEL]->call( @_ ) }
344 # sub yield { shift->[KERNEL]->yield( @_ ) }
345  
346  
347 6 1;
348 __END__
↑ Summary