Camel POOP - Perl Object Oriented Programming

I wrote this article for the CodeProject back in 2002 during my first consulting career as a web developer as a way to boost my visibility. I am capturing it here for my archives. The article is based on Perl 5.

Perl Object-Oriented Programming

Most people are not aware of the fact that Perl has support for object-oriented programming. If you've used another object-oriented programming language such as Java or C++ or been exposed to object-orientation, then object-oriented programming in Perl is nothing like that. To do real useful object-oriented programming in Perl, you only need to use a few simple rules as put forth by Damian Conway in his book Object Oriented Perl: A Comprehensive Guide to Concepts and Programming Techniques.

Object-oriented programmers are familiar with the concept of object and classes, but I will review that here quickly. An object is a thing that provides access to or modification of data. A class is the description of the attributes of a particular kind of object and how those objects can be accessed and modified. A method is a means by which an object's data is accessed or modified. An object is an instance of a class.

An example would be a Person class in an HR system. The Person class describes the attributes of a Person such as a name, address, title, social security number, id etc. A particular class instance or object would encapsulate data about a specific Person, e.g. name, title, social security number, address, etc. Some methods to access that object's data would be, name, address etc.

Perl Package delivery

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 reused 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; That's it. The scope of the package definition extends to the end of the file, or until another package keyword is encountered. Not very useful yet but on to the next section.

There's a method to this madness

A method is a means by which an object's data is accessed or modified. In Perl, a method is just a subroutine defined within a particular package. So to define a method to print our Person object we do:

