http://www.perlmonks.org?node_id=413036


in reply to Are state machines just for parsing?

Why doesn't the simple-minded method work?
#!/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]; }


-- All code is 100% tested and functional unless otherwise noted.