O'Reilly logo

Perl Cookbook by Nathan Torkington, Tom Christiansen

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 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 ...

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