http://www.perlmonks.org?node_id=468370


in reply to OO Identity Crisis

In my Java classes at school this question came up when dealing with Java collections. Very often a data structure will need to enforce some sort of uniqueness, but there's no way for the data structure designer to know how to compare two objects.

The way Java deals with this is to make the client class provide a comparison method. So, in your example, the Node class should be able to do the following:

if ( $node1->isEqual( $node2 ) ) { print "The two nodes are the same!\n"; }
That's a simple and easy way to prevent duplicates without making your Tree class do more than it should.