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

Discussion in 'Perl Misc' started by jl_post@hotmail.com, Oct 12, 2010.

  1. Guest

    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::Dumper;
    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
     
    , Oct 12, 2010
    #1
    1. Advertising

  2. Alan Curry Guest

    In article <>,
    <> 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
     
    Alan Curry, Oct 12, 2010
    #2
    1. Advertising

  3. Guest

    > <> 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, (Alan Curry) replied:
    >
    > Get their addresses with Scalar::Util::refaddr and compare.



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

    -- Jean-Luc
     
    , Oct 12, 2010
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    489
  2. Sandra-24
    Replies:
    15
    Views:
    674
    Sandra-24
    Dec 17, 2006
  3. John Nagle
    Replies:
    3
    Views:
    311
    John Nagle
    Feb 25, 2007
  4. Replies:
    6
    Views:
    371
  5. Saraswati lakki
    Replies:
    0
    Views:
    1,417
    Saraswati lakki
    Jan 6, 2012
Loading...

Share This Page