sub print { my ($self) = @_;
    #print Person info
    printf("Name:%s %s", $self->firstName, $self->lastName );

The subroutine print is now associated with the package Person. To call the method print on a Person object, we use the Perl "arrow" notation. If the variable $khurt contains a Person object we would call print on that object by writing: $khurt->print(); When the object method is invoked, a reference to the object is passed in along with any other arguments. This is important since the method now has access to the object on which it to operate.

How do we create the invoking object?

Bless me, father

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;

sub new {
    my $self = { _firstName => undef, _lastName => undef, _ssn => undef, _address => undef };
    bless $self, 'Person';
    return $self;

What have we done? We created a subroutine called new associated with the package Person. The entries of the hash reference $self become the attributes of our object.

We then use the bless function on the hash reference. The bless function take two arguments; a reference to the variable to be marked and a string containing the name of the class.

This indicates that the variable now belongs to the class Person. To create an instance of our Person object: my $khurt = new Person(); We have not defined accessor methods or done any error checking on the input values or keys or the anonymous hash reference, but we have the start of a Perl Person OO framework. To make our constructor more flexible and to make our class inheritable (more on that later) we can define it to use the $class variable to bless the hash reference.

sub new {
    my ($class) = @_;
    my $self = { _firstName => undef, _lastName => undef, _ssn => undef, _address => undef };
    bless $self, $class;
    return $self;

Other object-oriented languages have the concept of security of the data to prevent a programmer from changing an object's 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 accessor methods and ask programmers to not mess with our object innards.

For our Person class, we should provide accessor methods for our object attributes; name, address, title, SSN.

package Person; #class Person
use strict;

use Address; #Person class will contain an Address

sub new { #constructor
    my ($class) = @_;
    my $self = { _firstName => undef, _lastName => undef, _ssn => undef, _address => undef };
    bless $self, $class; return $self;

#accessor method for Person first name
sub firstName {
    my ( $self, $firstName ) = @_;
    $self->{_firstName} = $firstName if defined($firstName);
    return $self->{_firstName};

#accessor method for Person last name
sub lastName {
    my ( $self, $lastName ) = @_;
    $self->{_lastName} = $lastName if defined($lastName);
    return $self->{_lastName};

#accessor method for Person address
sub address {
    my ( $self, $address ) = @_;
    $self->{_address} = $address if defined($address);
    return $self->{_address};

#accessor method for Person social security number
sub ssn {
    my ( $self, $ssn ) = @_;
    $self->{_ssn} = $ssn if defined($ssn); return $self->{_ssn};

sub print {
    my ($self) = @_; #print Person info
    printf("Name:%s %s ", $self->firstName, $self->lastName );

Making babies

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 one, 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 which governs (method) inheritance. So to create a new Employee class that will inherit methods and attributes from our Person class we simply code:

package Employee; # class Employee
use Person;
use strict;
our @ISA = qw(Person); # inherits from Person

What we have done is load the Person class and declare that Employee class inherits methods from it. We have declared no methods for Employee, but an Employee object will behave just like a Person object. We should be able to write code:

my $khurt = new Employee(); #create Employee class instance

#set object attributes

# without any other changes.

Now let's add some methods.

package Employee; # class Employee
use Person;
use strict;
our @ISA = qw(Person); # inherits from Person

sub new { # constructor

    my ($class) = @_;

    #call the constructor of the parent class, Person.
     my $self = $class->SUPER::new();
     $self->{_id} = undef;
     $self->{_title} = undef;
     bless $self, $class;
     return $self; 

sub id {  #accessor method for id
    my ( $self, $id ) = @_;
    $self->{_id} = $id if defined($id);
    return ( $self->{_id} );

sub title { #accessor method for title
    my ( $self, $title ) = @_;
    $self->{_title} = $title if defined($title);
    return ( $self->{_title} );

sub print {
    my ($self) = @_;
    $self->SUPER::print; # call the print method of the parent class

Looking at the code, you will notice that we have a new method and a print method. Both a Child class and its Parent class have the same method defined. We have overridden the Parent class' methods with the ones from the Child. When those methods are called on an Employee object, we will get the Employee class' version of the method. This concept of using the methods of an existing object and modifying them is known as polymorphism.

Putting it together

So now we have a complete set of classes we can write a small program to test them.

use strict; 
use warnings;
use diagnostics; use Employee;

my $khurt = eval { new Employee(); } or die ($@); #create Employee class instance

# set object attributes
$khurt->lastName('Williams'); $khurt->id(1001);
$khurt->title('Executive Director');
$khurt->address( new Address() );
$khurt->address->street('10 Anywhere Lane');

$khurt->print(); #display Employee info

Let's execute our code and see the output.

$ ./
Name: Khurt Williams
Address: 10 Anywhere Lane
Anytown, NJ 12345

It works! We covered the basics of object-oriented programming in Perl.

I hope this article was informative and useful.

The post Camel POOP - Perl Object Oriented Programming first appeared 11/12/2002 on Code Project.

2019-09-20 21.09.51

Performance and Working Fast (

I think the reason I don't work fast anymore is because the code I write and any other actions I take for work cause ripples that effect a whole lot more people now than they did 10 years ago. I have to be conscious of this (at least I assume I do, or should be).

I often wish that software developers were criminally liable for the code they write. I have disdain for this way of working. It’s reckless.

Learning to Code is Damn Hard

Why Learning to Code is So Damn Hard

...for the most part, ... introductory tools do a great job of guiding you like a child in a crosswalk past the big scary variables and conditional statements and through the early phases of programming syntax. As you conquer one after another of their gamified challenges, your confidence rises. Maybe you can do this after all! How hard can it be? You're basically a developer already!

I've written code since I was about 13 (1980). First BASIC, then later Pascal, assembler, C, C++, Perl, Java, PHP etc. Back in the 1980s, everyone was self-taught. You either figured it out or you didn't. No Google. No online tutorials.

Learning a new language -- the basic syntax etc -- takes time is you've never done it before. Being good enough at it so that you can build an application on your own or as part of a team takes longer.

My daughter is learning to code now -- she's teaching her self -- and I want to encourage her. But I also want her expectations to be realistic. I want her to understand that coding isn't just putting little boxes with code snippets into a graphical web GUI.

I want her to be wary of hyperbolic claims about learning to code and becoming a developer in just a few weeks.