[CDBI] Best way to tell if two objects are the same?

William Ross will at spanner.org
Wed Jan 11 18:18:28 GMT 2006


On 11 Jan 2006, at 15:10, Bill Moseley wrote:

> On Wed, Jan 11, 2006 at 09:42:59AM -0500, Michael Peters wrote:
>>> I need to know if $obj1 eq $obj2, and I'm wondering the best way.  I
>>> suppose ref($obj1) eq ref($obj2) would fail without Live_Object  
>>> index.
>>
>> You mean if they are the same object or refer to the same row in  
>> the database.
>> If object, then simply comparing the address given by  
>> Scalar::Util::refaddr
>> would be enough.
>
> That relies on Live_Object index being in use (to see if they point to
> the same row), IIUC.
>
>> If you want to check if they are the same row in the db, then you  
>> could compare
>> their table() and id() to see if they are the same.
>>
>> Is that what you were asking for?
>
> I just thought that it might be a common operation.  I kind of
> expected for there to be a CDBI method available, then I saw that code
> that generates the unique key for %Live_Object is inlined.
>
> Might be a nice use for '==' overload.

just calling id() should work for single-column keys, but with mcpk  
you would need to call it in list context and compare the lists you  
get back. The easier way to do essentially the same thing is to call  
stringify_self() (in which case you can manage special cases by  
defining a Stringify column set) or _live_object_key() (which also  
includes the class name).

The overload does seem like a good idea. Maybe just:

   use overload 'cmp' => sub { $_[0]->_live_object_key cmp $_[1]- 
 >_live_object_key };

I'm stuck trying to add proper access control to php muppetware so I  
can't test any of this :(

will





More information about the ClassDBI mailing list