#!/usr/bin/perl -w # State transition table # # Current Inputs Next Action $next{'initial'}{'left' } = ['red', sub { print "Red \n"} ]; $next{'initial'}{'right'} = ['blue', sub { print "Blue\n"} ]; $next{'red'} {'left' } = ['blue', sub { print "Blue\n"} ]; $next{'red'} {'right'} = ['end', sub { print "End \n"} ]; $next{'blue'} {'left' } = ['blue', sub { print "Blue\n"} ]; $next{'blue'} {'right'} = ['red', sub { print "Red \n"} ]; $next{'end'} {'left' } = ['end', sub { print "End \n"} ]; $next{'end'} {'right'} = ['end', sub { print "End \n"} ]; @arbitrary_inputs = qw/left left right left left right right left/; $state = 'initial'; for (@arbitrary_inputs) { $action = $next{$state}{$_}->[1]; $action->(); $state = $next{$state}{$_}->[0]; }