DekGenius.com
[ Team LiB ] Previous Section Next Section

9.6 Making a Method Work with Either Classes or Instances

All you need to fix this is a way to detect whether the method is called on a class or an instance. The most straightforward way to find out is with the ref operator. This operator returns a string (the classname) when used on a blessed reference, and undef when used on a string (like a classname). Modify the name method first to notice the change:

sub name {
  my $either = shift;
  ref $either
    ? $$either                # it's an instance, return name
    : "an unnamed $either";   # it's a class, return generic
}

Here the ?: operator selects either the dereference or a derived string. Now you can use it with either an instance or a class. Note that you changed the first parameter holder to $either to show that it is intentional:

print Horse->name, "\n";      # prints "an unnamed Horse\n"

my $tv_horse = Horse->named("Mr. Ed");
print $tv_horse->name, "\n";   # prints "Mr Ed.\n"

and now you'll fix speak to use this:

sub speak {
  my $either = shift;
  print $either->name, " goes ", $either->sound, "\n";
}

Since sound already worked with either a class or an instance, you're done!

    [ Team LiB ] Previous Section Next Section