References

A reference is simply a Perl equivalent of a C pointer. Actually, Perl references do much more than C pointers, but for now, it’s easiest to think of them as pointers.

The backslash character (\) indicates a reference. The equivalent operator in C is address of (&). The following example creates a reference to a simple variable:

my $variable = 1; 
my $reference = \$variable;   # Reference to a variable 
$$reference = 2; 
print "$variable\n";    # Prints 2

In this example, $reference is a reference to a scalar—$variable. To get to the actual scalar being referenced, you need to add another $ to the expression— $$reference. Think of ($) as a (*) C operator being used with a C pointer.

The following example gets a little bolder and defines ...

Get Perl for C Programmers 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.