I've been thinking about this for a little bit. My current context is in web programming, but I think this could be applied to a wider variety of programs.

What, in your opinions, is the best way to populate a list of objects from a database?

In the past, I've done things without classes at all, looking something like this:

while( @info = $sth->fetchrow_array ) { $f_name = $info[0]; $l_name = $info[1]; ... # do what is necessary with these variables. }

But it seems like there should be a separation of getting all of the information about what could be an object, and what to do with that information. One thought would be something like:

my @objects; while ( @info = $sth->fetchrow_array ) { my $object = Object->new(); $object->set_fname( $info[0] ); $object->set_lname( $info[1] ); ... # or my $object = Object->new( @info ); # or my $object = Object->new( fname => $info[0], lname => $info[1], ... ); push @objects, $object } for ( @objects ) { # do something with the object }

Another thought would be to create a sub to populate the objects for me. When I think about this, though, I think about two things, really...

  1. Somehow pass an SQL string to the method, but that causes a few problems: the SQL logic would have to return exactly the right fields, but the function wouldn't be able to enforce it; and it doesn't look like you can use placeholders.
    @objects = Object::populate( "SELECT fname, lname, ... FROM object WHE +RE title='programmer'" )
  2. Add some abstraction, so in the options of the subroutine there is a way to specify what objects we want to get (assuming we don't want them all). But that would undoubtedly be less flexible than using SQL.
    @objects = Object::populate( title => "programmer" );

Maybe this is a bit of a meditation, but I'm definitely looking for some monks experienced in doing things like this to slap me with some wisdom, so let me know what you've done, and what you think is practically the best solution to populating a list of objects from a database. I'm sure there are some good ways to do this. I hope my question is clear. Thanks!

    -Bryan


In reply to Best Practices: Populating Objects from Database by mrborisguy

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.