in reply to (ar0n) Re: eq or == with references
in thread eq or == with references

Sorry, no. References have a little-used feature that was specifically designed to make == work for comparing references.

my $var; print \$var,$/; print 0+\$var,$/;
outputs:
SCALAR(0x1a6533c) 27677500
so $ref1 == $ref2 if and only if they both point to the same object. This is a very efficient test. eq will also work but isn't quite as efficient.

        - tye (but my friends call me "Tye")

Replies are listed 'Best First'.
Re: (tye)Re: eq or == with references
by ariels (Curate) on Jul 08, 2001 at 12:23 UTC
    This is true, but...

    References are compared according to some magical numerical values the contain (probably some address of what they refer to, but that's not important). So two references will only ever compare equal using == if they do indeed refer to the same object. However, a reference will compare equal to its numerical value.

    Try this in Tye's example...

    $rv = \$var; # reference to $var $num = 0+\$var; # NUMERICAL value of $rv # ($num could conceivably occur in your # program "by chance") ref $rv or die; # ASSERT: $rv is a reference !(ref $num) or die; # ASSERT: $num is NOT a reference print $rv == $num, $/; # Prints "1": a reference equals a # number.

      Sure, but that isn't any different than the case for eq:

      $rv = \$var; # reference to $var $str = "".\$var; # STRING value of $rv # ($str could conceivably occur in your # program "by chance") ref $rv or die; # ASSERT: $rv is a reference !(ref $str) or die; # ASSERT: $str is NOT a reference print $rv eq $str, $/; # Prints "1": a reference equals a string.
      So I don't really appreciate your point. If you want to decide if two things are equal references, then you have to first determine that they are both references, whether you use eq or ==. I suppose that you could argue that a matching number is more likely to come up than a matching string but I 1) wouldn't buy that argument and 2) think that making your program rely on the unlikelyhood of either is poor design. (:

              - tye (but my friends call me "Tye")