## a dispatch table in the form state => CODEref my %operation = ( 1 => \&do_this, 0 => \&do_that, 9 => \&do_another_thing, ); ## a table of transitions in the form pattern => state my @transition = ( [ $Start => 1 ], [ $Finish => 0 ], [ $Break => 9 ], ); my $State = 0; # or whatever your initial state is. for my $item (@Input) { # check transition conditions in order; if met, change state for my $trans (@transition) { next unless $item =~ /$trans->[0]/; $State = $trans->[1]; last; } $operation->{$State}->(); # dereference and execute op for this State }