http://qs321.pair.com?node_id=781460


in reply to What is the best way to compare variables so that different types are non-equal?

  1. 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.

  2. 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."

  3. ...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{""} )).

  4. ...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

    or

    sub comparifier { no warnings 'uninitialized'; # because blessed() and reftype() can r +eturn undef. blessed($_[0]).reftype($_[0]).$_[0] eq blessed($_[1]).reftype($_[1]).$_[1] }
      That can result in false positives.
      my $r = \@a; print comparifier($r, "ARRAY$r") ?1:0,"\n"; # 1

        True enough.

        sub comparifier { no warnings 'uninitialized'; # because blessed() and reftype() can + return undef. my $sep = pack 'N', rand 2**15; blessed($_[0]) . $sep . reftype($_[0]) . $sep . $_[0] eq blessed($_[1]) . $sep . reftype($_[1]) . $sep . $_[1] }
        Between the mind which plans and the hands which build, there must be a mediator... and this mediator must be the heart.