Masem gave a good answer and I want to extend it by pointing out some other reasons for not using SELECT * FROM foo.

I was puzzling through some old code trying to figure out what was going on. The programmer had used fetchrow_hashref (with the SELECT *) to grab a bunch of data that controls client customization. Later on in the code, I saw that he was doing some more selects to grab display information. I used Data::Dumper to dump the results of his first SELECT and lo, he already had the information! It turns out that we had a poorly normalized database and the data was stored in two different places. The programmer didn't realize this, but the original SELECT * hid from him exactly what fields he was grabbing. The moral? Specifying field names is self-documenting if the fields are named correctly.

Tell me what type of data the following returns:

SELECT * FROM customer WHERE customer_id = 7

You could make some good guesses, but unless you go to the database or print out the return data, you're not going to know. Making self-documenting code is incredibly important and should never be overlooked. Not forcing yourself to jump through hoops to look up data is a good example of the Perl virtue "Laziness".

Another reason for naming the fields you want is the idea that your code should do as little as possible. If you have 30 fields in a table, but only need two, why grab all 30? Yet another good example of "Laziness".

Lastly, ignoring the performance issue, we all know one of the worst problems with hashes:

$total_orders += $salesman{ orders_recieved };

In the above example, 'received' is spelled wrong. If you're not careful in your code, that undef field can evaluate as zero and your total orders are incorrect. That's always been a problem with hashes and you'll find many modules that attempt to rectify this (and this is part of the reason the ill-fated "pseudo-hash" was created).

Cheers,
Ovid

Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.


In reply to (Ovid - more "Death to Select Star") Re: Module Pondering by Ovid
in thread Module Pondering by MZSanford

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.