vikrams perl by dvinkster


More Info
									An object-oriented programming language needs to qualify three fundamental properties, namely

encapsulation, inheritance and polymorphism. These principles are fundamental to support the

virtues of object-oriented programming.

encapsulation refers to the practice of hiding data structures which represent

the internal state of an object from access except through public methods of that object. Basically, that

implies you should not change a property by directly modifying the internals of an object. Instead, you

should modify it through the interface of the object. An interface is what is expected to be seen from

outside of the object. In Perl, this includes all object methods. For example, while an airplane object

maintains the altitude property, you should not modify its value directly. Instead, you invoke the

methods ascend() and descend() to change its altitude because some actions need to be taken before

climbing up or going down, which are accounted for by the two methods. By interacting with methods

through the interface, the methods can check whether the operation is valid before committing any

changes to the object.

Inheritance allows a class (subclass) to inherit methods from another class (superclass). For example, a

Helicopter class may inherit from the Airplane class the ascend() and descend() methods, while

adding a stationary() method which an airplane doesn't have and is a characteristic of a helicopter.

By inheriting from another class, you don't need to write the inherited code again (unless you would

like to override them). Inheritance allows creation of code that can be easily reused.

Polymorphism is a more abstract notion that cannot be easily explained without resorting to examples.

The principle is that it allows programmers to use a consistent interface for method invocation on

objects of different classes.

Creating and unsing perl class
Don't forget to put the 1; at the end of the module. Omitting this
results in a compile-time error.
Before you use a module you should _rst import it into your program.

You may import a module in one of two ways. The _rst way is to pass
7.3 OOP Primer: Statistics 111
Package Name File Path (relative to @INC)
Crypt::CBC Crypt/
Table 7.1: Relationship of Package Names and File Placements
the path to the module (relative to @INC) in quotes to the require function. For example,
"Crypt/"; Another way is to just specify the package name without quotes, for
require Crypt::CBC; However, the use function is preferred in general for Perl modules.
The syntax
of use is
It is semantically equivalent to
require MODULE;
import MODULE LIST; # indirect object syntax
which not only imports the module MODULE at compile time, it also imports the symbols
speci_ed by
LIST into the current namespace (package) using the import class method. import is a
special class
method that may be de_ned by a class author. Many modules do not have any symbols to be
however, some of them do. If the method cannot be found, it is silently bypassed. For example,
users of
the CGI::Carp module may import from it the fatalsToBrowser symbol, which is actually
the name
of a subroutine that generates a page in HTML describing the details when an error occurs,
which is
then returned to the viewer's browser. This is used by CGI scripts to easily trap runtime errors.
use statement required is
use CGI::Carp ’fatalsToBrowser’;

The bless() statement makes the reference
$this no longer just an ordinary hash reference. It becomes an object reference of the Stats
class, and
by doing so you can also access the methods from the reference. Therefore, this method is
known as a
112 Chapter 7 Object-Oriented Programming
constructor, because it is where the object is created. A constructor is not necessarily called
`new()'. It
can be of any name, despite it is customarily called new. Object-speci_c data, or properties, are
into the object reference. Let us explain each line one by one.
my $arg0 = $_[0];
my $cls = ref($arg0) || $arg0;
In Perl, methods are exactly identical in behaviour to subroutines. However, whether it acts like
method or a plain subroutine is determined solely by the way it is invoked. A method is a
that expects an object reference as its _rst argument. Using the new() method as an example,
following ways of invoking it are identical:
my $obj = new Stats; # Indirect object syntax
my $obj = Stats->new(); # C++-like class member invocation
my $obj = Stats::new(Stats); # Resembling subroutine invocation

To rede_ne a method, which is called overriding a method, simply rewrite
the method in the way you desire and put it in the current package. If the method is invoked on
current package or its subclasses, the overriding method will be invoked instead of the
overridden ones
(that is, those in the base classes).
As an example, we will write a Stats2 class which inherits from Stats with the added
of deducing the maximum and minimum among the list of numbers input. Also attached below is
modi_ed which uses Stats2 with the additional functionalities:
EXAMPLE 7.2 Statistics Calculator (Extended)
1 #
2 # Derived "Stats" with some functionalities added

To top