Who is this for
Those who want to learn Object Oriented Programming in Perl
What you should
know Object Oriented Programming Concepts Basic Perl
Programming Perl
Packages Perl
Variable Scoping Perl
References Perl
Functions
Introduction
Now that you have learned how to create packages and functions
within packages, let us see how Perl uses these to implement Object
Oriented programming.
Object Oriented
Programming Concepts This is a very short description on OOP.
Objects
are implemented using classes. A class has methods and
properties.
Internal
representation of properties are transparent to you. This is called
Abstraction.
Methods
and properties are enclosed together in the class. This is called
encapsulation.
Classes
can be built on top of other classes. Methods and properties of
parent classes can be re-used by their child classes. This is called
inheritance.
A child class can
override a parent method through polymorphism.
Perl Implementation
of OOP Perl implements OOP using packages. Packages
are used as classes. Functions are used as
methods. Variables are used as properties.
Instances are implemented using references.
Don't be overwhelmed
with these terms, you can interchange the terms. When we say class,
we actually mean a Perl package. When we say method, we actually mean
function, etc.
Although you are free
to use any method name to initialize your object, you should use new
instead. This keeps it consistent with the C++ programming style. If
you will be the only one touching your program, you can use any name.
If not, be nice to the one who will revise your program, use the
standard new method.
@ISA Array All
packages have a local @ISA array. This array is used to
implement inheritance. This array contains the classes from which
your class is built on.
If you have say, a
city class built on top of a province class, you need
an item in the @ISA array in the city class with the
value of province. Should a function call a method in
your class, Perl will first search for the method within
your class. If the method is not in your class, it goes to the
@ISA array to find any defined parent class. If your class was
built on top of a base class, Perl goes to that class and looks for
the method there.
Sample
Program
package province; { sub new { my $class = shift; my $self = {}; bless $self, $class; } sub setProvince { # initialize variables my $self = shift; $self->{PROVINCE} = ''; $self->{COUNTRY} = ''; $self->{PROVINCE} = shift if @_; # values are $self->{COUNTRY} = shift if @_; # passed } sub printProvince { my $self = shift; print "Province : $self->{PROVINCE}\n"; print "Country : $self->{COUNTRY}\n"; }
}
package city; { push @ISA, 'province'; sub new { # Create new object my $class = shift; my $city = {}; bless $city, $class; # initialize variables $city->{CITYNAME} = ''; # Set default $city->{CITYNAME} = shift if @_; # set if passed $city->setProvince(@_); return $city; } sub printCity { my $city = shift; print "City Name: $city->{CITYNAME}\n"; $city->printProvince; } }
package main; { my $addr = new city('Victoria', 'BC', 'Canada'); $addr->printCity; }
|
Explanation
The
base class is the province class. It has three methods: new,
setProvince and printProvince.
The child class is the
city class. We defined the city class as a child of the
province class by pushing the literal 'province'
to the @ISA array.
Both classes have the
new method. The new method of the province class
is simpler because it just creates a reference to an empty hash and
blesses it. The new method of the city class is
different. It initializes all the properties and then calls the
setProvince method.
Notice that even if
the setProvince method is in its parent class, the method
is referred within itself ($city). This is inheritance
at work. The method in the parent class is re-used by the
child class.
Notice also the
printCity method in the city class, it calls the
printProvince method in its parent class - again - treating
the parent method as if it were its own.
Execution starts at
package main. It defines a new city with the following
parameters: Victoria, BC and Canada. It is
actually calling the new function in the city package.
When it enters the new function, it performs initialization and then
calls the setProvince method of the province class.
When the setProvince function is executed, it sets the PROVINCE
and COUNTRY properties.
The next statement in
the main package calls the printCity method of the $addr object. The
printCity function prints out the city name then calls the
printProvince function of the province object.
The result of this
script will be:
|
City
Name: Victoria Province: BC Country : Canada
|
|