> I keep running into situations where I want to set an item to a new
> state only if it is not equal to some other state, but if I just set
> that state this loop, the future value will not be returned by state().
>
> For example:
>
> set $mode_last_leave_count 'two';
> ...
> if (state $mode_last_leave_count eq 'none') {
> set $mode_last_leave_count 'one';
> }
>
> Well, the state of $mode_last_leave_count is set to 'two' but
> state() still
> returns 'none' causing this code to change the state to 'one'
> when I want it
> left as 'two'.
>
> So, I added this new function to Generic_Item.pm which helps me
> here. It is
> called final_state() and return the state as it will be after all pending
> states, if any, are applied.
>
> Here it is (goes in Generic_Item.pm):
>
> # Returns the objects final state that the object will be in after all
> # pending states are processed
> sub final_state {
> my ($self) = @_;
> if (@{$self->{state_next_pass}}) {
> return
> $self->{state_next_pass}->[$#{@{$self->{state_next_pass}}}];
> }
> return $self->state();
> }
Your new method looks good. I made 2 minor changes. A bit simpler way to
get the last element in a list array is to use a -1 index, like this:
return $self->{state_next_pass}->[-1];
The other change I made was to call the new method state_final, rather than
final_state. It reads maybe a little less intuitively, but it is more
consistent with all the other state_* methods.
Bruce
|