O'Reilly logo

Computer Science & Perl Programming by Jon Orwant

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 36. Hiding Objects with Closures

Nigel Chapman

Tom Christiansen’s object-oriented Perl tutorial (the perltootdocumentation bundled with Perl) describes a way of using closures to hide data stored in an object. It’s a technique that deserves to be better known, so in this article I will elaborate on Tom’s description and add a little background.

First of all, a warning. The three great object-oriented virtues are correctness, maintainability, and re-usability—efficiency isn’t included. The techniques I’m going to describe are not the fastest. If that bothers you, don’t use them.

Those who like to use objects and classes do so, in part, in order to manage the complexity of medium to large programs by breaking them into pieces and restricting the way in which separate pieces interact with one another. An extra benefit is that other people can take some of our pieces and use them in their programs, too.

The basic idea is to organize a program as a set of objects that know various things about their own state, and can perform certain actions in response to messages. There is no other way the object’s state can be changed. In anthropomorphic terms, the object takes responsibility for organizing its own internal state, and doesn’t allow any other object or function to change it directly.

The set of actions an object can perform—that is, the set of messages it responds to—depends on what sort of object it is, so objects can be grouped into classes according to the actions they perform. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required