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:
In fact, you could even use overload to overload the comparison operator so that the proper comparison happens automagically: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. }
Now you can just say:package Obj; use Storable qw/freeze/; use overload '==' => \&compare; sub compare { return freeze($_[0]) eq freeze($_[1]) }
if ($obj1 == $obj2) { # They're the same. }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: A simple Comparison
by njh@bandsman.co.uk (Acolyte) on Jul 24, 2015 at 14:19 UTC | |
by choroba (Cardinal) on Jul 25, 2015 at 00:00 UTC |
In Section
Seekers of Perl Wisdom