my %action = ( qr/^give (.*)(?:\s+(to) (.+)))$/ => \&give_to, qr/^go (.*)$/ => \&go_to, qr/^take (\w+)$/ => \&take_item, ); while (<>) { my $matched; for my $pattern (keys %action) { if (/$pattern/) { $action{$pattern}->(); $matched++; last; }; }; print "I didn't understand what you meant by '$_'.\n"; };