Hint: Formatting SQL in Perl code

janus on 2010-01-27T12:09:42

Usually i'm telling people NOT to put their SQL into the code, pretty much the same as with templates.

As that needs code to get the SQL from the outside it isn't always what you want. And with SQL there's more than just ``put different languages into different files'', it's also about formating.

To achieve that i was using heredocs for a while with different delimiters until i realized that a semicolon is the best option i can think of: $sql = <<';'; SELECT * FROM foo WHERE id = $1 ;

... which is also pretty nice for copy&waste and has no quoting issues either.


I use q{}

grantm on 2010-02-02T07:05:44

I've never been a big fan of heredocs - mainly because they break indentation. When I put SQL in code, I tend to use the single quote operator with curly braces for delimiters:

$sql = q{
    SELECT
            *
    FROM foo
    WHERE
          id = $1
};

Re:I use q{}

Aristotle on 2010-02-02T19:43:00

$sql = q;
    SELECT
            *
    FROM foo
    WHERE
          id = $1
    ;