Perl Brasil

Pesquisar

Documentação

Planeta

Eventos

Comunidade

r7 - 31 May 2008 - MarcoLima


NOME

perlboot - Tutorial de Orientação a Objetos para Iniciantes

voltar para o topo


DESCRIÇÃO

Se você não estiver familiarizado com objetos de outras linguagens, as demais documentações sobre objetos em Perl podem ser um pouco assustadoras, tal como the perlobj manpage,uma referência básica sobre o uso de objetos, e the perltoot manpage, que introduz os leitores às peculiaridades do sistema de orientação a objetos do Perl de modo tutorial. Então, vamos realizar uma abordagem diferente, presumindo a inexistência prévia de experiência com objetos. Será mais fácil se você conhecer subrotinas (the perlsub manpage), referências (the perlref manpage et. seq.), e pacotes (the perlmod manpage), então se familiarize com estes conceitos primeiro se você já não o fez.

Se pudéssemos falar com os animais...

Deixemos os animais falar por um momento:
    sub Cow::speak {
      print "a Cow goes moooo!\n";
    }
    sub Horse::speak {
      print "a Horse goes neigh!\n";
    }
    sub Sheep::speak {
      print "a Sheep goes baaaah!\n"
    }
    Cow::speak;
    Horse::speak;
    Sheep::speak;
Isto resulta em:
    a Cow goes moooo!
    a Horse goes neigh!
    a Sheep goes baaaah!
Nada espetacular aqui. Simples subrotinas, contudo de pacotes separados, e chamadas utilizando o nome completo do pacote. Então vamos criar um pasto inteiro:
    # Cow::speak, Horse::speak, Sheep::speak as before
    @pasture = qw(Cow Cow Horse Sheep Sheep);
    foreach $animal (@pasture) {
      &{$animal."::speak"};
    }
Isto resulta em:
    a Cow goes moooo!
    a Cow goes moooo!
    a Horse goes neigh!
    a Sheep goes baaaah!
    a Sheep goes baaaah!
Uau. Esta dereferenciação simbólica de coderefs aí é bastante indecente. Estamos contando com o modo no strict subs, certamente não recomendado para programas maiores. E porque isto foi necessário? Porque o nome do pacote parece ser inseparável do nome da subrotina que queremos invocar dentro deste pacote. Ou será que é?

Introduzindo a seta de invocação de métodos

Por enquanto, digamos que Class->method invoca a subrotina method no pacote Class. (Aqui, ``Class'' é utilizada no sentido de ``categoria'', não no seu sentido ``escolástico''.) Esta afirmação não está completamente correta, mas vamos tomar um passo de cada vez. Agora vamos utilizar assim:
    # Cow::speak, Horse::speak, Sheep::speak as before
    Cow->speak;
    Horse->speak;
    Sheep->speak;
E novamente, isto resulta em:
    a Cow goes moooo!
    a Horse goes neigh!
    a Sheep goes baaaah!
Ainda não está divertido. A mesma quantidade de caracteres, todos constantes, nenhuma variável. Porém, as partes agora são separáveis. Veja:
    $a = "Cow";
    $a->speak; # invokes Cow->speak
Ahh! Agora que o nome do pacote foi separado do nome da subrotina, podemos usar um nome de pacote variável. E desta vez, obtemos algo que funciona mesmo quando use strict refs está habilitado.

Invocando um celeiro

Vamos pegar aquela nova seta de invocação e colocá-la de volta no exemplo do celeiro:
    sub Cow::speak {
      print "a Cow goes moooo!\n";
    }
    sub Horse::speak {
      print "a Horse goes neigh!\n";
    }
    sub Sheep::speak {
      print "a Sheep goes baaaah!\n"
    }
    @pasture = qw(Cow Cow Horse Sheep Sheep);
    foreach $animal (@pasture) {
      $animal->speak;
    }
Pronto! Agora temos todos os animais falando, e seguramente, sem o uso de coderefs simbólicos. Mas observe todo esse código em comum. Cada uma das rotinas speak possuem estrutura similar: um operador print e uma string que contém texto em comum. Seria legal se pudéssemos fatorar as comunalidades, no caso de decidirmos mudar para says invés de goes. E na verdade nós temos uma forma de fazer isso sem muito alarde, mas temos que ouvir um pouco mais sobre o que a seta de invocação de métodos está realmente fazendo por nós.

O parâmetro extra de invocação de método

A invocação de:
    Class->method(@args)
tenta invocar a subrotina Class::method como:
    Class::method("Class", @args);
