How to create a reference to a scalar in Perl?
Question
How to create a reference to a scalar in Perl?
In Perl, a reference is a scalar value that "points to" another value or data structure, such as a scalar, array, or hash. Creating a reference to a scalar allows you to indirectly access or modify that scalar through the reference, which is especially useful when passing variables to subroutines or building more complex data structures like arrays of scalar references.
How to create a scalar reference
The simplest way to create a reference to a scalar is by using the backslash operator (\\) in front of a scalar variable or a literal value. This returns a reference (a scalar containing a memory address) to that scalar.
Here are two common ways to create scalar references:
my $scalar = 42; my $ref = \\$scalar;— Reference to an existing scalar variablemy $ref = \\"hello";— Reference to a scalar literal (anonymous scalar)
Dereferencing a scalar reference
Once you have a reference, you can dereference it using the $$ sigil to access or modify the underlying scalar:
$$refgives the scalar value the reference points to- You can assign to
$$refto change the original scalar
Perl sigils and context
Remember that the sigil ($, @, %) indicates the expected data type. When dereferencing a scalar ref, you use $$ref because the reference contains a scalar, and you want the scalar value inside.
Perl offers the flexibility of TMTOWTDI ("There's more than one way to do it")—you can create references using \\ or the ref function (to identify references), but backslash is the standard method to create scalar refs.
Common Pitfalls
- Not dereferencing properly—remember
$$refto get the scalar, not just$ref. - Trying to take a reference of a literal without parentheses:
my $ref = \\42;is invalid; instead usemy $ref = \\(42);or create a scalar variable first. - Confusing references to arrays/hashes (
\\@array,\\%hash) and scalar references—they use different sigils to dereference.
Code Example
use strict;
use warnings;
# Create a scalar variable
my $scalar = 10;
# Create a reference to the scalar
my $ref = \\$scalar;
print "Original scalar value: $scalar\n"; # 10
print "Dereferenced value: $$ref\n"; # 10
# Modify scalar through dereferencing the reference
$$ref = 20;
print "Modified scalar value: $scalar\n"; # 20
print "Dereferenced value now: $$ref\n"; # 20
# Reference to a literal scalar (anonymous scalar)
my $anon_ref = \\( 'hello' );
print "Anonymous scalar reference value: $$anon_ref\n";
Summary
To create a reference to a scalar in Perl, simply apply the backslash operator to the scalar: my $ref = \\$scalar;. You can then use $$ref to access or modify the original scalar value. Scalar references are fundamental for complex data structures and subroutine argument passing and form the basis of understanding Perl references across all data types.
Verified Code
Executed in a sandbox to capture real output. • v5.34.1 • 5ms
Original scalar value: 10
Dereferenced value: SCALAR(0x12682b2a0)
Modified scalar value: 10
Dereferenced value now: 20
Anonymous scalar reference value: SCALAR(0x12684ab60)
(empty)