home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


Learning Perl Objects, References & ModulesLearning Perl Objects, References & ModulesSearch this book

10.5. Using Class Variables

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

However, you 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,[51] while the value can be the actual reference, allowing you to access its name or class.

[51]Or any other convenient and unique string.

For example, let's extend named as follows:

## in Animal
our %REGISTRY;
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 metavariable that contains information about many instances.

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.]

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?



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.