(Se a subrotina não for encontrada, a ``herança'' entra em cena, mas falaremos sobre isso depois.) Isto significa que nós obtemos o nome da classe como o primeiro parâmetro (o único, se não for passado nenhum argumento). Então podemos reescrever a subrotina falante de Sheep como:
    sub Sheep::speak {
      my $class = shift;
      print "a $class goes baaaah!\n";
    }
E os outros animais saem de forma similar:
    sub Cow::speak {
      my $class = shift;
      print "a $class goes moooo!\n";
    }
    sub Horse::speak {
      my $class = shift;
      print "a $class goes neigh!\n";
    }
Em cada caso, $class terá o valor apropriado para aquela subrotina. Porém, novamente, temos muita estrutura similar. Poderemos fatorar isso mais ainda? Sim, chamando outro método na mesma classe.

Chamando um segundo método para simplificar as coisas

Dentro de speak, vamos chamar um método ajudante chamado sound. Este método provê o texto constante para o som em sí.
    { package Cow;
      sub sound { "moooo" }
      sub speak {
        my $class = shift;
        print "a $class goes ", $class->sound, "!\n"
      }
    }
Agora, quando chamarmos Cow->speak, obtemos uma $class de Cow em speak. Isto por sua vez seleciona o método Cow->sound, que retorna moooo. Mas quão diferente seria para para o Horse?
    { package Horse;
      sub sound { "neigh" }
      sub speak {
        my $class = shift;
        print "a $class goes ", $class->sound, "!\n"
      }
    }
Apenas o nome do pacote e a mudança específica do som. Então será que podemos compartilhar a definição de speak entre a Cow e o Horse? Sim, através de Herança!

Herdando as gargantas

Definiremos um pacote de subrotinas em comum chamado Animal, com a definição de speak:
    { package Animal;
      sub speak {
        my $class = shift;
        print "a $class goes ", $class->sound, "!\n"
      }
    }
Em seguida, dizemos que cada animal herda de Animal, junto com o som específico do animal:
    { package Cow;
      @ISA = qw(Animal);
      sub sound { "moooo" }
    }
Observe o array adicional @ISA. Chegaremos lá em um minute. Mas o que acontece quando invocamos <Core-speak>> agora? Primeiro, Perl constrói a lista de argumentos. Neste caso, somente Cow. Então Perl procura por Cow::speak. Perl checa pela herança do array @Cow::ISA. Ele está lá e contêm o nome Animal. Perl checa o próximo por speak em Animal em vez de, como em Animal::speak. And that's found, so Perl invokes that subroutine with the already frozen argument list. Inside the Animal::speak subroutine, $class becomes Cow (the first argument). So when we get to the step of invoking $class->sound, it'll be looking for Cow->sound, which gets it on the first try without looking at @ISA. Success!

Um pequena nota sobre @ISA

Essa variável mágica @ISA (pronunciada ``is a'' = ``é um(a)'' não ``ice-uh''), tinha sido declarada que Cow ``is a'' = ``é um(a)'' Animal. Perceba que ele é um array, não um valor singular simples, porque nas raras ocasiões, faz sentido ter mais de uma classe pai que busca métodos não encontrados. Se Animal também tinha um @ISA, então nós tínhamos checado lá também. A busca é recursiva, primeira extensão, esquerda para direita em cada<@ISA>. Normalmente, cada @ISA tinha apenas um elemento(múltiplos elementos significa múltiplas heranças e múltiplas dores de cabeças), apenas nós pegamos uma boa árvore de herança. When we turn on use strict, we'll get complaints on @ISA, since it's not a variable containing an explicit package name, nor is it a lexical (``my'') variable. We can't make it a lexical variable though (it has to belong to the package to be found by the inheritance mechanism), so there's a couple of straightforward ways to handle that. The easiest is to just spell the package name out:
    @Cow::ISA = qw(Animal);
Or allow it as an implicitly named package variable:
    package Cow;
    use vars qw(@ISA);
    @ISA = qw(Animal);
If you're bringing in the class from outside, via an object-oriented module, you change:
    package Cow;
    use Animal;
    use vars qw(@ISA);
    @ISA = qw(Animal);
into just:
    package Cow;
    use base qw(Animal);
And that's pretty darn compact.

Overriding the methods

Let's add a mouse, which can barely be heard:
    # Animal package from before
    { package Mouse;
      @ISA = qw(Animal);
      sub sound { "squeak" }
      sub speak {
        my $class = shift;
        print "a $class goes ", $class->sound, "!\n";
        print "[but you can barely hear it!]\n";
      }
    }
    Mouse->speak;
which results in:
    a Mouse goes squeak!
    [but you can barely hear it!]
