%FLUT = (
'condition_1' => \&func_1,
'condition_2' => \&func_2,
'condition_3' => \&func_3,
);
$condition = ## reduce the input to a known condition
$FLUT{$condition}->(@args);
####
package Command;
sub new {
my $type = shift;
my $O = shift;
bless $O, $type;
return ($O);
}
sub execute {
my $O = shift;
return ($O->( @_ ));
}
##
##
package Machine;
## new (nil) : Machine_ref
#
# trivial constructor
#
sub new {
my $self = bless {}, shift;
return ($self);
}
## execute
#
# run through the list of commands, halting when one of them fails
#
sub execute {
my $self = shift;
my $model = shift;
for $cmd (@{ $self->_get_commands() }) { # [1]
last if ( 0 == $cmd->execute ($model) ); # [2]
}
return();
}
###
# [1] iterate over the list of Command objects that belongs to the
# specific Machine, executing each one in turn.
# [2] drop out of the loop if any Command in the sequence returns
# a zero.
#
# in practice, this is equivalent to executing a set of
# short-circuit conditionals:
#
# if () { return ('value 1') }
# if () { return ('value 2') }
# if () { return ('value 3') }
#
# but it's cleaner, and you can make each Command much more complex
# than the body of a sensible conditional.
###
## STATE-SPECIFIC OPERATIONS
## _get_commands (nil) : listref
#
# each Machine has its own list of Commands that should be
# executed in response to the user's input. this routine returns
# a reference to that list.
#
sub _get_commands {
return ([]);
}
1;