Placeholders

The next obvious target is the VALUES section. There are two problems here. First, it's too long and there are too many repeated references to one variable; It would be better to abbreviate if we could. And second, if any of the @data items contains an apostrophe, the code will break. We can solve the second problem by using the DBI module's "placeholder" feature; this incidentally provides a simple solution to the first problem in the form of an array slice:

Loop hoisting

There's no need to prepare the statement every time through the loop, because it never changes. (This is another benefit of the placeholder approach.) So we can get a performance win by hoisting the prepare out of the loop;

This has the pleasant side-effect of turning the main loop of the routine from something long and complicated into something compact and simple.

The dot operator

Now let's clean up the SQL statement itself. Here we see another red flag: The dot operator. Yes, the dot operator is a red flag! It's rarely necessary, and usually a mistake. Here we can do better by writing the SQL like this:

We've just eliminated eight punctuation characters that were cluttering up the SQL. Now another programmer looking at the string can see it instantly without having to filter out a bunch of superfluous periods and quotation marks. (Note that I also got rid of two superstitious parentheses. Perl has too much punctuation, so it's good to take advantage of opportunities to reduce it.)

Omit needless variables

Finally, the variable $SqlStatement4 is useless. It's only used once, and its name has no documentary value, so we might as well get rid of it:

There are more improvements to be made here. For example, now
that the code is cleaned up, it's easy to see that it can
simply be replaced with a single INSERT statement.
But this is enough for now.

The original code was 815 characters long, and the result is 451 characters, a 45 percent reduction. Programmers have learned to distrust length measurements like this -- so much so that they often automatically reply "But you can reduce the length of any program by squeezing out the whitespace." But here we've made the program shorter while increasing the amount of whitespace. This is not unusual.

For more information

If you found this interesting, you will probably like the class. You may also enjoy the following www.perl.com articles, which represent some of my earlier thinking on this topic.