Perl - Object Oriented Programming Concepts
This is a very introductory overview of object-oriented programming (OOP) in Perl. Perl did not have object-oriented support up to Perl 4. Perl 5 introduced the concepts of objects, packages, modules, classes, methods to Perl and provided language constructs to support them. Scripts written in Perl 4 work with Perl 5. So for all practical purposes you can consider object-oriented features as extensions of Perl 4, which we have been learning in prior lessons.
Namespace, Package and Module
- Namespace. It stores names of variables, subroutines, filehandles, formats. Each namespace has its own symbol table, which is basically an associative array, or hash, with a key for each name.
- Package. Namespaces in Perl are held in one package. By convention pakage names start with a capital letter. The default namespace/package is main. Variables within a package are referenced using the package name followed by two colons :: and the variable name. For example the variable var in the package main would be referenced as $main::var . Packages may be nested inside of other packages, and the variable age of the package Person within the package Team would be addressed as $Team::Person::age . The symbol table is referred as %packagename:: and in the case of the Team package it would be %Team::
- Module. A module is a package stored in one file. The files are named with the package name followed by .pm . For example the package Rest when saved as Rest.pm becomes the module Rest. See example 13 for the basic structure of a package/module. A module can be included in a script with use or require. For example to include the module Rest in your script you write in your script: use Rest; or require Rest;
The difference between use and require is that use calls the module at the compile time and all names inside of the module became available in the rest of the script, and you do not need to qualify the names with the package/module name. Require only calls the package at the run time. I suggest that beginners using modules choose use instead of require to simplify their coding and avoid confusion. It is OK to require a subroutine, like cgi-lib.pl, for you will be able to use the names inside the subroutine without reference to a package name (remember subroutines are not packages/modules), like in &MethGet.
In order to use the names (functions, variables, filehandlers) inside of a module you need to know their names and what they are and do, see Example 13a for a script using the Rest.pm module we saw previously. Many modules automatically export names to your script, but with some modules and names you may need to make explicit the names you want to be able to use them. The format to call the module in this case is: use Modulename qw(name1 name2 .. namei);
Perl locates modules by searching the @INC array, which contains a list of library directories. In Windows it refers to files in Perl\lib, while in Linux it will be in /usr/lib/perl5.
- local and my functions. Two very useful functions regarding values of variables are local and my. The local function assigns a local value to a global variable within a block, subroutine, etc. When the block is exited the global value is automatically restore as shown in example 14. The my function creates a private (more than local ...) value for variable(s) inside a block as shown in example 15.
Classes, objects and methods
You will not need to use OOP Perl concepts to write most of your scripts, but you will need to understand its terminology, invocation and notation, so that you can use Perl object-oriented modules.
- Classes. Class cuts accross the concepts of files and modules, but the typical class in Perl is implemented with a module. The class defines methods that apply to all objects belonging to that class, create objects, etc. But, just because a module was created it does not mean the module is implementing a class (for example Rest.pm does not define a class) and OOP is happening. A module that defines a class must have one or more contructor methods to create objects belonging to the class.
In this course I will not teach you how to create classes, but rather how to use them, given our course objectives. You will think of a class as a package/module that provides methods to deal with objects, including their creation.
- Objects. Objects are instances of a class and have a specific set of available methods within their class, but they also inherit methods from their parent classes (classes which were included in the new class through the @ISA line we saw in Rest.pm). Perl classes do only method inheritance, data inheritance needs to be programmed in the class. The bless function is used by the constructor method to create an object, making the object to know to which package it belongs to.
- Methods. Methods are simply functions, subroutines, with no additional syntax, but a method expects its first argument to be an object or package it is being invoked on. A class method expects the first argument to be a class/module/package name. An instance method expects an object as its first argument. It typically shifts the first argument (object name) into a private variable ($self or $this) and uses it as a reference. See example 16 for the definition of the class Person.pm and the use of a constructor method.
There are two ways methods are invoked: (a) method class or instance name list as in: $c = find People "Joe"; where find is the method, People is the class/package and Joe is the name list. This method can be confusing sometimes and a second syntax using the -> notation is suggested to beginners and seasoned Perl programmers alike. This second syntax is: class or instance -> method(name list) as in $c = People->find("Joe"); See example16a for a script using the -> notation to use the Person.pm class.
This page is maintained by Al Bento
who can be reached at firstname.lastname@example.org. This page was last updated on October 7, 2002. Although we will attempt to keep this information accurate, we can not guarantee the accuracy of the information provided.