my $machine = {$Start => \&do_this, $Finish => \&do_that}; my $doit = \&do_this; foreach my $line(@Input) { my $func = $machine->{$line}; $doit = $func if (defined $func); $doit(); }