Click here to Skip to main content
15,881,877 members
Articles / Programming Languages / Perl
Article

The Power of Perl OO

Rate me:
Please Sign up or sign in to vote.
4.53/5 (8 votes)
14 Mar 2008GPL32 min read 18.8K   111   16  
Using Perl's object oriented interface to extend existing types

Introduction

Using the power of Perl's OO interface, it's possible to extend existing Perl types in an object oriented fashion.

Background

Perl is a great language, but one of the biggest problems I have with it is that the core of the language is not object oriented, leading to situations like:

my @foo = ();
push(@foo, "bar") 

While there isn't necessarily anything wrong with that, Perl does have some inconsistencies in its function calls. Sometimes the object being modified is the first argument, sometimes not. Having the array itself call the push eliminates the need to provide unnecessary arguments. You can also add your own functions that are not present, such as pulling out all unique elements from an array.

Creating the Module

For this example, I won't include all the "normal" functionality you'll find in a module, such as version, pod, etc.

package Array;

use strict;

#create our constructor
sub new()
{
    my ($package, @args) = @_;
    my @internal = ();
    @internal = @args if defined @args;
    my $self = \@internal;
    return bless($self, $package);
} 

Now that we have a constructor, we can create a new array by calling new Array(). We can create an empty array, or pass arguments in list or scalar context to our constructor to populate the array at create time. Since our object itself is nothing more than a reference to an array, we can use it like a normal array by wrapping the object in @{}:

$array = new Array;
print @{$array}; 

Now that we have an object, it is probably a good idea to add some methods to go along with it.

sub push()
{
    my ($self, $value) = @_;
    push(@{$self}, $value); 
}

Now we can push an object to the end of our array by using $array->push("Blah")
This can be repeated over for as many array functions as you want, such as shift, pop, etc. You can even add your own function such as the unique function.

sub unique()
{
    my $self = shift;
    my %dups = undef;
    return grep(!$dups{$_}++, @{$self});
}

All of the current functions return a normal array, but it's entirely possible to return an array object instead.

return new Array(grep(!$dups{$_}++, @{$self})); 

Using the Code

Using the example code is pretty simple:
use lib '/path/to/library';
use Array;
$array = new Array;
$array->push("New Item");

Points of Interest

Although it isn't practical to change every data type in Perl and add all the functions that go along with them, you may enjoy a more OO style of writing in your Perl code. This technique doesn't really make sense in small scripts, but it may be incredibly useful in larger projects, especially since you can add your own methods.

History

  • Friday, March 14, 2008 -- Created

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --