perl5ever has asked for the wisdom of the Perl Monks concerning the following question:
After some experimenting it seems that the best way to determine if two references are the same is to use the == operator:
This is better than using string equality (eq) since the references may have overridden stringification.my $x = []; my $y = []; if ($x == $y) { print "x and y are the same reference\n" } $y = $x; if ($x == $y) { print "x and x are the same reference\n" }
I couldn't find any explicit documentation for this, but it seems that evaluating a reference in numeric context returns the address of the reference which is how == works for determining reference equality.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: checking reference equality (refaddr)
by tye (Sage) on May 10, 2009 at 23:33 UTC | |
by perl5ever (Pilgrim) on May 11, 2009 at 05:13 UTC | |
|
Re: checking reference equality
by kennethk (Abbot) on May 10, 2009 at 23:41 UTC | |
by tye (Sage) on May 11, 2009 at 07:03 UTC | |
by perl5ever (Pilgrim) on May 11, 2009 at 01:14 UTC | |
by John M. Dlugosz (Monsignor) on May 12, 2009 at 15:18 UTC | |
|
Re: checking reference equality
by Marshall (Canon) on May 11, 2009 at 09:41 UTC |