Here, Mouse has its own speaking routine, so Mouse->speak doesn't immediately invoke Animal->speak. This is known as ``overriding''. In fact, we didn't even need to say that a Mouse was an Animal at all, since all of the methods needed for speak are completely defined with Mouse. But we've now duplicated some of the code from Animal->speak, and this can once again be a maintenance headache. So, can we avoid that? Can we say somehow that a Mouse does everything any other Animal does, but add in the extra comment? Sure! First, we can invoke the Animal::speak method directly:
    # Animal package from before
    { package Mouse;
      @ISA = qw(Animal);
      sub sound { "squeak" }
      sub speak {
        my $class = shift;
        Animal::speak($class);
        print "[but you can barely hear it!]\n";
      }
    }
Note that we have to include the $class parameter (almost surely the value of "Mouse") as the first parameter to Animal::speak, since we've stopped using the method arrow. Why did we stop? Well, if we invoke Animal->speak there, the first parameter to the method will be "Animal" not "Mouse", and when time comes for it to call for the sound, it won't have the right class to come back to this package. Invoking Animal::speak directly is a mess, however. What if Animal::speak didn't exist before, and was being inherited from a class mentioned in @Animal::ISA? Because we are no longer using the method arrow, we get one and only one chance to hit the right subroutine. Also note that the Animal classname is now hardwired into the subroutine selection. This is a mess if someone maintains the code, changing @ISA for <Mouse> and didn't notice Animal there in speak. So, this is probably not the right way to go.

Starting the search from a different place

A better solution is to tell Perl to search from a higher place in the inheritance chain:
    # same Animal as before
    { package Mouse;
      # same @ISA, &sound as before
      sub speak {
        my $class = shift;
        $class->Animal::speak;
        print "[but you can barely hear it!]\n";
      }
    }
Ahh. This works. Using this syntax, we start with Animal to find speak, and use all of Animal's inheritance chain if not found immediately. And yet the first parameter will be $class, so the found speak method will get Mouse as its first entry, and eventually work its way back to Mouse::sound for the details. But this isn't the best solution. We still have to keep the @ISA and the initial search package coordinated. Worse, if Mouse had multiple entries in @ISA, we wouldn't necessarily know which one had actually defined speak. So, is there an even better way?

The SUPER way of doing things

By changing the Animal class to the SUPER class in that invocation, we get a search of all of our super classes (classes listed in @ISA) automatically:
    # same Animal as before
    { package Mouse;
      # same @ISA, &sound as before
      sub speak {
        my $class = shift;
        $class->SUPER::speak;
        print "[but you can barely hear it!]\n";
      }
    }
So, SUPER::speak means look in the current package's @ISA for speak, invoking the first one found. Note that it does not look in the @ISA of $class.

Where we're at so far...

So far, we've seen the method arrow syntax:
  Class->method(@args);
or the equivalent:
  $a = "Class";
  $a->method(@args);
which constructs an argument list of:
  ("Class", @args)
and attempts to invoke
  Class::method("Class", @Args);
However, if Class::method is not found, then @Class::ISA is examined (recursively) to locate a package that does indeed contain method, and that subroutine is invoked instead. Using this simple syntax, we have class methods, (multiple) inheritance, overriding, and extending. Using just what we've seen so far, we've been able to factor out common code, and provide a nice way to reuse implementations with variations. This is at the core of what objects provide, but objects also provide instance data, which we haven't even begun to cover.

A horse is a horse, of course of course -- or is it?

