Note that when used as a key, $self
stringifies, which means it turns into a
string unique to the object.
We also need to add a new method:
sub registered {
return map { "a ".ref($_)." named ".$_->name } values %REGISTRY;
}
Now you can see all the animals we've made:
my @cows = map Cow->named($_), qw(Bessie Gwen);
my @horses = map Horse->named($_), ("Trigger", "Mr. Ed");
my @racehorses = RaceHorse->named("Billy Boy");
print "We've seen:\n", map(" $_\n", Animal->registered);
print "End of program.\n";
This prints:
We've seen:
a RaceHorse named Billy Boy
a Horse named Mr. Ed
a Horse named Trigger
a Cow named Gwen
a Cow named Bessie
End of program.
[Billy Boy has died.]
[Billy Boy has gone off to the glue factory.]
[Bessie has died.]
[Gwen has died.]
[Trigger has died.]
[Trigger has gone off to the glue factory.]
[Mr. Ed has died.]
[Mr. Ed has gone off to the glue factory.]