Документация
HTML CSS PHP PERL другое

Section 12.1.  A Horse Is a Horse, of Course of Courseor Is It?

 
Previous
Table of Contents
Next

12.1. A Horse Is a Horse, of Course of Courseor Is It?

Let's look at the code used in Chapter 11 for the Animal classes and Horse classes:

{ package Animal;
  sub speak {
    my $class = shift;
    print "a $class goes ", $class->sound, "!\n"
  }
}
{ package Horse;
  @ISA = qw(Animal);
  sub sound { 'neigh' }
}

This lets us invoke Horse->speak to ripple upward to Animal::speak, calling back to Horse::sound to get the specific sound, and the output of:

a Horse goes neigh!

But all Horse objects would have to be absolutely identical. If we add a method, all horses automatically share it. That's great for making horses identical, but how do we capture the properties of an individual horse? For example, suppose we want to give our horse a name. There's got to be a way to keep its name separate from those of other horses.

We can do so by establishing an instance. An instance is generally created by a class, much like a car is created by a car factory. An instance will have associated properties, called instance variables (or member variables, if you come from a C++ or Java background). An instance has a unique identity (like the serial number of a registered horse), shared properties (the color and talents of the horse), and common behavior (e.g., pulling the reins back tells the horse to stop).

In Perl, an instance must be a reference to one of the built-in types. Start with the simplest reference that can hold a horse's name, a scalar reference:[*]

[*] It's the simplest, but rarely used in real code for reasons we'll show shortly.

my $name = 'Mr. Ed';
my $tv_horse = \$name;

Now $tv_horse is a reference to what will be the instance-specific data (the name). The final step in turning this into a real instance involves a special operator called bless:

bless $tv_horse, 'Horse';

The bless operator follows the reference to find what variable it points toin this case, the scalar $name. Then it "blesses" that variable, turning $tv_horse into an objecta Horse object, in fact. (Imagine that a little sticky-note that says Horse is now attached to $name.)

At this point, $tv_horse is an instance of Horse.[Section 12.1.  A Horse Is a Horse, of Course of Courseor Is It?] That is, it's a specific horse. The reference is otherwise unchanged and can still be used with traditional dereferencing operators.[Section 12.1.  A Horse Is a Horse, of Course of Courseor Is It?]

[Section 12.1.  A Horse Is a Horse, of Course of Courseor Is It?] Actually, $tv_horse points to the object, but, in common terms, we nearly always deal with objects by references to those objects. Hence, it's simpler to say that $tv_horse is the horse, not "the thing that $tv_horse references."

[Section 12.1.  A Horse Is a Horse, of Course of Courseor Is It?] Although doing so outside the class is a bad idea, as we'll show later.


Previous
Table of Contents
Next
© 2000- NIV