Let's start with the code for the Animal class and the Horse class:
  { 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 of our Horse objects would have to be absolutely identical. If I add a subroutine, all horses automatically share it. That's great for making horses the same, but how do we capture the distinctions about an individual horse? For example, suppose I want to give my first horse a name. There's got to be a way to keep its name separate from the other horses. We can do that by drawing a new distinction, called an ``instance''. An ``instance'' is generally created by a class. In Perl, any reference can be an instance, so let's start with the simplest reference that can hold a horse's name: a scalar reference.
  my $name = "Mr. Ed";
  my $talking = \$name;
So now $talking is a reference to what will be the instance-specific data (the name). The final step in turning this into a real instance is with a special operator called bless:
  bless $talking, Horse;
This operator stores information about the package named Horse into the thing pointed at by the reference. At this point, we say $talking is an instance of Horse. That is, it's a specific horse. The reference is otherwise unchanged, and can still be used with traditional dereferencing operators.

Invoking an instance method

The method arrow can be used on instances, as well as names of packages (classes). So, let's get the sound that $talking makes:
  my $noise = $talking->sound;
To invoke sound, Perl first notes that $talking is a blessed reference (and thus an instance). It then constructs an argument list, in this case from just ($talking). (Later we'll see that arguments will take their place following the instance variable, just like with classes.) Now for the fun part: Perl takes the class in which the instance was blessed, in this case Horse, and uses that to locate the subroutine to invoke the method. In this case, Horse::sound is found directly (without using inheritance), yielding the final subroutine invocation:
  Horse::sound($talking)
Note that the first parameter here is still the instance, not the name of the class as before. We'll get neigh as the return value, and that'll end up as the $noise variable above. If Horse::sound had not been found, we'd be wandering up the @Horse::ISA list to try to find the method in one of the superclasses, just as for a class method. The only difference between a class method and an instance method is whether the first parameter is an instance (a blessed reference) or a class name (a string).

Accessing the instance data

Because we get the instance as the first parameter, we can now access the instance-specific data. In this case, let's add a way to get at the name:
  { package Horse;
    @ISA = qw(Animal);
    sub sound { "neigh" }
    sub name {
      my $self = shift;
      $$self;
    }
  }
Now we call for the name:
  print $talking->name, " says ", $talking->sound, "\n";
Inside Horse::name, the @_ array contains just $talking, which the shift stores into $self. (It's traditional to shift the first parameter off into a variable named $self for instance methods, so stay with that unless you have strong reasons otherwise.) Then, $self gets de-referenced as a scalar ref, yielding Mr. Ed, and we're done with that. The result is:
  Mr. Ed says neigh.

How to build a horse

Of course, if we constructed all of our horses by hand, we'd most likely make mistakes from time to time. We're also violating one of the properties of object-oriented programming, in that the ``inside guts`` of a Horse are visible. That's good if you're a veterinarian, but not if you just like to own horses. So, let's let the Horse class build a new horse:
  { package Horse;
    @ISA = qw(Animal);
    sub sound { "neigh" }
    sub name {
      my $self = shift;
      $$self;
    }
    sub named {
      my $class = shift;
      my $name = shift;
      bless \$name, $class;
    }
  }
Now with the new named method, we can build a horse:
  my $talking = Horse->named("Mr. Ed");
Notice we're back to a class method, so the two arguments to Horse::named are Horse and Mr. Ed. The bless operator not only blesses $name, it also returns the reference to $name, so that's fine as a return value. And that's how to build a horse. We've called the constructor named here, so that it quickly denotes the constructor's argument as the name for this particular Horse. You can use different constructors with different names for different ways of ``giving birth'' to the object (like maybe recording its pedigree or date of birth). However, you'll find that most people coming to Perl from more limited languages use a single constructor named new, with various ways of interpreting the arguments to new. Either style is fine, as long as you document your particular way of giving birth to an object. (And you were going to do that, right?)

Inheriting the constructor

But was there anything specific to Horse in that method? No. Therefore, it's also the same recipe for building anything else that inherited from Animal, so let's put it there:
  { package Animal;
    sub speak {
      my $class = shift;
      print "a $class goes ", $class->sound, "!\n"
    }
    sub name {
      my $self = shift;
      $$self;
    }
    sub named {
      my $class = shift;
      my $name = shift;
      bless \$name, $class;
    }
  }
  { package Horse;
    @ISA = qw(Animal);
    sub sound { "neigh" }
  }
Ahh, but what happens if we invoke speak on an instance?
  my $talking = Horse->named("Mr. Ed");
  $talking->speak;
We get a debugging value:
  a Horse=SCALAR(0xaca42ac) goes neigh!
Why? Because the Animal::speak routine is expecting a classname as its first parameter, not an instance. When the instance is passed in, we'll end up using a blessed scalar reference as a string, and that shows up as we saw it just now.

Making a method work with either classes or instances

All we need is for a method to detect if it is being called on a class or called on an instance. The most straightforward way is with the ref operator. This returns a string (the classname) when used on a blessed reference, and undef when used on a string (like a classname). Let's 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 comes in handy to select either the dereference or a derived string. Now we can use this with either an instance or a class. Note that I've changed the first parameter holder to $either to show that this is intended:
  my $talking = Horse->named("Mr. Ed");
  print Horse->name, "\n"; # prints "an unnamed Horse\n"
  print $talking->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";
  }
And since sound already worked with either a class or an instance, we're done!

Adding parameters to a method

Let's train our animals to eat:
  { package Animal;
    sub named {
      my $class = shift;
      my $name = shift;
      bless \$name, $class;
    }
    sub name {
      my $either = shift;
      ref $either
        ? $$either # it's an instance, return name
        : "an unnamed $either"; # it's a class, return generic
    }
    sub speak {
      my $either = shift;
      print $either->name, " goes ", $either->sound, "\n";
    }
    sub eat {
      my $either = shift;
      my $food = shift;
      print $either->name, " eats $food.\n";
    }
  }
  { package Horse;
    @ISA = qw(Animal);
    sub sound { "neigh" }
  }
  { package Sheep;
    @ISA = qw(Animal);
    sub sound { "baaaah" }
  }
And now try it out:
  my $talking = Horse->named("Mr. Ed");
  $talking->eat("hay");
  Sheep->eat("grass");
which prints:
  Mr. Ed eats hay.
  an unnamed Sheep eats grass.
An instance method with parameters gets invoked with the instance, and then the list of parameters. So that first invocation is like:
  Animal::eat($talking, "hay");

More interesting instances

What if an instance needs more data? Most interesting instances are made of many items, each of which can in turn be a reference or even another object. The easiest way to store these is often in a hash. The keys of the hash serve as the names of parts of the object (often called ``instance variables'' or ``member variables''), and the corresponding values are, well, the values. But how do we turn the horse into a hash? Recall that an object was any blessed reference. We can just as easily make it a blessed hash reference as a blessed scalar reference, as long as everything that looks at the reference is changed accordingly. Let's make a sheep that has a name and a color:
  my $bad = bless { Name => "Evil", Color => "black" }, Sheep;
so $bad->{Name} has Evil, and $bad->{Color} has black. But we want to make $bad->name access the name, and that's now messed up because it's expecting a scalar reference. Not to worry, because that's pretty easy to fix up:
  ## in Animal
  sub name {
    my $either = shift;
    ref $either ?
      $either->{Name} :
      "an unnamed $either";
  }
And of course named still builds a scalar sheep, so let's fix that as well:
  ## in Animal
  sub named {
    my $class = shift;
    my $name = shift;
    my $self = { Name => $name, Color => $class->default_color };
    bless $self, $class;
  }
What's this default_color? Well, if named has only the name, we still need to set a color, so we'll have a class-specific initial color. For a sheep, we might define it as white:
  ## in Sheep
  sub default_color { "white" }
And then to keep from having to define one for each additional class, we'll define a ``backstop'' method that serves as the ``default default'', directly in Animal:
  ## in Animal
  sub default_color { "brown" }
Now, because name and named were the only methods that referenced the ``structure'' of the object, the rest of the methods can remain the same, so speak still works as before.

A horse of a different color

But having all our horses be brown would be boring. So let's add a method or two to get and set the color.
  ## in Animal
  sub color {
    $_[0]->{Color}
  }
  sub set_color {
    $_[0]->{Color} = $_[1];
  }
Note the alternate way of accessing the arguments: $_[0] is used in-place, rather than with a shift. (This saves us a bit of time for something that may be invoked frequently.) And now we can fix that color for Mr. Ed:
  my $talking = Horse->named("Mr. Ed");
  $talking->set_color("black-and-white");
  print $talking->name, " is colored ", $talking->color, "\n";
which results in:
  Mr. Ed is colored black-and-white

Summary

So, now we have class methods, constructors, instance methods, instance data, and even accessors. But that's still just the beginning of what Perl has to offer. We haven't even begun to talk about accessors that double as getters and setters, destructors, indirect object notation, subclasses that add instance data, per-class data, overloading, ``isa'' and ``can'' tests, UNIVERSAL class, and so on. That's for the rest of the Perl documentation to cover. Hopefully, this gets you started, though.

voltar para o topo


SEE ALSO

For more information, see the perlobj manpage (for all the gritty details about Perl objects, now that you've seen the basics), the perltoot manpage (the tutorial for those who already know objects), the perltooc manpage (dealing with class data), the perlbot manpage (for some more tricks), and books such as Damian Conway's excellent Object Oriented Perl. Some modules which might prove interesting are Class::Accessor, Class::Class, Class::Contract, Class::Data::Inheritable, Class::MethodMaker and Tie::SecureHash

voltar para o topo


COPYRIGHT

Copyright (c) 1999, 2000 by Randal L. Schwartz and Stonehenge Consulting Services, Inc. Permission is hereby granted to distribute this document intact with the Perl distribution, and in accordance with the licenses of the Perl distribution; derived documents must include this copyright notice intact. Portions of this text have been derived from Perl Training materials originally appearing in the Packages, References, Objects, and Modules> course taught by instructors for Stonehenge Consulting Services, Inc. and used with permission. Portions of this text have been derived from materials originally appearing in Linux Magazine and used with permission.

voltar para o topo


TRADUZINDO

EdenCardim; MarcoLima

voltar para o topo