ref 
ref EXPR
refThe ref operator returns a true value if EXPR is
a reference, and false otherwise. The value returned depends on the type
of thing the reference refers to. Built-in types include:
SCALAR ARRAY HASH CODE REF GLOB LVALUE FORMAT IO VSTRING Regexp
The return value LVALUE
indicates a reference to an lvalue that is not a variable. You get this
from taking the reference of function calls like pos or substr. VSTRING is returned if the reference points to
a version string.
The result Regexp indicates
that the argument is a regular expression resulting from qr//.
If the referenced object has been blessed into a package, then
that package name is returned instead. You can think of ref as a “typeof” operator.
if (ref($r) eq "HASH") {
say "r is a reference to a hash.";
}
elsif (ref($r) eq "Hump") { # Naughty—see below
say "r is a reference to a Hump object.";
}
elsif (not ref $r) {
say "r is not a reference at all.";
}It’s considered bad OO style to test your object’s class for
equality to any particular class name, since a derived class will have a
different name but should be allowed access to the base class’s methods,
according to the Liskov Substitution Principle. It’s better to use the
UNIVERSAL method isa, as follows:
if ($r–>isa("Hump")) {
say "r is a reference to a Hump object, || subclass.";
}It’s usually best not to test at all, since the OO mechanism won’t send the object ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access