How to check if a variable is a reference in Perl?
Question
How to check if a variable is a reference in Perl?
In Perl, determining whether a variable is a reference is a common need when working with complex data structures like arrays, hashes, or objects. Perl provides a built-in function ref to check if a variable holds a reference and to identify the type of reference it is.
Using the ref Function
The ref function takes a scalar (typically expected to be a variable) and returns:
- The type of reference as a string if the scalar contains a reference (e.g.,
SCALAR,ARRAY,HASH,CODE,REF) - An empty string
''if the scalar is not a reference
For example:
my $var = [1, 2, 3]; # Array reference
print ref($var); # prints "ARRAY"
my $str = "hello";
print ref($str); # prints empty string (not a reference)
Checking If a Variable Is a Reference
To explicitly test whether a variable is a reference, evaluate ref($var) in boolean context. Since ref returns a non-empty string only for references, it works well in conditional expressions:
if (ref($var)) {
print "It's a reference!\n";
} else {
print "Not a reference.\n";
}
This is often sufficient in day-to-day usage. However, if you want to test for a specific reference type, compare ref($var) to the expected string:
if (ref($var) eq 'ARRAY') {
print "This is an array reference.\n";
}
Using Scalar::Util::reftype (Note on Core Modules)
While ref is built-in and sufficient for many cases, it sometimes returns the package name for blessed objects (Perl's object references). If you want to distinguish that, modules like Scalar::Util provide reftype, but note this is not a core module in very old Perl versions. For simple reference checks, ref remains the standard approach.
Common Gotchas
refreturns the package name for blessed objects, so it's truthy but may not equal standard types like "ARRAY". To check if something is an object, userefcombined withblessed()fromScalar::Util. But that goes beyond basic reference checking.- Empty strings, numbers, or undef are not references;
refreturns empty string''. - References can be nested, e.g. references to references. Calling
refonce returns the outer type: e.g.REFif it's a reference to a reference.
Runnable Example
Here is a simple Perl script demonstrating how to check if variables are references and print their types:
#!/usr/bin/perl
use strict;
use warnings;
my $scalar = 42;
my $array_ref = [1, 2, 3];
my $hash_ref = { foo => 'bar' };
my $code_ref = sub { return "hello" };
my @vars = (
\$scalar,
$array_ref,
$hash_ref,
$code_ref,
"not a ref",
undef,
);
foreach my $var (@vars) {
my $type = ref($var);
if ($type) {
print "Variable is a reference of type: $type\n";
} else {
print "Variable is NOT a reference.\n";
}
}
This script creates various types of variables, including references and non-references, then uses ref to identify their reference status and type. Running it will print:
Variable is a reference of type: SCALAR
Variable is a reference of type: ARRAY
Variable is a reference of type: HASH
Variable is a reference of type: CODE
Variable is NOT a reference.
Variable is NOT a reference.
In summary, use Perl’s native ref function when you want to check if a variable is a reference and to identify the type of reference. It’s simple, effective, and built in to Perl's core language.
Verified Code
Executed in a sandbox to capture real output. • v5.34.1 • 6ms
ARRAY(empty)