Click here to Skip to main content
Licence GPL3
First Posted 14 Mar 2008
Views 10,590
Downloads 36
Bookmarked 15 times

The Power of Perl OO

By | 14 Mar 2008 | Article
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)

About the Author

Helbrax

Web Developer

United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120528.1 | Last Updated 14 Mar 2008
Article Copyright 2008 by Helbrax
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid