in reply to Re: check if 2 values are equal
in thread check if 2 values are equal
The problem with this is that the numeric comparison could return false positives. Try doing a comparison of 'aa' == 'bb'. With -w on, this does issue warnings, however it will still return true.
That is why something like Fletcher's looks_like_number from Scalar::Util is needed.
I have not used it myself, so I can not attest to the accuracy of this function, however the code would be like this:
sub equals { my ($val1,$val2) = @_; return 1 if !defined $val1 && !defined $val2; if (looks_like_number($val1) && looks_like_number($val2)) { return $val1 == $val2; } else { return $val1 eq $val2; } }
|
|---|