Saturday, November 11, 2006

why I like perl

At work, we're primarily a java shop but yesterday I had to edit a perl module that is used by our editorial department. I looked at an existing function and wanted to refactor it since there was a lot of code duplication. To do so, I needed to dynamically generate an sql statement. The statement was of the form:

Insert into table (col1, col2, .. colx) values (?,?,...,?)
or Replace into table (col1, col2, .. colx) values (?,?,...,?)

Array @columns contains the name of the columns. so to generate the string "col1, col2, col3", it's just: $string1 = join ',', @columns

now to generate the "?,?,?" strings, it's just $string2 = join ',', map '?', (1.. scalar @columns)

No comments: