Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Comparing objects for equality

Thread Tools

Comparing objects for equality

Jens Thoms Toerring
Posts: n/a

I have often to compare objects for equality that are created
for an entry in a database, where equality means that the objects
refer to the same entry in the database. The normal '==' operator
of course only tells me if the objects are identical in the sense
that their addresses are the same, so it won't work for objects
created for the same database entry. But this is nearly never what
I'm really interested in so I usually overload the '==' (and '!=')
operator to do something like

use overload '==' => sub { die 'Invalid comparison'
unless $_[ 0 ]->isa( __PACKAGE__ ) and
$_[ 1 ]->isa( __PACKAGE__ );
$_[ 0 ]->id == $_[ 1 ]->id },
'!=' => sub { die 'Invalid comparison'
unless $_[ 0 ]->isa( __PACKAGE__ ) and
$_[ 1 ]->isa( __PACKAGE__ );
$_[ 0 ]->id != $_[ 1 ]->id },
fallback => 1;

with '$object->id' being the method to determine the primary key
of the entry in the database for '$object' (it gets more inter-
esting if there isn't a simple primary key). While this works
quite fine so far I am wondering if there is some better, more
elegant (and maybe even faster method to do such a comparison
for (in-) equality
Regards, Jens
\ Jens Thoms Toerring ___ Removed)
Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Pointer Equality for Different Array Objects Shao Miller C Programming 11 02-06-2012 07:25 AM
Comparing fp types for equality Edward Rutherford C Programming 12 12-20-2011 06:17 PM
comparing Locales for equality and degree of specificity tom forsmo Java 2 04-18-2007 02:08 PM
comparing doubles for equality John Smith C Programming 12 01-03-2007 01:14 PM
Comparing two files for equality Edgardo Hames Ruby 11 01-18-2005 05:53 AM