Chapter 11. References and Records

Introduction

With as little a web as this will I ensnare as great a fly as Cassio.

Shakespeare, Othello, Act II, scene i

Perl provides three fundamental data types: scalars, arrays, and hashes. It’s certainly possible to write many programs without recourse to complex records, but most programs need something more complex than simple variables and lists.

Perl’s three built-in types combine with references to produce arbitrarily complex and powerful data structures, the records that users of ancient versions of Perl desperately yearned for. Selecting the proper data structure and algorithm can make the difference between an elegant program that does its job quickly and an ungainly concoction that’s glacially slow to execute and consumes system resources voraciously.

The first part of this chapter shows how to create and use plain references. The second part shows how to use references to create higher order data structures.

References

To grasp the concept of references, you must first understand how Perl stores values in variables. Each defined variable has a name and the address of a chunk of memory associated with it. This idea of storing addresses is fundamental to references because a reference is a value that holds the location of another value. The scalar value that contains the memory address is called a reference. Whatever value lives at that memory address is called a referent . (You may also call it a “thingie” if you prefer to live ...

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.