Section 13.6.  Using Class Variables

Table of Contents

13.6. Using Class Variables

What if we want to iterate over all the animals we've made so far? Animals may exist all over the program namespace but are lost once they're handed back from the named constructor method.[*]

[*] Well, not really lost. Perl knows where they are, but we don't.

However, we can record the created animal in a hash and iterate over that hash. The key to the hash can be the stringified form of the animal reference,[Section 13.6.  Using Class Variables] while the value can be the actual reference, allowing us to access its name or class.

[Section 13.6.  Using Class Variables] Or any other convenient and unique string.

For example, let's extend named as follows:

## in Animal
sub named {
  my $class = shift;
  my $name = shift;
  my $self = { Name => $name, Color => $class->default_color };
  bless $self, $class;
  $REGISTRY{$self} = $self;  # also returns $self

The uppercase name for %REGISTRY is a reminder that this variable is more global than most variables. In this case, it's a meta-variable that contains information about many instances.

When we use $self as a key, Perl stringifies it, 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 we 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.]

Note that the animals die at their proper time because the variables holding the animals are all being destroyed at the final step. Or are they?

Table of Contents
© 2000- NIV