Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

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

by LanX (Canon)
on Jul 19, 2009 at 21:06 UTC ( #781484=note: print w/ replies, xml ) Need Help??


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

Is there a way to override this so that things belonging to different data types are always not equal?

I'm not sure if I understood your application, you're comparing "nodes" which can be strings or regexes or other data types, right?

So wouldn't it be natural to construct a class "Node", with an overloaded "eq" operator? ()

If your goal is to compare different node-objects, why don't you implement them as objects?

If you also tie these object-refrences you can use them like normal scalars.

So the answer is yes you might be able override this behaviour ;-)

Cheers Rolf

FOOTNOTE: (1) hmm I think I'd rather prefere to overload ==


Comment on Re^2: What is the best way to compare variables so that different types are non-equal?
Download Code
Re^3: What is the best way to compare variables so that different types are non-equal?
by ELISHEVA (Prior) on Jul 19, 2009 at 23:59 UTC

    My question about overriding the type-blind behavior of eq (#2) was primarily a question about Perl idiom. Before I settled on a solution, I wanted a better understanding of the Perl syntax for handling various definitions of equality.

    I was also looking ahead to future coding scenarios. Part of good design is anticipating the environment around the design. Part of good testing is understanding exactly what one's test for equality is doing. Once I saw my mistake I was worried about what other magic and 'action at a distance' effects I need to consider when writing tests and developing algorithms that involve testing for equality.

    So wouldn't it be natural to construct a class "Node", with an overloaded "eq" operator?

    The code I'm testing is pretty well factored so the actual fix involves exactly two comparisons within a single subroutine. There isn't really a need for a global solution that will be "carried" with a "node object". Also the "node-i-ness" comes from the fact that the datum is part of larger structure, e.g. an array or a hash. It doesn't need an object wrapper to get that trait.

    If there is no ready-made Perl idiom I will probably have my subroutine call the subroutine below for its two comparisons. The subroutine mentioned above needs a definition of equality that duplicates unoverloaded eq, except for the added constraint that like must be compared to like:

    sub my_eq { # make sure we are comparing like to like my $xRef = ref($_[0]); return '' unless ($xRef eq ref($_[1])); # compare pure scalars and regex's using 'eq' # compare reference addresses for the rest return ($xRef and ($xRef ne 'Regexp')) ? (Scalar::Util::refaddr($_[0]) == Scalar::Util::refaddr($_[1])) : ($_[0] eq $_[1]); }

    Best, beth

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2014-07-13 02:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (245 votes), past polls