The Basics: A General Overview of How to Use References

Let's look at a few simple examples of creating and using references so you can get a feel for the technique. Perl actually has a number of ways of working with references, but here we'll focus on the easiest and most popular mechanisms, and explore the others in a later section (“Other Ways of Using References”).

Creating a Reference

Start with something you've seen a gazillion times before in this book: A plain old scalar variable that contains a string.

$str = "This is a string."

This is an ordinary scalar variable, holding ordinary scalar data. There's some location in memory that stores that string, which you can get to through the $str variable name. If you assigned something else ...

Get Sams Teach Yourself Perl in 21 Days, Second Edition now with O’Reilly online learning.

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