Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: A simple Comparison

by btrott (Parson)
on Jun 30, 2000 at 21:11 UTC ( #20628=note: print w/ replies, xml ) Need Help??


in reply to A simple Comparison

If you compare them using eq or ==, all you're doing is comparing their values as references. And since they're two different objects--ie. not references to the same object--they're two different references. So they won't be equal.

To do a proper object comparison, you'll need to write a method that compares two of your objects. One possibility would be to serialize the objects and compare the serialized strings:

my $obj1 = new Obj; my $obj2 = new Obj; ## The two objects above are now equal, let's say. ## Serialize them, then compare the serialized ## data. use Storable qw/freeze/; if (freeze($obj1) eq freeze($obj2)) { ## They're the same. }
In fact, you could even use overload to overload the comparison operator so that the proper comparison happens automagically:
package Obj; use Storable qw/freeze/; use overload '==' => \&compare; sub compare { return freeze($_[0]) eq freeze($_[1]) }
Now you can just say:
if ($obj1 == $obj2) { # They're the same. }


Comment on Re: A simple Comparison
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (12)
As of 2014-07-28 12:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (197 votes), past polls