blib/lib/Class/StateMachine.pm

filter: Statementstmt 88.5% 279 / 315 Branchbran 43.5% 54 / 124 Conditioncond 37.1% 13 / 35 MC/DCmcdc 4.7% 1 / 21 Subroutinesub 90.5% 48 / 53 Podpod 100% 2 / 2 totaltotal 72.1% 397 / 550 CC 82 SCAR 31.2
CC
82
Cov
73%
_state
86.5
_debug
20.0
_on_leave_state
20.0
CRAP 22.7
↑ Summary
1 package Class::StateMachine;
2  
3 package Class::StateMachine::Private;
4  
5 13 ▶sub _eval_states {
Subroutine _eval_states: called CC=1 SCAR=0.0
6 # we want the state declarations evaluated inside a clean
7 # environment (lexicaly free):
8 1 ▶ eval $_[0]
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
9 }
10  
11 package Class::StateMachine;
12  
13 1 our $VERSION = '0.24';
14  
15 1 ▶our $debug //= 0;
Condition50%1/2
our $debug //= 0
l!l
01
MC/DC0%0/1
our $debug //= 0
our $debug
Truth table50%1/2
A
A= our $debug
Aresult
11
00
16 1 ▶our $ignore_same_state_changes //= 0;
Condition50%1/2
our $ignore_same_state_changes //= 0
l!l
01
MC/DC0%0/1
our $ignore_same_state_changes //= 0
our $ignore_same_state_changes
Truth table50%1/2
A
A= our $ignore_same_state_changes
Aresult
11
00
17  
18 package Class::StateMachine::Private;
19  
20 1 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶BEGIN { our @CARP_NOT = qw(Class::StateMachine) }
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use mro;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use MRO::Define;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use Hash::Util qw(fieldhash);
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 ▶use Devel::Peek 'CvGV';
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 ▶use Package::Stash;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 1 ▶use Sub::Name;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 1 ▶use Scalar::Util qw(refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 1 fieldhash my %state;
35 1 fieldhash my %state_changed;
36 1 fieldhash my %delayed;
37 1 fieldhash my %delayed_once;
38 1 fieldhash my %on_leave_state;
39  
40 1 my %class_bootstrapped;
41 my %class_state_isa;
42  
43 0 ▶sub _debug {
Subroutine _debug: not called CC=4 SCAR=30.0
44 0 my $self = shift;
45 0 require Time::HiRes;
46 0 ▶ if (length(my $class = Class::StateMachine::ref($self))) {
Branch: 1 branch
BranchTrueFalseTotal
if (length(my $class = Class::StateMachine::ref($self))) { } 0 0 0
47 0 ▶ my $state = $state{$self} // '<undef>';
Condition0%0/2
$state{$self} // "<undef>"
l!l
00
MC/DC0%0/1
$state{$self} // "<undef>"
$state{$self}
Truth table0%0/2
A
A= $state{$self}
Aresult
11
00
48 0 my $addr = refaddr($self);
49 warn sprintf "%08.3f %s[%x/%s%s]> %s\n",
50 0 ▶ Time::HiRes::time(), $class, $addr, $state, ($state_changed{$self} ? '|sc' : ''), "@_";
Branch: 1 branch
BranchTrueFalseTotal
$state_changed{$self} ? : 0 0 0
51 }
52 else {
53 0 warn sprintf "%08.3f %s> %s\n", Time::HiRes::time(), $self, "@_";
54 }
55 }
56  
57 10 ▶sub _state {
Subroutine _state: called CC=28 SCAR=44.6
58 10 my($self, $new_state) = @_;
59 10 ▶ $state{$self} // croak("object $self has no state, " .
Condition50%1/2
$state{$self} // croak("object $self has no state, " . 'use Class::StateMachine::bless to create Class::StateMachine objects')
l!l
100
MC/DC0%0/1
$state{$self} // croak("object $self has no state, " . 'use Class::StateMachine::bless to create Class::StateMachine objects')
$state{$self}
Truth table50%1/2
A
A= $state{$self}
Aresult
11
00
60 "use Class::StateMachine::bless to create Class::StateMachine objects");
61 10 ▶ if (defined $new_state) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $new_state) 9 1 10
62 9 my $old_state = $state{$self};
63 9 ▶ return $old_state if $ignore_same_state_changes and $new_state eq $old_state;
Branch: 1 branch
BranchTrueFalseTotal
if $Class::StateMachine::ignore_same_state_changes and $new_state eq $old_state 0 9 9
Condition33%1/3
$Class::StateMachine::ignore_same_state_changes and $new_state eq $old_state
!ll&&!rl&&r
900
MC/DC0%0/2
$Class::StateMachine::ignore_same_state_changes and $new_state eq $old_state
$Class::StateMachine::ignore_same_state_changes $new_state eq $old_state
Truth table33%1/3
A and B
A= $Class::StateMachine::ignore_same_state_changesB= $new_state eq $old_state
ABresult
0X0
100
111
64 9 ▶ $debug and _debug($self, "changing state from $old_state to $new_state");
Branch: 1 branch
BranchTrueFalseTotal
$Class::StateMachine::debug and _debug($self, "changing state from $old_state to $new_state") 0 9 9
65 9 $state_changed{$self} = 1;
66 9 local $state_changed{$self};
67  
68 9 ▶ if (my $check = $self->can('check_state')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $check = $self->can("check_state")) 0 9 9
69 0 ▶ $debug and _debug($self, "checking state $new_state");
Branch: 1 branch
BranchTrueFalseTotal
$Class::StateMachine::debug and _debug($self, "checking state $new_state") 0 0 0
70 0 ▶ $check->($self, $new_state) or croak qq(invalid state "$new_state");
Branch: 1 branch
BranchTrueFalseTotal
&$check($self, $new_state) or croak(qq[invalid state "$new_state"]) 0 0 0
71 0 ▶ return $state{$self} if $state_changed{$self};
Branch: 1 branch
BranchTrueFalseTotal
if $state_changed{$self} 0 0 0
72 }
73 9 ▶ if (my $leave = $self->can('leave_state')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $leave = $self->can("leave_state")) 9 0 9
74 9 ▶ $debug and _debug($self, "calling leave_state($old_state, $new_state)");
Branch: 1 branch
BranchTrueFalseTotal
$Class::StateMachine::debug and _debug($self, "calling leave_state($old_state, $new_state)") 0 9 9
75 9 $leave->($self, $old_state, $new_state);
76 9 ▶ return $state{$self} if $state_changed{$self};
Branch: 1 branch
BranchTrueFalseTotal
if $state_changed{$self} 0 9 9
77 }
78 9 ▶ if (my $on_leave = $on_leave_state{$self}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $on_leave = $on_leave_state{$self}) 0 9 9
79 0 ▶ while (defined(my $cb_and_args = shift @$on_leave)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $cb_and_args = shift @$on_leave)) 0 0 0
80 0 my $cb = shift @$cb_and_args;
81 0 ▶ $debug and _debug($self, "calling on_leave_state hook $cb");
Branch: 1 branch
BranchTrueFalseTotal
$Class::StateMachine::debug and _debug($self, "calling on_leave_state hook $cb") 0 0 0
82 0 ▶ ref $cb ? $cb->(@$cb_and_args) : $self->$cb(@$cb_and_args);
Branch: 1 branch
BranchTrueFalseTotal
ref $cb ? : 0 0 0
83 0 ▶ return $state{$self} if $state_changed{$self};
Branch: 1 branch
BranchTrueFalseTotal
if $state_changed{$self} 0 0 0
84 }
85 }
86  
87 9 my $base_class = ref($self);
88 9 $base_class =~ s|::__state__::.*$||;
89 9 my $class = _bootstrap_state_class($base_class, $new_state);
90 9 $state{$self} = $new_state;
91 9 bless $self, $class;
92 9 ▶ $debug and _debug($self, "real class set to $class");
Branch: 1 branch
BranchTrueFalseTotal
$Class::StateMachine::debug and _debug($self, "real class set to $class") 0 9 9
93  
94 9 my $delayed = $delayed{$self};
95 9 ▶ my $delayed_top = ($delayed ? $#$delayed : -1);
Branch: 1 branch
BranchTrueFalseTotal
$delayed ? : 6 3 9
96  
97 9 ▶ if (my $enter = $self->can('enter_state')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $enter = $self->can("enter_state")) 9 0 9
98 9 ▶ $debug and _debug($self, "calling enter_state($new_state, $old_state)");
Branch: 1 branch
BranchTrueFalseTotal
$Class::StateMachine::debug and _debug($self, "calling enter_state($new_state, $old_state)") 0 9 9
99 9 $enter->($self, $new_state, $old_state);
100 9 ▶ $debug and _debug($self, "back from enter_state($new_state, $old_state)");
Branch: 1 branch
BranchTrueFalseTotal
$Class::StateMachine::debug and _debug($self, "back from enter_state($new_state, $old_state)") 0 9 9
101 9 ▶ return $state{$self} if $state_changed{$self};
Branch: 1 branch
BranchTrueFalseTotal
if $state_changed{$self} 0 9 9
102 }
103  
104 9 for (0..$delayed_top) {
105 2 my $action = shift @$delayed;
106 2 ▶ $debug and _debug($self, "running delayed action $action");
Branch: 1 branch
BranchTrueFalseTotal
$Class::StateMachine::debug and _debug($self, "running delayed action $action") 0 2 2
107 2 $self->$action;
108 2 ▶ return $state{$self} if $state_changed{$self};
Branch: 1 branch
BranchTrueFalseTotal
if $state_changed{$self} 0 2 2
109 }
110 }
111  
112 10 ▶ $debug and _debug($self, "state set to $state{$self}");
Branch: 1 branch
BranchTrueFalseTotal
$Class::StateMachine::debug and _debug($self, "state set to $state{$self}") 0 10 10
113 10 return $state{$self};
114 }
115  
116 1 ▶sub _bless {
Subroutine _bless: called CC=6 SCAR=21.8
117 1 my ($self, $base_class, $state) = @_;
118 1 ▶ $base_class //= caller;
Condition33%1/3
$base_class //= caller
l!l&&r!l&&!r
100
MC/DC0%0/2
$base_class //= caller
$base_class caller
Truth table33%1/3
A //= B
A= $base_classB= caller
ABresult
1X1
011
000
119 1 ▶ if (defined $state) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $state) { } 0 1 1
120 0 ▶ defined $state{$self} and croak "unable to change state when reblessing";
Branch: 1 branch
BranchTrueFalseTotal
defined $state{$self} and croak("unable to change state when reblessing") 0 0 0
121 0 $state{$self} = $state;
122 }
123 else {
124 1 ▶ $state{$self} //= 'new';
Condition50%1/2
$state{$self} //= "new"
l!l
01
MC/DC0%0/1
$state{$self} //= "new"
$state{$self}
Truth table50%1/2
A
A= $state{$self}
Aresult
11
00
125 }
126 1 my $class = _bootstrap_state_class($base_class, $state{$self});
127 1 bless $self, $class;
128 1 ▶ $debug and _debug($self, "real class set to $class");
Branch: 1 branch
BranchTrueFalseTotal
$Class::StateMachine::debug and _debug($self, "real class set to $class") 0 1 1
129 1 $self;
130 }
131  
132 2 ▶sub _delay {
Subroutine _delay: called CC=5 SCAR=16.2
133 2 my $self = shift;
134 2 my $code;
135 2 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 1 1 2
136 1 $code = shift;
137 1 ▶ @_ and croak 'Usage: $self->delay_until_next_state($method_or_cb)';
Branch: 1 branch
BranchTrueFalseTotal
@_ and croak("Usage: \$self->delay_until_next_state(\$method_or_cb)") 0 1 1
138 1 ▶ defined $code or return;
Branch: 1 branch
BranchTrueFalseTotal
defined $code or return 1 0 1
139 }
140 else {
141 1 $code = (caller 1)[3];
142 1 $code =~ s/.*:://;
143 }
144 2 ▶ my $delayed = ($delayed{$self} //= []);
Condition100%2/2
$delayed{$self} //= []
l!l
11
MC/DC100%1/1
$delayed{$self} //= []
$delayed{$self}
Truth table100%2/2
A
A= $delayed{$self}
Aresult
11
00
145 2 push @$delayed, $code;
146 }
147  
148 2 ▶sub _delay_once {
Subroutine _delay_once: called CC=6 SCAR=24.8
149 2 my $self = shift;
150 2 my $method;
151 2 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 2 2
152 0 $method = shift;
153 0 ▶ @_ and croak 'Usage: $self->delay_once_until_next_state($method)';
Branch: 1 branch
BranchTrueFalseTotal
@_ and croak("Usage: \$self->delay_once_until_next_state(\$method)") 0 0 0
154 0 ▶ defined $method or return;
Branch: 1 branch
BranchTrueFalseTotal
defined $method or return 0 0 0
155 0 ▶ croak "_delay_once does not accept code refs" if ref $method;
Branch: 1 branch
BranchTrueFalseTotal
if ref $method 0 0 0
156 }
157 else {
158 2 $method = (caller 1)[3];
159 2 $method =~ s/.*:://;
160 }
161 2 ▶ $delayed_once{$self}{$method}++
Branch: 1 branch
BranchTrueFalseTotal
$delayed_once{$self}{$method}++ or _delay($self, $method) 1 1 2
162 or _delay($self, $method);
163 }
164  
165 0 ▶sub _on_leave_state {
Subroutine _on_leave_state: not called CC=4 SCAR=30.0
166 0 my $self = shift;
167 0 ▶ @_ or croak 'Usage: $self->on_leave_state($callback, @args)';
Branch: 1 branch
BranchTrueFalseTotal
@_ or croak("Usage: \$self->on_leave_state(\$callback, \@args)") 0 0 0
168 0 ▶ push @{$on_leave_state{$self} //= []}, [@_] if defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0] 0 0 0
Condition0%0/2
$on_leave_state{$self} //= []
l!l
00
MC/DC0%0/1
$on_leave_state{$self} //= []
$on_leave_state{$self}
Truth table0%0/2
A
A= $on_leave_state{$self}
Aresult
11
00
169 }
170  
171 0 ▶sub _state_changed_on_call {
Subroutine _state_changed_on_call: not called CC=3 SCAR=24.8
172 0 my $self = shift;
173 0 my $cb = shift;
174 0 ▶ local $state_changed{$self} if $state_changed{$self};
Branch: 1 branch
BranchTrueFalseTotal
if $state_changed{$self} 0 0 0
175 0 ▶ ref $cb ? $cb->(@_) : $self->$cb(@_);
Branch: 1 branch
BranchTrueFalseTotal
ref $cb ? : 0 0 0
176 0 $state_changed{$self};
177 }
178  
179 10 ▶sub _bootstrap_state_class {
Subroutine _bootstrap_state_class: called CC=3 SCAR=11.0
180 10 my ($class, $state) = @_;
181 10 my $state_class = "${class}::__state__::${state}";
182 10 ▶ unless ($class_bootstrapped{$state_class}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class_bootstrapped{$state_class}) 8 2 10
183 # disallow control characters and colons inside state names:
184 8 ▶ $state =~ m|^[\x21-\x39\x3b-\x7f]+$| or croak "'$state' is not a valid state name";
Branch: 1 branch
BranchTrueFalseTotal
$state =~ /^[\x21-\x39\x3b-\x7f]+$/ or croak("'${state}' is not a valid state name") 8 0 8
185 8 $class_bootstrapped{$state_class} = 1;
186  
187 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
188 8 @{$state_class.'::ISA'} = $class;
189 8 ${$state_class.'::state'} = $state;
190 8 ${$state_class.'::base_class'} = $class;
191 8 ${$state_class.'::state_class'} = $state_class;
192 8 mro::set_mro($state_class, 'statemachine');
193 }
194 10 return $state_class;
195 }
196  
197 1 my @state_methods;
198  
199 13 ▶sub _handle_attr_OnState {
Subroutine _handle_attr_OnState: called CC=4 SCAR=14.1
200 13 my ($class, $sub, $on_state) = @_;
201 13 my ($filename, $line) = (caller 2)[1,2];
202 13 my ($err, @on_state);
203 13 do {
204 13 local $@;
205 13 @on_state = _eval_states <<EOE;
206 package $class;
207 no warnings 'reserved';
208 # line $line $filename
209 $on_state;
210 EOE
211 13 $err = $@;
212 };
213 13 ▶ croak $err if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 13 13
214 13 ▶ grep(!defined, @on_state) and croak "undef is not a valid state";
Branch: 1 branch
BranchTrueFalseTotal
grep((!defined($_)), @on_state) and croak("undef is not a valid state") 0 13 13
215 13 ▶ @on_state or warnings::warnif('Class::StateMachine',
Branch: 1 branch
BranchTrueFalseTotal
@on_state or warnings::warnif("Class::StateMachine", "no states on OnState attribute declaration") 13 0 13
216 'no states on OnState attribute declaration');
217 13 push @state_methods, [$class, undef, $sub, @on_state];
218 }
219  
220 1 ▶sub _move_state_methods {
Subroutine _move_state_methods: called CC=6 SCAR=18.1
221 1 ▶ $_->[1] //= CvGV $_->[2] for @state_methods;
Condition33%1/3
$_->[1] //= CvGV($_->[2])
l!l&&r!l&&!r
0130
MC/DC0%0/2
$_->[1] //= CvGV($_->[2])
$_->[1] CvGV($_->[2])
Truth table33%1/3
A //= B
A= $_->[1]B= CvGV($_->[2])
ABresult
1X1
011
000
222 1 ▶ while (@state_methods) {
Branch: 1 branch
BranchTrueFalseTotal
if (@state_methods) 13 1 14
223 13 my ($class, $sym, $sub, @on_state) = @{shift @state_methods};
224 13 ▶ my ($method) = $sym=~/([^:]+)$/ or croak "invalid symbol name '$sym'";
Branch: 1 branch
BranchTrueFalseTotal
my($method) = $sym =~ /([^:]+)$/ or croak("invalid symbol name '${sym}'") 13 0 13
225  
226 13 my $stash = Package::Stash->new($class);
227 13 $stash->remove_symbol("&$method");
228  
229 13 for my $state (@on_state) {
230 15 my $methods_class = join('::', $class, '__methods__', $state);
231 15 my $full_name = "${methods_class}::$method";
232 # print "registering method at $full_name\n";
233 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
234 15 *$full_name = subname($full_name, $sub);
235 }
236 }
237 }
238  
239 1 ▶sub _set_state_isa {
Subroutine _set_state_isa: called CC=2 SCAR=7.0
240 1 my ($class, $state, @isa) = @_;
241 1 %class_bootstrapped = ();
242 1 ▶ mro::method_changed_in($class) if mro::get_pkg_gen($class);
Branch: 1 branch
BranchTrueFalseTotal
if &mro::get_pkg_gen($class) 1 0 1
243 1 $class_state_isa{$class}{$state} = \@isa;
244 }
245  
246 1 ▶sub _state_isa {
Subroutine _state_isa: called CC=3 SCAR=11.2
247 1 my ($class, $state) = @_;
248 1 ▶ if (my $ref = ref $class) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ref = ref $class) 1 0 1
249 1 ▶ $state //= $class->state;
Condition33%1/3
$state //= $class->state
l!l&&r!l&&!r
010
MC/DC0%0/2
$state //= $class->state
$state $class->state
Truth table33%1/3
A //= B
A= $stateB= $class->state
ABresult
1X1
011
000
250 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
251 1 $class = ${$ref . "::base_class"};
252 }
253 1 _state_isa_from_derived([grep { $_->isa('Class::StateMachine') } @{mro::get_linear_isa($class)}],
254 $state);
255 }
256  
257  
258 59 ▶sub _state_isa_from_derived {
Subroutine _state_isa_from_derived: called CC=6 SCAR=18.0
259 59 my ($derived, $state) = @_;
260 59 my (@isa, @queue, %seen);
261 59 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
defined($state = shift @queue) and do { do { unless ($seen{$state}++) { push @isa, $state; foreach my $class (@$derived) { if (my $isa = $class_state_isa{$class}{$state}) { push @queue, @$isa; last; } ; } ; } } } 8 59 67
262 67 ▶ unless ($seen{$state}++) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($seen{$state}++) 67 0 67
263 67 push @isa, $state;
264 67 for my $class (@$derived) {
265 193 ▶ if (my $isa = $class_state_isa{$class}{$state}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $isa = $class_state_isa{$class}{$state}) 8 185 193
266 8 push @queue, @$isa;
267 8 last;
268 }
269 }
270 }
271 } while (defined($state = shift @queue));
272 59 push @isa, '__any__';
273 59 ▶ wantarray ? @isa : $isa[1];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 59 0 59
274 }
275  
276 # we make $^V into a constant so that it gets optimized away at
277 # compile time:
278 1 ▶use constant _perl_version => $^V;
Subroutine BEGIN: called CC=1 SCAR=0.0
279  
280 # use Data::Dumper;
281 58 ▶sub _statemachine_mro {
Subroutine _statemachine_mro: called CC=3 SCAR=11.0
282 58 my $stash = shift;
283 58 ▶ _move_state_methods if @state_methods;
Branch: 1 branch
BranchTrueFalseTotal
if @state_methods 1 57 58
284 58 my $base_class = ${$stash->{base_class}};
285 58 my $state = ${$stash->{state}};
286 58 my @linear = @{mro::get_linear_isa($base_class)};
287 58 my @derived = grep { $_->isa('Class::StateMachine') } @linear;
288 58 my @classes;
289 58 for my $state (_state_isa_from_derived(\@derived, $state)) {
290 123 push @classes, map join('::', $_, '__methods__', $state), @derived;
291 }
292  
293 58 ( _perl_version >= 5.016000
294 ? [ grep mro::get_pkg_gen($_), @classes, @linear ]
295 # workaround bug on early mro implementations where the first
296 # class on the list returned was always discarded. Also, as we may
297 # have inserted methods from this callback, the state class should
298 # be searched again, so we hardcode it in the list even when empty.
299 : [ $classes[0], grep mro::get_pkg_gen($_), @classes, @linear ] )
300 }
301  
302 1 MRO::Define::register_mro('statemachine' => \&_statemachine_mro);
303  
304 package Class::StateMachine;
305 1 ▶use warnings::register;
Subroutine BEGIN: called CC=1 SCAR=0.0
306  
307 13 ▶sub MODIFY_CODE_ATTRIBUTES {
Subroutine MODIFY_CODE_ATTRIBUTES: called CC=2 SCAR=7.6
308 13 my ($class, undef, @attr) = @_;
309 13 ▶ grep { !/^OnState\((.*)\)$/
Condition33%1/3
!/^OnState\((.*)\)$/ || (Class::StateMachine::Private::_handle_attr_OnState($class, $_[1], $1), 0)
l!l&&r!l&&!r
0013
MC/DC0%0/2
!/^OnState\((.*)\)$/ || (Class::StateMachine::Private::_handle_attr_OnState($class, $_[1], $1), 0)
!/^OnState\((.*)\)$/ (Class::StateMachine::Private::_handle_attr_OnState($class, $_[1], $1), 0)
Truth table33%1/3
not(A) || B
A= !/^OnState\((.*)\)$/B= (Class::StateMachine::Private::_handle_attr_OnState($class, $_[1], $1), 0)
ABresult
1X1
011
000
310 or (Class::StateMachine::Private::_handle_attr_OnState($class, $_[1], $1), 0) } @attr;
311 }
312  
313 1 *state = \&Class::StateMachine::Private::_state;
314 1 *rebless = \&Class::StateMachine::Private::_bless;
315 1 *bless = \&Class::StateMachine::Private::_bless;
316 1 *delay_until_next_state = \&Class::StateMachine::Private::_delay;
317 1 *delay_once_until_next_state = \&Class::StateMachine::Private::_delay_once;
318 1 *on_leave_state = \&Class::StateMachine::Private::_on_leave_state;
319 1 *state_changed_on_call = \&Class::StateMachine::Private::_state_changed_on_call;
320 1 *set_state_isa = \&Class::StateMachine::Private::_set_state_isa;
321 1 *state_isa = \&Class::StateMachine::Private::_state_isa;
322  
323 1 ▶sub ref {
Subroutine ref: called CC=3 SCAR=11.4
Pod documented
324 1 my $class = ref $_[0];
325 1 ▶ return '' if $class eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $class eq "" 0 1 1
326 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
327 1 ▶ ${$class .'::base_class'} // $class;
Condition33%1/3
${$class . '::base_class';} // $class
l!l&&r!l&&!r
100
MC/DC0%0/2
${$class . '::base_class';} // $class
${$class . '::base_class';} $class
Truth table33%1/3
A // B
A= ${$class . '::base_class';}B= $class
ABresult
1X1
011
000
328 }
329  
330 0 ▶sub DESTROY {}
Subroutine DESTROY: not called CC=1 SCAR=0.0
331 1 ▶sub import {}
Subroutine import: called CC=1 SCAR=0.0
332  
333 1 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=8 SCAR=20.9
334 1 our $AUTOLOAD;
335 1 my $self = $_[0];
336 1 ▶ if (CORE::ref $self and defined $state{$self}) {
Branch: 1 branch
BranchTrueFalseTotal
if (CORE::ref $self and defined $state{$self}) { } 1 0 1
Condition33%1/3
CORE::ref $self and defined $state{$self}
!ll&&!rl&&r
001
MC/DC0%0/2
CORE::ref $self and defined $state{$self}
CORE::ref $self defined $state{$self}
Truth table33%1/3
A and B
A= CORE::ref $selfB= defined $state{$self}
ABresult
0X0
100
111
337 1 my $state = $state{$self};
338 1 my $method = $AUTOLOAD;
339 1 $method =~ s/.*:://;
340 1 my $state_class = CORE::ref($self);
341 1 my @state_mro = @{mro::get_linear_isa($state_class)};
342 1 my $base_class = Class::StateMachine::ref($self);
343 1 my @base_mro = @{mro::get_linear_isa($base_class)};
344  
345 1 my @submethods;
346 1 for my $class (@base_mro) {
347 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
348 3 ▶ if (exists ${"${class}::"}{"__methods__::"}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists do { "${class}::" }->{"__methods__::"}) 2 1 3
349 2 my $methods = "${class}::__methods__::";
350 2 for my $state (grep /::$/, keys %$methods) {
351 10 ▶ exists ${"$methods$state"}{$method} and
Branch: 1 branch
BranchTrueFalseTotal
exists do { "$methods$state" }->{$method} and push @submethods, "$methods$state$method" 3 7 10
352 push @submethods, "$methods$state$method";
353 }
354 }
355 3 defined *{"${class}::$method"}{CODE} and
356 3 ▶ push @submethods, "${class}::$method";
Branch: 1 branch
BranchTrueFalseTotal
defined *{"${class}::$method";}{"CODE"} and push @submethods, "${class}::$method" 0 3 3
357 }
358  
359 1 my $error = join("\n",
360 qq|Can't locate Class::StateMachine object method "$method" via package "$base_class" ("$state_class") for object in state "$state"|,
361 "The base mro is:",
362 " " . join("\n ", @base_mro),
363 "The state mro is:",
364 " " . join("\n ", @state_mro),
365 "The submethods on the inheritance chain are:",
366 " " . join("\n ", @submethods),
367 "...");
368 1 local $Carp::Verbose = 1;
369 1 Carp::croak $error;
370 }
371 else {
372 0 Carp::croak "Undefined subroutine &$AUTOLOAD called"
373 }
374 }
375  
376 0 ▶sub install_method {
Subroutine install_method: not called CC=3 SCAR=24.8
Pod documented
377 0 my ($class, $name, $sub, @states) = @_;
378 0 ▶ CORE::ref($class) and Carp::croak "$class is not a package valid package name";
Branch: 1 branch
BranchTrueFalseTotal
CORE::ref $class and Carp::croak("$class is not a package valid package name") 0 0 0
379 0 ▶ CORE::ref($sub) eq 'CODE' or Carp::croak "$sub is not a subroutine reference";
Branch: 1 branch
BranchTrueFalseTotal
CORE::ref $sub eq "CODE" or Carp::croak("$sub is not a subroutine reference") 0 0 0
380 0 push @state_methods, [$class, $name, $sub, @states];
381 0 Class::StateMachine::Private::_move_state_methods;
382 }
383  
384 1 1;
385 __END__
↑ Summary