Essential OO programming language features
Generally, three language properties are essential to an OO programming language. They are inheritance, polymorphism, and encapsulation.
Perl supports inheritance. Inheritance is applied when one object (the child) uses another as a starting point (the parent), and then modifies its properties and behavior as necessary. This child-parent relationship is essential to OOP, as it makes it possible to build objects upon other objects. This reuse is one of the benefits of OOP that make it a favorite with programmers.
There are two types of inheritance: single and multiple. Single inheritance requires a child to have only one parent, while multiple inheritance is more liberal (in programming, as in real life, having more than two parents can cause confusion and a difficult childhood, so don't overdo multiple inheritance). Perl supports multiple inheritance, though two or more parents are rarely seen in practice.
Polymorphism (from Greek, meaning "many forms") is the technique of making an object be seen as another. This is a little bit complex, so let's use an example. Say you have a sheep farm with four sheep (Ovis aries), but you just bought two goats (Capra hircus) and one German Shepherd dog (Canis lupis familiaris). How many animals do you own? You'd add up all the sheep, goats, and the dog to get seven. You just applied polymorphism, treating three specific kinds of animal as one generic type of "Animal" for the purposes of counting. If you imagine sheep, goats, and dogs as mammalian species, this is an easy leap of faith. Biologists use polymorphism in this fashion every day, and programmers are famous for stealing -- I mean, "reusing" -- good ideas from other sciences.
In Perl, polymorphism is fully supported. It is not
used very frequently, as Perl programmers seem to prefer to modify
generic behavior with object properties rather than by modifying
inherited behavior. This means that you are more likely to see code
that creates three
IO::Socket::INET objects, one for UDP
packets reception and transmission on port 234, one for TCP packet
reception on port 80, and one for TCP packet transmission on port 1024,
than you are to see code that uses
IO::Socket::INET::UDPTransceiver for the first case,
IO::Socket::INET::TCPReceiver for the second, and
for the third. In biological terms, this would be like saying that dogs
and goats are just mammals, but the goat has the Capra flag turned on,
while the dog has the Canis flag turned on.
OOP purists feel that everything should be classified properly, but Perl programmers are not purists by any means. They tend to be more relaxed about OOP rules, which makes them more fun at parties than OOP purists.
Encapsulation refers to enclosing object behavior and properties
in a manner that makes them inaccessible to users unless the object
author wants to allow that access. That way, object users can't do
things they are not supposed to do, access data they are not supposed
to access, and generally be a pest. Perl allows encapsulation in the
usual laid-back way. See Listing 1.