Stored Zoo data: Llama - Feet:4 - Color:Tan Camel - Feet:4 - Humps:2 #### #[pseudocode - will not compile] foreach $line () { # Get $name and hash data from file # $name, $animalstats (hashref) my $newanimal = new Animal($name, $animalstats); push (@allanimals, $newanimal); } #### __DATA__ Llamas: Llama1 - data data data Llama2 - data data data Camels: Camel1 - data data data Camel2 - data data data __CODE__ # [pseudocode] foreach (my $groupname = from_datafile()) { ## Llamas, Camels, the collections. unless (exists $allanimals{$groupname}) ## Create a new collection if you don't already have one for that animal type. my $newcollection = new Animal::Collection($groupname); $allanimals{$groupname} = $newcollection; } my $collection = $allanimals{$groupname}; # Now work with that collection. Add all your animals in that group. foreach $animal (animals_from_datafile()) { ## llama1, llama2, etc. $collection->add_animal(new Animal($animal, $animalstats); } } #### package Animal; sub new ( my $name = shift; my $data = shift; if($name == 'llama') { return new Animal::Llama($data); } elseif ($name == 'camel') { return new Animal::Camel($data); } else { warn "Unknown animal type\n"; return new Animal::Generic($name, $data); } } #### foreach $type (keys $allanimals) { my $num_of_animals = $allanimals{$type}->count_collection(); # A method in Animal::Colelction foreach $animal ($allanimals{$type}->get_animals()) { ## Using methods defined by the Animal interface ## But implemented in the Animal::Llama, Animal::Camel, etc subclasses. my $name = $animal->get_name(); $animal->feed(); } }