MyGGaN
MyGGaN

Reputation: 1806

How to format SQL statements in perl (code-style)

I'd like to please perlcritic and have easy to read SQL queries in my code. Currently this is how I do it:

if (defined $dbh) {
    $sth = $dbh->prepare(<<'SQL'
SELECT firstname, lastname, email, country
FROM Person
JOIN Country ON Person.countryID = Country.ID
WHERE age = ?
  AND sex = ?
LIMIT 100
SQL
    );
}

The statement is not indented at all which makes it pop if the context is indented. I find this nice since it makes it easy to spot the statement and they're split into several lines to make them easy to read.

However, if the context is not indented this looks really messy. And if possible I don't like to introduce newlines or unnecessary white spaces in my stmt. Also SQL at the end of the stmt and the dangling ); is not nice either.

Please suggest a good style guide on this matter that I can adopt.

Upvotes: 0

Views: 610

Answers (1)

Dave Cross
Dave Cross

Reputation: 69274

I'd recommend removing all SQL from your program completely by switching to DBIx::Class. If that's a little scary, then perhaps a halfway house like Data::Phrasebook::SQL.

In general, having raw SQL statements in your Perl code is a pretty bad idea.

Upvotes: 6

Related Questions