in reply to What is the best way to compare variables so that different types are non-equal?
- For what other pairings of data types does eq ignore type?
Basically every type. It forces each argument to a string before making a comparison.
- Is there a way to override this so that things belonging to different data types are always not equal?
I'd probably write a custom comparison sub to do it.
use Scalar::Util qw( blessed reftype ); sub comparifier { blessed $_[0] eq blessed $_[1] && reftype $_[0] eq reftype $_[1] && $_[0] eq $_[1] }Along the same lines, you might be interested in Reference assessment techniques and how they fail, whose subtext is "gosh, it's hard to figure out what a scalar really is."
- ...how can I detect overloaded operators?
Overloading applies to objects, not operators. (In Perl 6 you can override operators and/or write your own.) If you want to know if an object is overloading, use overload::Overloaded( $obj ). If you want to know if it's overloading eq in particular, you can check overload::Method( $obj, 'eq' ), but you'll also have to look for stringification (overload::Method( $obj, q{""} )).
- ...how should I understand it?
Just that eq forces its operands to be strings in order to do its job. A regular expression stringifies as you've found (it's a blessed reference to an undef with regex magic added).
You might also be interested in overload::StrVal( $obj ), which gives you the string value of $obj without string overloading. For a regular expression, this is similar to "Regexp=SCALAR(0x1d10860)".
I hope this helps.
Updated to fix a thinko in comparifier, thanks to AnomalousMonk.
Updated again thanks to jdporter.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: What is the best way to compare variables so that different types are non-equal?
by jdporter (Paladin) on Jul 20, 2009 at 11:21 UTC | |
by ikegami (Patriarch) on Jul 21, 2009 at 17:56 UTC | |
by jdporter (Paladin) on Jul 21, 2009 at 22:27 UTC |