Chapter 13. Introduction to Objects

Object-oriented programming (often called OOP) helps us run code sooner and maintain it easier by organizing the code into things that we can name and compartmentalize. We need a little more infrastructure to get going with objects, but in the long run, it’s worth it.

The benefits of OOP become worthwhile when our program (including all external libraries and modules) exceeds about N lines of code. Unfortunately, nobody can agree on what the value of N is, but for Perl programs, it’s arguably around 1,000 lines of code. If our whole program is only a couple hundred lines of code, using objects is probably overkill.

Like references, Perl’s object architecture was grafted on after a substantial amount of existing pre-v5 code was already in use, so we had to ensure that it wouldn’t break existing syntax. Amazingly, the only additional syntax to achieve object nirvana is the method call, which we’ll introduce shortly. The meaning of that syntax requires a bit of study, and we’re going to cover just those syntax basics.


If you want to learn about OOP in Perl in all the gory details, try Damian Conway’s Object Oriented Perl (Manning), which also covers the theory and architecture behind these ideas.

The Perl object architecture relies heavily on packages, subroutines, and references, so if you’re skipping around in this book, go back to the beginning. Ready? Here we go.

If We Could Talk to the Animals. . .

Obviously, the castaways can’t survive on coconuts ...

Get Intermediate Perl, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.