Perl Packages Part 5 (Object oriented programming)


© Philip Yuson

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.

  1. Objects are implemented using classes. A class has methods and properties.

  2. Internal representation of properties are transparent to you. This is called Abstraction.

  3. Methods and properties are enclosed together in the class. This is called encapsulation.

  4. 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.

  5. 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 {

Go To Page: 1 2 3


The copyright of the article Perl Packages Part 5 (Object oriented programming) in Perl is owned by . Permission to republish Perl Packages Part 5 (Object oriented programming) in print or online must be granted by the author in writing.

Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo