How should I compare Perl references?

后端 未结 3 700
孤独总比滥情好
孤独总比滥情好 2021-02-11 18:32

I want to check if two references point to the same object. It seems I can simply use

if ($ref1 == $ref2) { 
 # cheap numeric compare of references
 print \"ref         


        
3条回答
  •  独厮守ぢ
    2021-02-11 18:58

    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.

提交回复
热议问题