Chapter 35. Building Objects Out of Arrays

Greg Bacon

Object-oriented programming is an approach to software design that most programmers simultaneously love and hate. OOP is just so adorable because it provides a simple conceptual model and code that often reads very similarly to natural language:

	my $dog = new Dog; ## create a new dog

	$dog->bark;        ## Speak, Fido!

The misconception that object-oriented design is the proper ritual to drive out bugs and maintenance problems causes most most programmers to hang their heads and sigh when someone touts the latest Magic Object-Oriented Toaster. But the OOP Establishment thrives on rituals. In the tradition of Galileo, I will challenge the Establishment and repudiate one of those rituals:

Objects in Perl need not be represented with hashes.

This article assumes at least some knowledge of how to implement objects in Perl. If you don’t currently possess the prerequisite knowledge, fear not! You are but a short reading of the perltoot documentation away. This article explores how using arrays as objects can be more efficient (both timewise and spacewise) and encourage nicer style, while refraining from violating this principle given in the perlmodlib documentation:

Perl doesn’t have an infatuation with enforced privacy. It would prefer that you stayed out of its living room because you weren’t invited, not because it has a shotgun.

We’ll call this the Graciousness Principle.

OO Basics

There are several ways to create objects in Perl, but they all ...

Get Computer Science & Perl Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.