| 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
MC/DC0%0/1
our $debug //= 0
our $debug
Truth table50%1/2
A
A= our $debug
| |||||||||||||||||||||||||||||
| 16 | 1 | ▶ | our $ignore_same_state_changes //= 0; | ||||||||||||||||||||||||||
|
Condition50%1/2
our $ignore_same_state_changes //= 0
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 47 | 0 | ▶ | my $state = $state{$self} // '<undef>'; | ||||||||||||||||||||||||||
|
Condition0%0/2
$state{$self} // "<undef>"
MC/DC0%0/1
$state{$self} // "<undef>"
$state{$self}
Truth table0%0/2
A
A= $state{$self}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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')
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}
| |||||||||||||||||||||||||||||
| 60 | "use Class::StateMachine::bless to create Class::StateMachine objects"); | ||||||||||||||||||||||||||||
| 61 | 10 | ▶ | if (defined $new_state) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
$Class::StateMachine::ignore_same_state_changes and $new_state eq $old_state
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
| |||||||||||||||||||||||||||||
| 64 | 9 | ▶ | $debug and _debug($self, "changing state from $old_state to $new_state"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 69 | 0 | ▶ | $debug and _debug($self, "checking state $new_state"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 0 | ▶ | $check->($self, $new_state) or croak qq(invalid state "$new_state"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | 0 | ▶ | return $state{$self} if $state_changed{$self}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | 9 | ▶ | if (my $leave = $self->can('leave_state')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 9 | ▶ | $debug and _debug($self, "calling leave_state($old_state, $new_state)"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 9 | $leave->($self, $old_state, $new_state); | |||||||||||||||||||||||||||
| 76 | 9 | ▶ | return $state{$self} if $state_changed{$self}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | 9 | ▶ | if (my $on_leave = $on_leave_state{$self}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 0 | ▶ | while (defined(my $cb_and_args = shift @$on_leave)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 0 | my $cb = shift @$cb_and_args; | |||||||||||||||||||||||||||
| 81 | 0 | ▶ | $debug and _debug($self, "calling on_leave_state hook $cb"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 0 | ▶ | ref $cb ? $cb->(@$cb_and_args) : $self->$cb(@$cb_and_args); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 0 | ▶ | return $state{$self} if $state_changed{$self}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 9 | my $delayed = $delayed{$self}; | |||||||||||||||||||||||||||
| 95 | 9 | ▶ | my $delayed_top = ($delayed ? $#$delayed : -1); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 9 | ▶ | if (my $enter = $self->can('enter_state')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 9 | ▶ | $debug and _debug($self, "calling enter_state($new_state, $old_state)"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 101 | 9 | ▶ | return $state{$self} if $state_changed{$self}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 107 | 2 | $self->$action; | |||||||||||||||||||||||||||
| 108 | 2 | ▶ | return $state{$self} if $state_changed{$self}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 10 | ▶ | $debug and _debug($self, "state set to $state{$self}"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
MC/DC0%0/2
$base_class //= caller
$base_class
caller
Truth table33%1/3
A //= B
A= $base_classB= caller
| |||||||||||||||||||||||||||||
| 119 | 1 | ▶ | if (defined $state) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 0 | ▶ | defined $state{$self} and croak "unable to change state when reblessing"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | 0 | $state{$self} = $state; | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | else { | ||||||||||||||||||||||||||||
| 124 | 1 | ▶ | $state{$self} //= 'new'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$state{$self} //= "new"
MC/DC0%0/1
$state{$self} //= "new"
$state{$self}
Truth table50%1/2
A
A= $state{$self}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 136 | 1 | $code = shift; | |||||||||||||||||||||||||||
| 137 | 1 | ▶ | @_ and croak 'Usage: $self->delay_until_next_state($method_or_cb)'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | 1 | ▶ | defined $code or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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} //= []
MC/DC100%1/1
$delayed{$self} //= []
$delayed{$self}
Truth table100%2/2
A
A= $delayed{$self}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 152 | 0 | $method = shift; | |||||||||||||||||||||||||||
| 153 | 0 | ▶ | @_ and croak 'Usage: $self->delay_once_until_next_state($method)'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | 0 | ▶ | defined $method or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | 0 | ▶ | croak "_delay_once does not accept code refs" if ref $method; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | else { | ||||||||||||||||||||||||||||
| 158 | 2 | $method = (caller 1)[3]; | |||||||||||||||||||||||||||
| 159 | 2 | $method =~ s/.*:://; | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | 2 | ▶ | $delayed_once{$self}{$method}++ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 168 | 0 | ▶ | push @{$on_leave_state{$self} //= []}, [@_] if defined $_[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$on_leave_state{$self} //= []
MC/DC0%0/1
$on_leave_state{$self} //= []
$on_leave_state{$self}
Truth table0%0/2
A
A= $on_leave_state{$self}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 175 | 0 | ▶ | ref $cb ? $cb->(@_) : $self->$cb(@_); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 214 | 13 | ▶ | grep(!defined, @on_state) and croak "undef is not a valid state"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 215 | 13 | ▶ | @on_state or warnings::warnif('Class::StateMachine', | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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])
MC/DC0%0/2
$_->[1] //= CvGV($_->[2])
$_->[1]
CvGV($_->[2])
Truth table33%1/3
A //= B
A= $_->[1]B= CvGV($_->[2])
| |||||||||||||||||||||||||||||
| 222 | 1 | ▶ | while (@state_methods) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 249 | 1 | ▶ | $state //= $class->state; | ||||||||||||||||||||||||||
|
Condition33%1/3
$state //= $class->state
MC/DC0%0/2
$state //= $class->state
$state
$class->state
Truth table33%1/3
A //= B
A= $stateB= $class->state
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 262 | 67 | ▶ | unless ($seen{$state}++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 263 | 67 | push @isa, $state; | |||||||||||||||||||||||||||
| 264 | 67 | for my $class (@$derived) { | |||||||||||||||||||||||||||
| 265 | 193 | ▶ | if (my $isa = $class_state_isa{$class}{$state}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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)
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)
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
MC/DC0%0/2
${$class . '::base_class';} // $class
${$class . '::base_class';}
$class
Truth table33%1/3
A // B
A= ${$class . '::base_class';}B= $class
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
CORE::ref $self and defined $state{$self}
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}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 349 | 2 | my $methods = "${class}::__methods__::"; | |||||||||||||||||||||||||||
| 350 | 2 | for my $state (grep /::$/, keys %$methods) { | |||||||||||||||||||||||||||
| 351 | 10 | ▶ | exists ${"$methods$state"}{$method} and | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 352 | push @submethods, "$methods$state$method"; | ||||||||||||||||||||||||||||
| 353 | } | ||||||||||||||||||||||||||||
| 354 | } | ||||||||||||||||||||||||||||
| 355 | 3 | defined *{"${class}::$method"}{CODE} and | |||||||||||||||||||||||||||
| 356 | 3 | ▶ | push @submethods, "${class}::$method"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 379 | 0 | ▶ | CORE::ref($sub) eq 'CODE' or Carp::croak "$sub is not a subroutine reference"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 380 | 0 | push @state_methods, [$class, $name, $sub, @states]; | |||||||||||||||||||||||||||
| 381 | 0 | Class::StateMachine::Private::_move_state_methods; | |||||||||||||||||||||||||||
| 382 | } | ||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||
| 384 | 1 | 1; | |||||||||||||||||||||||||||
| 385 | __END__ | ||||||||||||||||||||||||||||