in reply to Best way to compare two objects?
"I wish to compare two objects to determine if the attribute values are the same. "
Do you want to compare two whole objects or just one attribute value held by two objects? If it is the former, then this begs the question: why? The latter, however, is fairly easy to solve:
Since you you know which attribute you are comparing, you will know what its type is and how to compare it accordingly.if ($object1->get_attr != $object2->get_attr) { print "they are different!" }
But back to the former case, comparing two whole objects. Rarely does one need to do this, which is probably why you found "very little guidance on this issue anywhere." It is not an issue for most of us. If you find yourself in a situation where you have to compare two whole objects then surely you can decompose the problem a little further. If you simply cannot, then most solutions for comparing whole objects involve writing a method that takes the object in question as an argument and inspects that object. This method (usually a class method) sees if it has every attribute required and if it can perform every method required. What is tricky is when the object in question has other attributes and methods. This is not a good road to take to find your solution. I recommend instead finding the 1 or 2 attributes or methods necessary to determine a delta. If you still insist on taking that road, look into overload the equality operator (=), which is a fairly natural thing to do in full featured OO languages, but not so much in Perl mostly because Perl has better ways to solve those problems. Good luck. :)
jeffa
L-LL-L--L-LL-L--L-LL-L-- -R--R-RR-R--R-RR-R--R-RR B--B--B--B--B--B--B--B-- H---H---H---H---H---H--- (the triplet paradiddle with high-hat)
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Best way to compare two objects?
by nysus (Parson) on Mar 22, 2016 at 19:29 UTC | |
by 1nickt (Canon) on Mar 23, 2016 at 00:53 UTC | |
by nysus (Parson) on Mar 23, 2016 at 15:07 UTC |