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

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.

Replies are listed 'Best First'.
(tye)Re2: eq or == with references
by tye (Sage) on Jul 09, 2001 at 00:03 UTC

    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")