Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > How can I check if two refs point to the same object?

Reply
Thread Tools

How can I check if two refs point to the same object?

 
 
jl_post@hotmail.com
Guest
Posts: n/a
 
      10-11-2010

Hi,

I was wondering if there was a way I could check if two references
are referencing the same object. For example, consider the following
code:

use Math::BigInt;
my $n1 = Math::BigInt->new(5);
my $n2 = $n1;
my $n3 = Math::BigInt->new(5);

In this code, $n1, $n2, and $n3 will all be equivalent as far as the
'==' and 'eq' operators are concerned, but how can I check to see
which ones are referencing the same object? In other words, how can I
tell that $n1 and $n2 are referring to the same object, and $n3 is
not?

The only way I know how to do this is to run the following code:

use Data:umper;
print Dumper $n1, $n2, $n3;

and check for lines like "$VAR2 = $VAR1;" but it seems like there
should be a more straight-forward way to do this.

Is there a way to see which references refer to the same object,
even when the '==' and 'eq' operators are overloaded?

Thanks,

-- Jean-Luc
 
Reply With Quote
 
 
 
 
Alan Curry
Guest
Posts: n/a
 
      10-11-2010
In article <(E-Mail Removed)>,
http://www.velocityreviews.com/forums/(E-Mail Removed) <(E-Mail Removed)> wrote:
>
> I was wondering if there was a way I could check if two references
>are referencing the same object. For example, consider the following
>code:
>
> use Math::BigInt;
> my $n1 = Math::BigInt->new(5);
> my $n2 = $n1;
> my $n3 = Math::BigInt->new(5);

[...]
>
> Is there a way to see which references refer to the same object,
>even when the '==' and 'eq' operators are overloaded?


Get their addresses with Scalar::Util::refaddr and compare.

use Scalar::Util 'refaddr';

sub sameref
{
my ($a1,$a2) = (refaddr($_[0]), refaddr($_[1]));
return defined($a1) && defined($a2) && $a1 == $a2;
}

use Math::BigInt;
my $n1 = Math::BigInt->new(5);
my $n2 = $n1;
my $n3 = Math::BigInt->new(5);

print "n1 sameref n2: ", (sameref($n1,$n2) ? "yes" : "no"), "\n";
print "n1 sameref n3: ", (sameref($n1,$n3) ? "yes" : "no"), "\n";
print "n2 sameref n3: ", (sameref($n2,$n3) ? "yes" : "no"), "\n";

--
Alan Curry
 
Reply With Quote
 
 
 
 
jl_post@hotmail.com
Guest
Posts: n/a
 
      10-12-2010
> (E-Mail Removed) <(E-Mail Removed)> wrote:
>
> > * Is there a way to see which references refer to the same
> > object, even when the '==' and 'eq' operators are overloaded?



On Oct 11, 5:28*pm, (E-Mail Removed) (Alan Curry) replied:
>
> Get their addresses with Scalar::Util::refaddr and compare.



Thanks, Alan! That's exactly what I'm looking for!

-- Jean-Luc
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Share-Point-2010 ,Share-Point -2010 Training , Share-point-2010Hyderabad , Share-point-2010 Institute Saraswati lakki ASP .Net 0 01-06-2012 06:39 AM
Are weak refs slower than strong refs? John Nagle Python 3 02-25-2007 08:54 PM
Scenario 5: IS-IS routing on Frame Relay Multi-point and Point-to-Point David Sudjiman Cisco 0 06-08-2006 09:11 AM
sort and Sub Refs Perl 1 01-03-2004 09:43 PM



Advertisments