Querying a Reference

The ref function queries a scalar to see whether it contains a reference and, if so, what type of data it is pointing to. ref returns false (a Boolean value, not a string) if its argument contains a number or a string; and if it’s a reference, ref returns one of these strings to describe the data being referred to: “SCALAR”, “HASH”, “ARRAY”, “REF” (referring to another reference variable), “GLOB” (referring to a typeglob), “CODE” (referring to a subroutine), or "package name" (an object belonging to this package—we’ll see more of it later).

$a = 10;
$ra = \$a;

ref($a) yields FALSE, since $a is not a reference.

ref($ra) returns the string “SCALAR”, since $ra is pointing to a scalar value.

Get Advanced Perl Programming 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.