#!/usr/bin/perl -w use strict; use warnings; # Set empty strings/arrays my $typed_command = my $cmd_verb = my $cmd_noun = my $action = ""; my @cmd_list = (); # Define subroutines sub misunderstood {print "I don't understand `@cmd_list'\n";} sub go_north {print "You go north\n";} sub go_south {print "You go south\n";} sub go_west {print "You go west\n";} sub go_east {print "You go east\n";} sub get {print "You take the $cmd_noun\n";} sub jump {print "Boing! Boing!\n";} sub quit {print "See you later.\n"; exit;} # Declare valid commands hash table # Pair up the commands with the subroutines that will execute them. # The verb is used as a key in the hash table for quick selection. # This will avoid a whole string of if statements later and keep the # definition near the top of the program for easy editing later. # Don't forget to include synonyms! my %valid_commands = ( 'north', \&go_north, 'n', \&go_north, 'south', \&go_south, 's', \&go_south, 'west', \&go_west, 'w', \&go_west, 'east', \&go_east, 'e', \&go_east, 'get', \&get, 'take', \&get, 'quit', \&quit, 'jump', \&jump, ); # Main loop print "What now?>"; while (defined($typed_command = )) { @cmd_list = split " ", lc($typed_command); $cmd_verb = $cmd_list[0]; $cmd_noun = $cmd_list[1]; if (exists($valid_commands{$cmd_verb})) { $action = $valid_commands{$cmd_verb}; &$action($cmd_noun); } else { &misunderstood(@cmd_list); } print "What now?>"; }