Accessor returning objects not values

Problem: In a simple loop through some search results, I kept getting much more than I expected from $object->company_id where company_id is a method created by Class::DBI to access a field in my table called company_id - Instead of returning a simple integer (which is what I expected), it returns a blessed reference!? This seems extremely silly and annoying. I was simply trying to create an array of all matched company_id values, so the array should look like (1, 2, 3, 4). It sort of worked. I think there is some overloading going on or something, but I ended up having to use the Perl built-in int function to cast the return value of the method call like this: push @my_array, int($object->company_id); I don't understand why a simple accessor should be returning blessed references.

Answer: Simple accessors don't return blessed references - only accessors that you've overridden - usually by setting up a has_a on that column. If that's not the case, you probably have a bug in your code. This is not likely to be a Class::DBI problem, or else thousands of other people would have been complaining about it. If you can't find the problem, try posting your code to the mailing list.

Comment: Without a has_a the call $object->company_id</tt> returns the value of the column. When you add a has_a</tt> it does indeed return a blessed reference which can be very handy at times, i.e. $object->company_id->company_name</tt>. If you want the original id then you can also do it as $object->company_id->id</tt>, which does not require any extra trips to the datbase.

Although this looks a little strange, most CDBI users either name their foreign key columns after the table to which they refer, or set up a custom accessor name for *_id columns, so that this becomes a more understandable $object->company->id</tt>.