Chapter 11. References and Records

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

Shakespeare, Othello, Act II, scene i

Introduction

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

Perl’s three built-in types combine with references to produce arbitrarily complex and powerful data structures. 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 create higher-order data structures out of references.

References

To grasp the concept of references, you must first understand how Perl stores values in variables. Each defined variable has associated with it a name and the address of a chunk of memory. 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 its referent. See Figure 11-1.

Reference and referent
Figure 11-1. Reference and referent ...

Get Perl Cookbook, 2nd Edition now with O’Reilly online learning.

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