How to get the type of a reference in Perl using ref()?
Question
How to get the type of a reference in Perl using ref()?
In Perl, references are scalar values that "point to" other data such as scalars, arrays, hashes, or subroutines. When you want to determine what kind of reference you have, Perl's built-in ref() function is the primary tool. It returns a string representing the data type that the reference points to.
What does ref() do?
The ref() function takes a single scalar (expected to be a reference) and returns:
- A string indicating the type of data it references:
'SCALAR','ARRAY','HASH','CODE','REF'(a reference to a reference),'GLOB', or the name of a blessed package if the reference is an object. - An empty string (
'') if the argument is not a reference.
How to use ref() to check reference types
Given a variable holding a reference, you can check its type like this:
my $ref = \@array;
print ref($ref); # prints 'ARRAY'
This can help when writing functions that accept different reference types and need to behave differently based on the input.
Common gotchas
ref()only tells you if the scalar is a reference and which type, but it does not validate if the reference is valid or if it's a blessed object and still supports that type.- When an object is passed (a blessed reference),
ref()returns the class (package) name, not the underlying data type. - To get the underlying reference type of an object, you need to use the
reftypefunction from theScalar::Utilmodule (a core module but not part of the bare minimum). This is useful in more advanced type checking. - Remember the Perl sigils: scalar (
$), array (@), and hash (%). References are scalars ($), but they may point to complex structures.
Example: Determine and print the type of various references
#!/usr/bin/perl
use strict;
use warnings;
my $scalar = 42;
my $scalar_ref = \$scalar;
my $array_ref = [1, 2, 3];
my $hash_ref = { foo => 'bar' };
my $code_ref = sub { return "Hello"; };
my $ref_to_ref = \$scalar_ref;
my $undef_value;
# A subroutine to print the ref type or say "Not a reference"
sub show_ref_type {
my ($ref) = @_;
my $type = ref($ref);
if ($type) {
print "Reference type: $type\n";
} else {
print "Not a reference\n";
}
}
print "Scalar value:\n";
show_ref_type($scalar); # Not a reference
print "\nScalar reference:\n";
show_ref_type($scalar_ref); # SCALAR
print "\nArray reference:\n";
show_ref_type($array_ref); # ARRAY
print "\nHash reference:\n";
show_ref_type($hash_ref); # HASH
print "\nCode reference:\n";
show_ref_type($code_ref); # CODE
print "\nReference to a reference:\n";
show_ref_type($ref_to_ref); # REF
print "\nUndefined value:\n";
show_ref_type($undef_value); # Not a reference
This script demonstrates how ref() distinguishes between various reference types and non-references.
Summary
ref()returns the type of the reference as a string or an empty string if not a reference.- Useful for runtime type-checking of variables in subroutines or complex data structures.
- Works well for unblessed references; returns the package name for objects.
- To differentiate objects' underlying data types, use
Scalar::Util::reftype()(available from Perl 5.14+ core).
Using ref() wisely can help make your Perl code robust by handling different input types appropriately.
Verified Code
Executed in a sandbox to capture real output. • v5.34.1 • 5ms
ARRAY(empty)