[Dbix-class] Return value of DBIx::Class::ResultSet::find()

Matt S Trout dbix-class at trout.me.uk
Mon May 14 00:48:48 GMT 2007


On Sun, May 13, 2007 at 10:25:16PM +0200, Bernhard Graf wrote:
> Documentation tells that DBIx::Class::ResultSet::find() returns a row 
> object.
> 
> What it does not tell is what is returned if no row is found. Actually 
> it returns an empty list in list context - not an undef as one might 
> expect.

find() returns -nothing- if nothing is found, not an undef which corresponds
to SQL NULL.
 
> While the difference between empty list and undef is usually not 
> important, it can lead to confusing results if find() is called as 
> subroutine argument:
> 
>   do_something($alpha, $rs->find($id), $omega);
> 
> meaning do_something() is called with three arguments if find() succeeds 
> and two arguments if it fails.

I always do

if (my $obj = $rs->find(...)) {

for clarity.
 
> So since find() is not designed to return lists, to be consistent it 
> should always return a single value: either a row object or undef.

Depends really - is it worth giving up consistency to support the poor
programming practice exemplified above?

Opinions welcome, I'm not -overly- troubled either way although by default
I'd say the current conceptual consistency is the correct answer.

-- 
      Matt S Trout       Need help with your Catalyst or DBIx::Class project?
   Technical Director    Want a managed development or deployment platform?
 Shadowcat Systems Ltd.  Contact mst (at) shadowcatsystems.co.uk for a quote
http://chainsawblues.vox.com/             http://www.shadowcatsystems.co.uk/ 



More information about the Dbix-class mailing list