Clear questions and runnable code get the best and fastest answer |
|
PerlMonks |
Re: OO Identity Crisisby herveus (Prior) |
on Jun 20, 2005 at 15:34 UTC ( [id://468389]=note: print w/replies, xml ) | Need Help?? |
Howdy!
Another way to look at the problem is "how do I find a given node in the collection?" If the items in the collection are unblessed scalars, you can compare them directly. The more "interesting" case is where the items are blessed scalars (or "objects"). The default comparison is "==". If they come up with the same address, they are the same thing. If ref(x) ne ref(y), the items are probably not equal, but your situation may permit objects of different classes to still come up "equal". Any deeper comparison *must* (as you note) be a method on the object. It's already been noted that Java provides an "equals" method in the uber-superclass "Object", so you can always invoke .equals on an object. Something along those lines for your node class(es) would be indicated. Pick an appropriate name for the method and go forth. is_duplicate could be a wrapper around a find() method that returns the given object if it is in the collection, or some other useful value. find() could be responsible for iterating over the collection in an appropriate manner, applying the comparison function.
yours, Michael
In Section
Seekers of Perl Wisdom
|
|