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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.