Chapter 13. Classes, Objects, and Ties

Introduction

All the world over, I will back the masses against the classes.

William E. GladstoneSpeech at Liverpool, 28 June 1886

Along with references and modules, release 5.000 of Perl added objects. As usual, Perl doesn’t try to enforce one true style but embraces many. This helps more people do their job the way they want to do it.

You don’t have to use objects to write programs, unlike Java, where programs are instances of objects. If you want to, though, you can write Perl programs that use nearly every weapon in the object-oriented arsenal. Perl supports classes and objects, single and multiple inheritance, instance methods and class methods, access to overridden methods, constructors and destructors, operator overloading, proxy methods through autoloading, delegation, a rooted hierarchy for all objects, and two levels of garbage collection.

You can use as many or as few object-oriented techniques as you want and need. Ties are the only parts of Perl where you must use object orientation. And even then, only the module implementor need be aware of this; the casual user gets to remain blissfully unaware of the internal mechanics. Ties, discussed in Section 13.15, let you transparently intercept access to a variable. For example, you can use ties to make a hash that allows lookups by key or value.

Under the Hood

If you ask ten people what object orientation is, you’ll get ten different answers. People bandy about terms like abstraction ...

Get Perl Cookbook 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.