my %action = ( go => sub { # some code }, turn => \&turn_sub, #... } while (1) { my $act = <>; chomp $act; # parsing can get fancier here exists $action{$act} and $action{$act}->(); }