How should I compare Perl references?

References, by default, numify to their addresses. Those reference addresses are unique for every reference, so it can often be used in equality checks.

However, in the snippet you showed, you’d first have to make sure that both $ref1 and $ref2 are actually references. Otherwise you might get incorrect results due to regular scalars containing reference addresses.

Also, nothing guarantees references to numify to their address. Objects, for example, can use overloading to influence the value they return in different contexts.

A more solid way than comparing references directly for numeric equality would be to use the refaddr function as provided by Scalar::Util, after making sure both sides actually are references.

Leave a Comment

tech