Before we start Object Oriented concept of perl, lets understand references and anonymous arrays and hashes
References
- A reference is, exactly as the name suggests, a reference or pointer to another object.
- There are two types of references: symbolic and hard.
- A symbolic reference enables you to refer to a variable by name, using the value of another variable. For example, if the variable $foo contains the string "bar", the symbolic reference to $foo refers to the variable $bar.
Creating Hard References
The unary backslash operator is used to create a reference to a named variable or subroutine, for example:
$foo = 'Bill'; |
The $fooref variable now contains a hard reference to the $foo variable. You can do the same with other variables:
$array = @ARGV; |
To create a reference to a subroutine:
sub foo { print "foo" }; |
Anonymous Arrays
When you create a reference to an array directly - that is, without creating an intervening named array - you are creating an anonymous array.
Creating an anonymous array is easy:
$array = [ 'Bill', 'Ben, 'Mary' ]; |
This line assigns an array, indicated by the enclosing square brackets instead of the normal parentheses, to the scalar $array. The values on the right side of the assignment make up the array, and the left side contains the reference to this array.
You can create more complex structures by nesting arrays:
@arrayarray = ( 1, 2, [1, 2, 3]); |
The @arrayarray now contains three elements; the third element is a reference to an anonymous array of three elements.
Anonymous Hashes
Anonymous hashes are similarly easy to create, except you use braces instead of square brackets:
$hash = { 'Man' => 'Bill', |
Dereferencing
The most direct way of dereferencing a reference is to prepend the corresponding data type character ($ for scalars, @ for arrays, % for hashes, and & for subroutines) that you are expecting in front of the scalar variable containing the reference. For example, to dereference a scalar reference $foo, you would access the data as $$foo. Other examples are:
$array = @ARGV; # Create reference to array |
Object Basics
There are three main terms, explained from the point of view of how Perl handles objects. The terms are object, class, and method.
- Within Perl, an object is merely a reference to a data type that knows what class it belongs to. The object is stored as a reference in a scalar variable. Because a scalar only contains a reference to the object, the same scalar can hold different objects in different classes.
- A class within Perl is a package that contains the corresponding methods required to create and manipulate objects.
- A method within Perl is a subroutine, defined with the package. The first argument to the method is an object reference or a package name, depending on whether the method affects the current object or the class.
Defining a Class
Its very simple to define a class. In Perl, a class is corresponds to a Package.To create a class in Perl, we first build a package. A package is a self-contained unit of user-defined variables and subroutines, which can be re-used over and over again. They provide a separate namespace within a Perl program that keeps subroutines and variables from conflicting with those in other packages.
To declare a class named Person in Perl we do:
package Person; |
The scope of the package definition extends to the end of the file, or until another package keyword is encountered.
Creating and Using Objects
To create an instance of a class (an object) we need an object constructor. This constructor is a method defined within the package. Most programmers choose to name this object constructor method new, but in Perl one can use any name.
One can use any kind of Perl variable as an object in Perl. Most Perl programmers choose either references to arrays or hashes.
Let's create our constructor for our Person class using a Perl hash reference;
When creating an object, you need to supply a constructor. This is a subroutine within a package that returns an object reference. The object reference is created by blessing a reference to the package's class. For example:
package Person; |
Every method of a class passes first argument as class name. So in the above example class name would be "Person". You can try this out by printing value of $class. Next rest of the arguments will be rest of the arguments passed to the method.
Now Let us see how to create an Object
$object = new Person( "Mohammad", "Saleem", 23234345); |
You can use simple hash in your consturctor if you don't want to assign any value to any class variable. For example
package Person; |
Defining Methods
Other object-oriented languages have the concept of security of data to prevent a programmer from changing an object data directly and so provide accessor methods to modify object data. Perl does not have private variables but we can still use the concept of helper functions methods and ask programmers to not mess with our object innards.
Lets define a helper method to get person first name:
sub getFirstName { |
Another helper
function to set person first name:
function to set person first name:
sub setFirstName { |
Lets have a look into complete example: Keep Person package and helper functions into Person.pm file
#!/usr/bin/perl |
Now create Person object in mail.pl fileas follows
#!/usr/bin/perl |
Inheritance
Object-oriented programming sometimes involves inheritance. Inheritance simply means allowing one class called the Child to inherit methods and attributes from another, called the Parent, so you don't have to write the same code again and again. For example, we can have a class Employee which inherits from Person. This is referred to as an "isa" relationship because an employee is a person. Perl has a special variable, @ISA, to help with this. @ISA governs (method) inheritance.
Following are noteable points while using inheritance
- Perl searches the class of the specified object for the specified object.
- Perl searches the classes defined in the object class's @ISA array.
- If no method is found in steps 1 or 2, then Perl uses an AUTOLOAD subroutine, if one is found in the @ISA tree.
- If a matching method still cannot be found, then Perl searches for the method within the UNIVERSAL class (package) that comes as part of the standard Perl library.
- If the method still hasn't been found, then Perl gives up and raises a runtime exception.
So to create a new Employee class that will inherit methods and attributes from our Person class, we simply code: Keep this code into Employee.pm
#!/usr/bin/perl |
Now Employee Class has all the methods and attributes inherited from Person class and you can use it as follows: Use main.pl file to test it
#!/usr/bin/perl |
Method Overriding
The child class Employee inherits all the methods from parent class Person. But if you would like to override those methods in your
child class then you can do it by givig your implementation. You can add your additional functions in child class. It can done as follows: modify Employee.pm file
child class then you can do it by givig your implementation. You can add your additional functions in child class. It can done as follows: modify Employee.pm file
#!/usr/bin/perl |
Now put following code into main.pl and execute it.
#!/usr/bin/perl |
Default Autoloading
Perl offers a feature which you would not find any many other programming languages: a default subroutine.
If you define a function called AUTOLOAD() then any calls to undefined subroutines will call AUTOLOAD() function. The name of the missing subroutine is accessible within this subroutine as $AUTOLOAD. This function is very useful for error handling purpose. Here is an example to implement AUTOLOAD, you can implement this function in your way.
sub AUTOLOAD |
Destructors and Garbage Collection
If you have programmed using objects before, then you will be aware of the need to create a .destructor. to free the memory allocated to the object when you have finished using it. Perl does this automatically for you as soon as the object goes out of scope.
In case you want to implement your destructore which should take care of closing files or doing some extra processing then you need to define a special method called DESTROY. This method will be called on the object just before Perl frees the memory allocated to it. In all other respects, the DESTROY method is just like any other, and you can do anything you like with the object in order to close it properly.
A destructor method is simply a member function (subroutine) named DESTROY which will be automatically called
- When the object reference's variable goes out of scope.
- When the object reference's variable is undef-ed
- When the script terminates
- When the perl interpreter terminates
For Example:
package MyClass; |
Another OOP Example
Here is another nice example which will help you to understand Object Oriented Concepts of Perl. Put this source code into any file and execute it.
#!/usr/bin/perl |
No comments:
Post a Comment