All I can suggest is comment, comment, comment.

But let's look at some code. Your first snippet is

my $sth = $dbh -> prepare("insert into table1 (col1, col2, col2) value +s (?, ?, ?)");
This wraps a little ugly; I'd change it to
my $sth = $dbh->prepare( "insert into table1 (col1, col2, col2) values (?, ?, ?)");
That way, the entire SQL statement is on a line by itself. A little lower you have
my $updater = $dbh -> prepare("update table1 set col1 = ?, col2 = ?, c +ol3 = ? where col1 = ? and col2 = ? and col3 = ?");
I'd change that to
my $updater = $dbh -> prepare( "update table1 set col1 = ?, col2 = ?, col3 = ? \ where col1 = ? and col2 = ? and col3 = ?");
That makes the variables all line up (I know, it's not to everyone's taste.) Although I ponder at the logic of that statement .. if the row values are already set to those values, what good is setting them to those same values? Usually when I do an update, I get an id value for the row that I'm going to change, and do something like
my $updater = $dbh->prepare( "update table1 set col1 = ?, col2 = ?, col3 = ? where id = $ID");
Generally I believe you want to use placeholders like ? when you are going to be inserting or updating many rows. For one-offs it isn't really necessary.

In general style terms, I try to stick with an 80 character limit on line lengths. I may have a big monitor now, but maybe later I'll have to look at my code on my client's 14" screen in character mode. Then I be lookin' stoopid.

Make your code as readable as possible -- if you can't read your own code in three months time, what chance does anyone else have?

--t. alex

"Excellent. Release the hounds." -- Monty Burns.


In reply to Re: dbi style questions (code, discussion) by talexb
in thread dbi style questions (code, discussion) by deprecated

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.