Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

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

by kyle (Abbot)
on Jul 19, 2009 at 16:15 UTC ( #781460=note: print w/ replies, xml ) Need Help??


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.


Comment on Re: What is the best way to compare variables so that different types are non-equal?
Select or Download Code
Re^2: What is the best way to compare variables so that different types are non-equal?
by jdporter (Canon) 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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://781460]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2014-08-30 20:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (293 votes), past polls