I haven't used Class::DBI for a lot of projects, but I've been looking for such a method myself and haven't found one. Here's how I normally handle it, assuming username is a unique column that isn't the primary key:
my ($username) = My::DBI::Users->search( username => 'foo' ); # Could also do: my $username = (My::DBI::Users->search( username => 'foo' ))[0]; # Or maybe (haven't tried this one) my $username = My::DBI::Users->search( username => 'foo' ) ->next();
In the first example, search will generate a list, the first element of which we save to $username and anything else is thrown away. Since we know this is a unique field, there should only be one result, so nothing is really lost.
In the second, we directly specify the first element.
In the third, search is being called in scalar context, so it will return an iterator. We immediatly grab first result and throw the iterator away. This one will probably fail with errors like "calling method on undefined value" if there were no results.
I usually use the first method.
----
I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
-- Schemer
:(){ :|:&};:
Note: All code is untested, unless otherwise stated
In reply to Re: Class::DBI 'distinct' records
by hardburn
in thread Class::DBI 'distinct' records
by grazer
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |