1N/A=head1 NAME
1N/A
1N/Aperlboot - Beginner's Object-Oriented Tutorial
1N/A
1N/A=head1 DESCRIPTION
1N/A
1N/AIf you're not familiar with objects from other languages, some of the
1N/Aother Perl object documentation may be a little daunting, such as
1N/AL<perlobj>, a basic reference in using objects, and L<perltoot>, which
1N/Aintroduces readers to the peculiarities of Perl's object system in a
1N/Atutorial way.
1N/A
1N/ASo, let's take a different approach, presuming no prior object
1N/Aexperience. It helps if you know about subroutines (L<perlsub>),
1N/Areferences (L<perlref> et. seq.), and packages (L<perlmod>), so become
1N/Afamiliar with those first if you haven't already.
1N/A
1N/A=head2 If we could talk to the animals...
1N/A
1N/ALet's let the animals talk for a moment:
1N/A
1N/A sub Cow::speak {
1N/A print "a Cow goes moooo!\n";
1N/A }
1N/A sub Horse::speak {
1N/A print "a Horse goes neigh!\n";
1N/A }
1N/A sub Sheep::speak {
1N/A print "a Sheep goes baaaah!\n"
1N/A }
1N/A
1N/A Cow::speak;
1N/A Horse::speak;
1N/A Sheep::speak;
1N/A
1N/AThis results in:
1N/A
1N/A a Cow goes moooo!
1N/A a Horse goes neigh!
1N/A a Sheep goes baaaah!
1N/A
1N/ANothing spectacular here. Simple subroutines, albeit from separate
1N/Apackages, and called using the full package name. So let's create
1N/Aan entire pasture:
1N/A
1N/A # Cow::speak, Horse::speak, Sheep::speak as before
1N/A @pasture = qw(Cow Cow Horse Sheep Sheep);
1N/A foreach $animal (@pasture) {
1N/A &{$animal."::speak"};
1N/A }
1N/A
1N/AThis results in:
1N/A
1N/A a Cow goes moooo!
1N/A a Cow goes moooo!
1N/A a Horse goes neigh!
1N/A a Sheep goes baaaah!
1N/A a Sheep goes baaaah!
1N/A
1N/AWow. That symbolic coderef de-referencing there is pretty nasty.
1N/AWe're counting on C<no strict subs> mode, certainly not recommended
1N/Afor larger programs. And why was that necessary? Because the name of
1N/Athe package seems to be inseparable from the name of the subroutine we
1N/Awant to invoke within that package.
1N/A
1N/AOr is it?
1N/A
1N/A=head2 Introducing the method invocation arrow
1N/A
1N/AFor now, let's say that C<< Class->method >> invokes subroutine
1N/AC<method> in package C<Class>. (Here, "Class" is used in its
1N/A"category" meaning, not its "scholastic" meaning.) That's not
1N/Acompletely accurate, but we'll do this one step at a time. Now let's
1N/Ause it like so:
1N/A
1N/A # Cow::speak, Horse::speak, Sheep::speak as before
1N/A Cow->speak;
1N/A Horse->speak;
1N/A Sheep->speak;
1N/A
1N/AAnd once again, this results in:
1N/A
1N/A a Cow goes moooo!
1N/A a Horse goes neigh!
1N/A a Sheep goes baaaah!
1N/A
1N/AThat's not fun yet. Same number of characters, all constant, no
1N/Avariables. But yet, the parts are separable now. Watch:
1N/A
1N/A $a = "Cow";
1N/A $a->speak; # invokes Cow->speak
1N/A
1N/AAhh! Now that the package name has been parted from the subroutine
1N/Aname, we can use a variable package name. And this time, we've got
1N/Asomething that works even when C<use strict refs> is enabled.
1N/A
1N/A=head2 Invoking a barnyard
1N/A
1N/ALet's take that new arrow invocation and put it back in the barnyard
1N/Aexample:
1N/A
1N/A sub Cow::speak {
1N/A print "a Cow goes moooo!\n";
1N/A }
1N/A sub Horse::speak {
1N/A print "a Horse goes neigh!\n";
1N/A }
1N/A sub Sheep::speak {
1N/A print "a Sheep goes baaaah!\n"
1N/A }
1N/A
1N/A @pasture = qw(Cow Cow Horse Sheep Sheep);
1N/A foreach $animal (@pasture) {
1N/A $animal->speak;
1N/A }
1N/A
1N/AThere! Now we have the animals all talking, and safely at that,
1N/Awithout the use of symbolic coderefs.
1N/A
1N/ABut look at all that common code. Each of the C<speak> routines has a
1N/Asimilar structure: a C<print> operator and a string that contains
1N/Acommon text, except for two of the words. It'd be nice if we could
1N/Afactor out the commonality, in case we decide later to change it all
1N/Ato C<says> instead of C<goes>.
1N/A
1N/AAnd we actually have a way of doing that without much fuss, but we
1N/Ahave to hear a bit more about what the method invocation arrow is
1N/Aactually doing for us.
1N/A
1N/A=head2 The extra parameter of method invocation
1N/A
1N/AThe invocation of:
1N/A
1N/A Class->method(@args)
1N/A
1N/Aattempts to invoke subroutine C<Class::method> as:
1N/A
1N/A Class::method("Class", @args);
1N/A
1N/A(If the subroutine can't be found, "inheritance" kicks in, but we'll
1N/Aget to that later.) This means that we get the class name as the
1N/Afirst parameter (the only parameter, if no arguments are given). So
1N/Awe can rewrite the C<Sheep> speaking subroutine as:
1N/A
1N/A sub Sheep::speak {
1N/A my $class = shift;
1N/A print "a $class goes baaaah!\n";
1N/A }
1N/A
1N/AAnd the other two animals come out similarly:
1N/A
1N/A sub Cow::speak {
1N/A my $class = shift;
1N/A print "a $class goes moooo!\n";
1N/A }
1N/A sub Horse::speak {
1N/A my $class = shift;
1N/A print "a $class goes neigh!\n";
1N/A }
1N/A
1N/AIn each case, C<$class> will get the value appropriate for that
1N/Asubroutine. But once again, we have a lot of similar structure. Can
1N/Awe factor that out even further? Yes, by calling another method in
1N/Athe same class.
1N/A
1N/A=head2 Calling a second method to simplify things
1N/A
1N/ALet's call out from C<speak> to a helper method called C<sound>.
1N/AThis method provides the constant text for the sound itself.
1N/A
1N/A { package Cow;
1N/A sub sound { "moooo" }
1N/A sub speak {
1N/A my $class = shift;
1N/A print "a $class goes ", $class->sound, "!\n"
1N/A }
1N/A }
1N/A
1N/ANow, when we call C<< Cow->speak >>, we get a C<$class> of C<Cow> in
1N/AC<speak>. This in turn selects the C<< Cow->sound >> method, which
1N/Areturns C<moooo>. But how different would this be for the C<Horse>?
1N/A
1N/A { package Horse;
1N/A sub sound { "neigh" }
1N/A sub speak {
1N/A my $class = shift;
1N/A print "a $class goes ", $class->sound, "!\n"
1N/A }
1N/A }
1N/A
1N/AOnly the name of the package and the specific sound change. So can we
1N/Asomehow share the definition for C<speak> between the Cow and the
1N/AHorse? Yes, with inheritance!
1N/A
1N/A=head2 Inheriting the windpipes
1N/A
1N/AWe'll define a common subroutine package called C<Animal>, with the
1N/Adefinition for C<speak>:
1N/A
1N/A { package Animal;
1N/A sub speak {
1N/A my $class = shift;
1N/A print "a $class goes ", $class->sound, "!\n"
1N/A }
1N/A }
1N/A
1N/AThen, for each animal, we say it "inherits" from C<Animal>, along
1N/Awith the animal-specific sound:
1N/A
1N/A { package Cow;
1N/A @ISA = qw(Animal);
1N/A sub sound { "moooo" }
1N/A }
1N/A
1N/ANote the added C<@ISA> array. We'll get to that in a minute.
1N/A
1N/ABut what happens when we invoke C<< Cow->speak >> now?
1N/A
1N/AFirst, Perl constructs the argument list. In this case, it's just
1N/AC<Cow>. Then Perl looks for C<Cow::speak>. But that's not there, so
1N/APerl checks for the inheritance array C<@Cow::ISA>. It's there,
1N/Aand contains the single name C<Animal>.
1N/A
1N/APerl next checks for C<speak> inside C<Animal> instead, as in
1N/AC<Animal::speak>. And that's found, so Perl invokes that subroutine
1N/Awith the already frozen argument list.
1N/A
1N/AInside the C<Animal::speak> subroutine, C<$class> becomes C<Cow> (the
1N/Afirst argument). So when we get to the step of invoking
1N/AC<< $class->sound >>, it'll be looking for C<< Cow->sound >>, which
1N/Agets it on the first try without looking at C<@ISA>. Success!
1N/A
1N/A=head2 A few notes about @ISA
1N/A
1N/AThis magical C<@ISA> variable (pronounced "is a" not "ice-uh"), has
1N/Adeclared that C<Cow> "is a" C<Animal>. Note that it's an array,
1N/Anot a simple single value, because on rare occasions, it makes sense
1N/Ato have more than one parent class searched for the missing methods.
1N/A
1N/AIf C<Animal> also had an C<@ISA>, then we'd check there too. The
1N/Asearch is recursive, depth-first, left-to-right in each C<@ISA>.
1N/ATypically, each C<@ISA> has only one element (multiple elements means
1N/Amultiple inheritance and multiple headaches), so we get a nice tree of
1N/Ainheritance.
1N/A
1N/AWhen we turn on C<use strict>, we'll get complaints on C<@ISA>, since
1N/Ait's not a variable containing an explicit package name, nor is it a
1N/Alexical ("my") variable. We can't make it a lexical variable though
1N/A(it has to belong to the package to be found by the inheritance mechanism),
1N/Aso there's a couple of straightforward ways to handle that.
1N/A
1N/AThe easiest is to just spell the package name out:
1N/A
1N/A @Cow::ISA = qw(Animal);
1N/A
1N/AOr allow it as an implicitly named package variable:
1N/A
1N/A package Cow;
1N/A use vars qw(@ISA);
1N/A @ISA = qw(Animal);
1N/A
1N/AIf you're bringing in the class from outside, via an object-oriented
1N/Amodule, you change:
1N/A
1N/A package Cow;
1N/A use Animal;
1N/A use vars qw(@ISA);
1N/A @ISA = qw(Animal);
1N/A
1N/Ainto just:
1N/A
1N/A package Cow;
1N/A use base qw(Animal);
1N/A
1N/AAnd that's pretty darn compact.
1N/A
1N/A=head2 Overriding the methods
1N/A
1N/ALet's add a mouse, which can barely be heard:
1N/A
1N/A # Animal package from before
1N/A { package Mouse;
1N/A @ISA = qw(Animal);
1N/A sub sound { "squeak" }
1N/A sub speak {
1N/A my $class = shift;
1N/A print "a $class goes ", $class->sound, "!\n";
1N/A print "[but you can barely hear it!]\n";
1N/A }
1N/A }
1N/A
1N/A Mouse->speak;
1N/A
1N/Awhich results in:
1N/A
1N/A a Mouse goes squeak!
1N/A [but you can barely hear it!]
1N/A
1N/AHere, C<Mouse> has its own speaking routine, so C<< Mouse->speak >>
1N/Adoesn't immediately invoke C<< Animal->speak >>. This is known as
1N/A"overriding". In fact, we didn't even need to say that a C<Mouse> was
1N/Aan C<Animal> at all, since all of the methods needed for C<speak> are
1N/Acompletely defined with C<Mouse>.
1N/A
1N/ABut we've now duplicated some of the code from C<< Animal->speak >>,
1N/Aand this can once again be a maintenance headache. So, can we avoid
1N/Athat? Can we say somehow that a C<Mouse> does everything any other
1N/AC<Animal> does, but add in the extra comment? Sure!
1N/A
1N/AFirst, we can invoke the C<Animal::speak> method directly:
1N/A
1N/A # Animal package from before
1N/A { package Mouse;
1N/A @ISA = qw(Animal);
1N/A sub sound { "squeak" }
1N/A sub speak {
1N/A my $class = shift;
1N/A Animal::speak($class);
1N/A print "[but you can barely hear it!]\n";
1N/A }
1N/A }
1N/A
1N/ANote that we have to include the C<$class> parameter (almost surely
1N/Athe value of C<"Mouse">) as the first parameter to C<Animal::speak>,
1N/Asince we've stopped using the method arrow. Why did we stop? Well,
1N/Aif we invoke C<< Animal->speak >> there, the first parameter to the
1N/Amethod will be C<"Animal"> not C<"Mouse">, and when time comes for it
1N/Ato call for the C<sound>, it won't have the right class to come back
1N/Ato this package.
1N/A
1N/AInvoking C<Animal::speak> directly is a mess, however. What if
1N/AC<Animal::speak> didn't exist before, and was being inherited from a
1N/Aclass mentioned in C<@Animal::ISA>? Because we are no longer using
1N/Athe method arrow, we get one and only one chance to hit the right
1N/Asubroutine.
1N/A
1N/AAlso note that the C<Animal> classname is now hardwired into the
1N/Asubroutine selection. This is a mess if someone maintains the code,
1N/Achanging C<@ISA> for <Mouse> and didn't notice C<Animal> there in
1N/AC<speak>. So, this is probably not the right way to go.
1N/A
1N/A=head2 Starting the search from a different place
1N/A
1N/AA better solution is to tell Perl to search from a higher place
1N/Ain the inheritance chain:
1N/A
1N/A # same Animal as before
1N/A { package Mouse;
1N/A # same @ISA, &sound as before
1N/A sub speak {
1N/A my $class = shift;
1N/A $class->Animal::speak;
1N/A print "[but you can barely hear it!]\n";
1N/A }
1N/A }
1N/A
1N/AAhh. This works. Using this syntax, we start with C<Animal> to find
1N/AC<speak>, and use all of C<Animal>'s inheritance chain if not found
1N/Aimmediately. And yet the first parameter will be C<$class>, so the
1N/Afound C<speak> method will get C<Mouse> as its first entry, and
1N/Aeventually work its way back to C<Mouse::sound> for the details.
1N/A
1N/ABut this isn't the best solution. We still have to keep the C<@ISA>
1N/Aand the initial search package coordinated. Worse, if C<Mouse> had
1N/Amultiple entries in C<@ISA>, we wouldn't necessarily know which one
1N/Ahad actually defined C<speak>. So, is there an even better way?
1N/A
1N/A=head2 The SUPER way of doing things
1N/A
1N/ABy changing the C<Animal> class to the C<SUPER> class in that
1N/Ainvocation, we get a search of all of our super classes (classes
1N/Alisted in C<@ISA>) automatically:
1N/A
1N/A # same Animal as before
1N/A { package Mouse;
1N/A # same @ISA, &sound as before
1N/A sub speak {
1N/A my $class = shift;
1N/A $class->SUPER::speak;
1N/A print "[but you can barely hear it!]\n";
1N/A }
1N/A }
1N/A
1N/ASo, C<SUPER::speak> means look in the current package's C<@ISA> for
1N/AC<speak>, invoking the first one found. Note that it does I<not> look in
1N/Athe C<@ISA> of C<$class>.
1N/A
1N/A=head2 Where we're at so far...
1N/A
1N/ASo far, we've seen the method arrow syntax:
1N/A
1N/A Class->method(@args);
1N/A
1N/Aor the equivalent:
1N/A
1N/A $a = "Class";
1N/A $a->method(@args);
1N/A
1N/Awhich constructs an argument list of:
1N/A
1N/A ("Class", @args)
1N/A
1N/Aand attempts to invoke
1N/A
1N/A Class::method("Class", @Args);
1N/A
1N/AHowever, if C<Class::method> is not found, then C<@Class::ISA> is examined
1N/A(recursively) to locate a package that does indeed contain C<method>,
1N/Aand that subroutine is invoked instead.
1N/A
1N/AUsing this simple syntax, we have class methods, (multiple)
1N/Ainheritance, overriding, and extending. Using just what we've seen so
1N/Afar, we've been able to factor out common code, and provide a nice way
1N/Ato reuse implementations with variations. This is at the core of what
1N/Aobjects provide, but objects also provide instance data, which we
1N/Ahaven't even begun to cover.
1N/A
1N/A=head2 A horse is a horse, of course of course -- or is it?
1N/A
1N/ALet's start with the code for the C<Animal> class
1N/Aand the C<Horse> class:
1N/A
1N/A { package Animal;
1N/A sub speak {
1N/A my $class = shift;
1N/A print "a $class goes ", $class->sound, "!\n"
1N/A }
1N/A }
1N/A { package Horse;
1N/A @ISA = qw(Animal);
1N/A sub sound { "neigh" }
1N/A }
1N/A
1N/AThis lets us invoke C<< Horse->speak >> to ripple upward to
1N/AC<Animal::speak>, calling back to C<Horse::sound> to get the specific
1N/Asound, and the output of:
1N/A
1N/A a Horse goes neigh!
1N/A
1N/ABut all of our Horse objects would have to be absolutely identical.
1N/AIf I add a subroutine, all horses automatically share it. That's
1N/Agreat for making horses the same, but how do we capture the
1N/Adistinctions about an individual horse? For example, suppose I want
1N/Ato give my first horse a name. There's got to be a way to keep its
1N/Aname separate from the other horses.
1N/A
1N/AWe can do that by drawing a new distinction, called an "instance".
1N/AAn "instance" is generally created by a class. In Perl, any reference
1N/Acan be an instance, so let's start with the simplest reference
1N/Athat can hold a horse's name: a scalar reference.
1N/A
1N/A my $name = "Mr. Ed";
1N/A my $talking = \$name;
1N/A
1N/ASo now C<$talking> is a reference to what will be the instance-specific
1N/Adata (the name). The final step in turning this into a real instance
1N/Ais with a special operator called C<bless>:
1N/A
1N/A bless $talking, Horse;
1N/A
1N/AThis operator stores information about the package named C<Horse> into
1N/Athe thing pointed at by the reference. At this point, we say
1N/AC<$talking> is an instance of C<Horse>. That is, it's a specific
1N/Ahorse. The reference is otherwise unchanged, and can still be used
1N/Awith traditional dereferencing operators.
1N/A
1N/A=head2 Invoking an instance method
1N/A
1N/AThe method arrow can be used on instances, as well as names of
1N/Apackages (classes). So, let's get the sound that C<$talking> makes:
1N/A
1N/A my $noise = $talking->sound;
1N/A
1N/ATo invoke C<sound>, Perl first notes that C<$talking> is a blessed
1N/Areference (and thus an instance). It then constructs an argument
1N/Alist, in this case from just C<($talking)>. (Later we'll see that
1N/Aarguments will take their place following the instance variable,
1N/Ajust like with classes.)
1N/A
1N/ANow for the fun part: Perl takes the class in which the instance was
1N/Ablessed, in this case C<Horse>, and uses that to locate the subroutine
1N/Ato invoke the method. In this case, C<Horse::sound> is found directly
1N/A(without using inheritance), yielding the final subroutine invocation:
1N/A
1N/A Horse::sound($talking)
1N/A
1N/ANote that the first parameter here is still the instance, not the name
1N/Aof the class as before. We'll get C<neigh> as the return value, and
1N/Athat'll end up as the C<$noise> variable above.
1N/A
1N/AIf Horse::sound had not been found, we'd be wandering up the
1N/AC<@Horse::ISA> list to try to find the method in one of the
1N/Asuperclasses, just as for a class method. The only difference between
1N/Aa class method and an instance method is whether the first parameter
1N/Ais an instance (a blessed reference) or a class name (a string).
1N/A
1N/A=head2 Accessing the instance data
1N/A
1N/ABecause we get the instance as the first parameter, we can now access
1N/Athe instance-specific data. In this case, let's add a way to get at
1N/Athe name:
1N/A
1N/A { package Horse;
1N/A @ISA = qw(Animal);
1N/A sub sound { "neigh" }
1N/A sub name {
1N/A my $self = shift;
1N/A $$self;
1N/A }
1N/A }
1N/A
1N/ANow we call for the name:
1N/A
1N/A print $talking->name, " says ", $talking->sound, "\n";
1N/A
1N/AInside C<Horse::name>, the C<@_> array contains just C<$talking>,
1N/Awhich the C<shift> stores into C<$self>. (It's traditional to shift
1N/Athe first parameter off into a variable named C<$self> for instance
1N/Amethods, so stay with that unless you have strong reasons otherwise.)
1N/AThen, C<$self> gets de-referenced as a scalar ref, yielding C<Mr. Ed>,
1N/Aand we're done with that. The result is:
1N/A
1N/A Mr. Ed says neigh.
1N/A
1N/A=head2 How to build a horse
1N/A
1N/AOf course, if we constructed all of our horses by hand, we'd most
1N/Alikely make mistakes from time to time. We're also violating one of
1N/Athe properties of object-oriented programming, in that the "inside
1N/Aguts" of a Horse are visible. That's good if you're a veterinarian,
1N/Abut not if you just like to own horses. So, let's let the Horse class
1N/Abuild a new horse:
1N/A
1N/A { package Horse;
1N/A @ISA = qw(Animal);
1N/A sub sound { "neigh" }
1N/A sub name {
1N/A my $self = shift;
1N/A $$self;
1N/A }
1N/A sub named {
1N/A my $class = shift;
1N/A my $name = shift;
1N/A bless \$name, $class;
1N/A }
1N/A }
1N/A
1N/ANow with the new C<named> method, we can build a horse:
1N/A
1N/A my $talking = Horse->named("Mr. Ed");
1N/A
1N/ANotice we're back to a class method, so the two arguments to
1N/AC<Horse::named> are C<Horse> and C<Mr. Ed>. The C<bless> operator
1N/Anot only blesses C<$name>, it also returns the reference to C<$name>,
1N/Aso that's fine as a return value. And that's how to build a horse.
1N/A
1N/AWe've called the constructor C<named> here, so that it quickly denotes
1N/Athe constructor's argument as the name for this particular C<Horse>.
1N/AYou can use different constructors with different names for different
1N/Aways of "giving birth" to the object (like maybe recording its
1N/Apedigree or date of birth). However, you'll find that most people
1N/Acoming to Perl from more limited languages use a single constructor
1N/Anamed C<new>, with various ways of interpreting the arguments to
1N/AC<new>. Either style is fine, as long as you document your particular
1N/Away of giving birth to an object. (And you I<were> going to do that,
1N/Aright?)
1N/A
1N/A=head2 Inheriting the constructor
1N/A
1N/ABut was there anything specific to C<Horse> in that method? No. Therefore,
1N/Ait's also the same recipe for building anything else that inherited from
1N/AC<Animal>, so let's put it there:
1N/A
1N/A { package Animal;
1N/A sub speak {
1N/A my $class = shift;
1N/A print "a $class goes ", $class->sound, "!\n"
1N/A }
1N/A sub name {
1N/A my $self = shift;
1N/A $$self;
1N/A }
1N/A sub named {
1N/A my $class = shift;
1N/A my $name = shift;
1N/A bless \$name, $class;
1N/A }
1N/A }
1N/A { package Horse;
1N/A @ISA = qw(Animal);
1N/A sub sound { "neigh" }
1N/A }
1N/A
1N/AAhh, but what happens if we invoke C<speak> on an instance?
1N/A
1N/A my $talking = Horse->named("Mr. Ed");
1N/A $talking->speak;
1N/A
1N/AWe get a debugging value:
1N/A
1N/A a Horse=SCALAR(0xaca42ac) goes neigh!
1N/A
1N/AWhy? Because the C<Animal::speak> routine is expecting a classname as
1N/Aits first parameter, not an instance. When the instance is passed in,
1N/Awe'll end up using a blessed scalar reference as a string, and that
1N/Ashows up as we saw it just now.
1N/A
1N/A=head2 Making a method work with either classes or instances
1N/A
1N/AAll we need is for a method to detect if it is being called on a class
1N/Aor called on an instance. The most straightforward way is with the
1N/AC<ref> operator. This returns a string (the classname) when used on a
1N/Ablessed reference, and C<undef> when used on a string (like a
1N/Aclassname). Let's modify the C<name> method first to notice the change:
1N/A
1N/A sub name {
1N/A my $either = shift;
1N/A ref $either
1N/A ? $$either # it's an instance, return name
1N/A : "an unnamed $either"; # it's a class, return generic
1N/A }
1N/A
1N/AHere, the C<?:> operator comes in handy to select either the
1N/Adereference or a derived string. Now we can use this with either an
1N/Ainstance or a class. Note that I've changed the first parameter
1N/Aholder to C<$either> to show that this is intended:
1N/A
1N/A my $talking = Horse->named("Mr. Ed");
1N/A print Horse->name, "\n"; # prints "an unnamed Horse\n"
1N/A print $talking->name, "\n"; # prints "Mr Ed.\n"
1N/A
1N/Aand now we'll fix C<speak> to use this:
1N/A
1N/A sub speak {
1N/A my $either = shift;
1N/A print $either->name, " goes ", $either->sound, "\n";
1N/A }
1N/A
1N/AAnd since C<sound> already worked with either a class or an instance,
1N/Awe're done!
1N/A
1N/A=head2 Adding parameters to a method
1N/A
1N/ALet's train our animals to eat:
1N/A
1N/A { package Animal;
1N/A sub named {
1N/A my $class = shift;
1N/A my $name = shift;
1N/A bless \$name, $class;
1N/A }
1N/A sub name {
1N/A my $either = shift;
1N/A ref $either
1N/A ? $$either # it's an instance, return name
1N/A : "an unnamed $either"; # it's a class, return generic
1N/A }
1N/A sub speak {
1N/A my $either = shift;
1N/A print $either->name, " goes ", $either->sound, "\n";
1N/A }
1N/A sub eat {
1N/A my $either = shift;
1N/A my $food = shift;
1N/A print $either->name, " eats $food.\n";
1N/A }
1N/A }
1N/A { package Horse;
1N/A @ISA = qw(Animal);
1N/A sub sound { "neigh" }
1N/A }
1N/A { package Sheep;
1N/A @ISA = qw(Animal);
1N/A sub sound { "baaaah" }
1N/A }
1N/A
1N/AAnd now try it out:
1N/A
1N/A my $talking = Horse->named("Mr. Ed");
1N/A $talking->eat("hay");
1N/A Sheep->eat("grass");
1N/A
1N/Awhich prints:
1N/A
1N/A Mr. Ed eats hay.
1N/A an unnamed Sheep eats grass.
1N/A
1N/AAn instance method with parameters gets invoked with the instance,
1N/Aand then the list of parameters. So that first invocation is like:
1N/A
1N/A Animal::eat($talking, "hay");
1N/A
1N/A=head2 More interesting instances
1N/A
1N/AWhat if an instance needs more data? Most interesting instances are
1N/Amade of many items, each of which can in turn be a reference or even
1N/Aanother object. The easiest way to store these is often in a hash.
1N/AThe keys of the hash serve as the names of parts of the object (often
1N/Acalled "instance variables" or "member variables"), and the
1N/Acorresponding values are, well, the values.
1N/A
1N/ABut how do we turn the horse into a hash? Recall that an object was
1N/Aany blessed reference. We can just as easily make it a blessed hash
1N/Areference as a blessed scalar reference, as long as everything that
1N/Alooks at the reference is changed accordingly.
1N/A
1N/ALet's make a sheep that has a name and a color:
1N/A
1N/A my $bad = bless { Name => "Evil", Color => "black" }, Sheep;
1N/A
1N/Aso C<< $bad->{Name} >> has C<Evil>, and C<< $bad->{Color} >> has
1N/AC<black>. But we want to make C<< $bad->name >> access the name, and
1N/Athat's now messed up because it's expecting a scalar reference. Not
1N/Ato worry, because that's pretty easy to fix up:
1N/A
1N/A ## in Animal
1N/A sub name {
1N/A my $either = shift;
1N/A ref $either ?
1N/A $either->{Name} :
1N/A "an unnamed $either";
1N/A }
1N/A
1N/AAnd of course C<named> still builds a scalar sheep, so let's fix that
1N/Aas well:
1N/A
1N/A ## in Animal
1N/A sub named {
1N/A my $class = shift;
1N/A my $name = shift;
1N/A my $self = { Name => $name, Color => $class->default_color };
1N/A bless $self, $class;
1N/A }
1N/A
1N/AWhat's this C<default_color>? Well, if C<named> has only the name,
1N/Awe still need to set a color, so we'll have a class-specific initial color.
1N/AFor a sheep, we might define it as white:
1N/A
1N/A ## in Sheep
1N/A sub default_color { "white" }
1N/A
1N/AAnd then to keep from having to define one for each additional class,
1N/Awe'll define a "backstop" method that serves as the "default default",
1N/Adirectly in C<Animal>:
1N/A
1N/A ## in Animal
1N/A sub default_color { "brown" }
1N/A
1N/ANow, because C<name> and C<named> were the only methods that
1N/Areferenced the "structure" of the object, the rest of the methods can
1N/Aremain the same, so C<speak> still works as before.
1N/A
1N/A=head2 A horse of a different color
1N/A
1N/ABut having all our horses be brown would be boring. So let's add a
1N/Amethod or two to get and set the color.
1N/A
1N/A ## in Animal
1N/A sub color {
1N/A $_[0]->{Color}
1N/A }
1N/A sub set_color {
1N/A $_[0]->{Color} = $_[1];
1N/A }
1N/A
1N/ANote the alternate way of accessing the arguments: C<$_[0]> is used
1N/Ain-place, rather than with a C<shift>. (This saves us a bit of time
1N/Afor something that may be invoked frequently.) And now we can fix
1N/Athat color for Mr. Ed:
1N/A
1N/A my $talking = Horse->named("Mr. Ed");
1N/A $talking->set_color("black-and-white");
1N/A print $talking->name, " is colored ", $talking->color, "\n";
1N/A
1N/Awhich results in:
1N/A
1N/A Mr. Ed is colored black-and-white
1N/A
1N/A=head2 Summary
1N/A
1N/ASo, now we have class methods, constructors, instance methods,
1N/Ainstance data, and even accessors. But that's still just the
1N/Abeginning of what Perl has to offer. We haven't even begun to talk
1N/Aabout accessors that double as getters and setters, destructors,
1N/Aindirect object notation, subclasses that add instance data, per-class
1N/Adata, overloading, "isa" and "can" tests, C<UNIVERSAL> class, and so
1N/Aon. That's for the rest of the Perl documentation to cover.
1N/AHopefully, this gets you started, though.
1N/A
1N/A=head1 SEE ALSO
1N/A
1N/AFor more information, see L<perlobj> (for all the gritty details about
1N/APerl objects, now that you've seen the basics), L<perltoot> (the
1N/Atutorial for those who already know objects), L<perltooc> (dealing
1N/Awith class data), L<perlbot> (for some more tricks), and books such as
1N/ADamian Conway's excellent I<Object Oriented Perl>.
1N/A
1N/ASome modules which might prove interesting are Class::Accessor,
1N/AClass::Class, Class::Contract, Class::Data::Inheritable,
1N/AClass::MethodMaker and Tie::SecureHash
1N/A
1N/A=head1 COPYRIGHT
1N/A
1N/ACopyright (c) 1999, 2000 by Randal L. Schwartz and Stonehenge
1N/AConsulting Services, Inc. Permission is hereby granted to distribute
1N/Athis document intact with the Perl distribution, and in accordance
1N/Awith the licenses of the Perl distribution; derived documents must
1N/Ainclude this copyright notice intact.
1N/A
1N/APortions of this text have been derived from Perl Training materials
1N/Aoriginally appearing in the I<Packages, References, Objects, and
1N/AModules> course taught by instructors for Stonehenge Consulting
1N/AServices, Inc. and used with permission.
1N/A
1N/APortions of this text have been derived from materials originally
1N/Aappearing in I<Linux Magazine> and used with permission.