I am trying to make a state machine in Perl. To do this I have an array indexed by statenames. I can put subs into this array. Like this:
use constant {
stInit => 0,
stHeader => 1,
stSalesHeader => 2,
stCatagory => 3,
stData => 4,
stTotal => 5,
stError => 6,
};
my $state = stInit;
my @actions;
$actions[stInit] = [sub{logState("Initial state entered",2) }];
$actions[stHeader] = [sub{logState("Header state entered",2) }];
$actions[stSalesHeader] = [sub{logState("Sales header state entered",2) }];
$actions[stCatagory] = [sub{logState("Category state entered",2) }];
$actions[stData] = [sub{logState("Data state entered",2) }];
$actions[stTotal] = [sub{logState("Total state entered",2) }];
But then I have no Idea how to call the subroutine. I have tried this
$actions[$state]
But that appears not to work. Is this possible or am I completely off?