Приглашаем посетить
Гоголь (gogol-lit.ru)

Section 12.6.  Making a Method Work with Either Classes or Instances

Previous
Table of Contents
Next

12.6. Making a Method Work with Either Classes or Instances

All we 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 class name) when used on a blessed reference, and undef when used on a string (like a class name). We 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 we can use it with either an instance or a class. Note that we 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 we'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, we're done!


Previous
Table of Contents
Next