On 2008-01-12 11:51:42 +0100, Dr.Ruud wrote:
> Colin Wetherbee schreef:
> >    my $sql = q(SELECT departure_date, eq.name AS equipment,
> >      dp.full_city AS departure_city, ap.full_city AS arrival_city,
> >      ca.name AS carrier_name, number
> >      FROM jsjourneys
> >      FULL OUTER JOIN jscarriers AS ca ON jsjourneys.carrier = ca.id
> >      FULL OUTER JOIN jsequipment AS eq ON jsjourneys.equipment = eq.id
> >      JOIN jsports AS dp ON jsjourneys.departure_port = dp.id
> >      JOIN jsports AS ap ON jsjourneys.arrival_port = ap.id
> >      ORDER BY departure_date);
> >
> > (As an aside, how do you guys quote your queries?  I find that for
> > anything longer than about 60 characters, q() and '' and everything
> > else start to look horribly inelegant.)
> 
> 
> my $sql = <<'SQL';
> 
> SELECT
>     jo.departure_date  AS  departure
> ,   eq.name            AS  equipment
> ,   dp.full_city       AS  departure_city
> ,   ap.full_city       AS  arrival_city
> ,   ca.name            AS  carrier_name
> ,   jo.number
> 
> FROM
>     jsjourneys   AS  jo
[...]
> SQL

I almost never use here documents because they cannot be properly
indented:


sub foo {
    some;
    code;
    here;
    if (bla) {
        more;
        code;
        here;
        my $sql = <<'SQL';
SELECT
    jo.departure_date  AS  departure
,   eq.name            AS  equipment
,   dp.full_city       AS  departure_city
,   ap.full_city       AS  arrival_city
,   ca.name            AS  carrier_name
,   jo.number
[...]
SQL
        even;
        more;
    }
    code;
    here;
}

just looks terrible. Of course with SQL leading whitespace doesn't
matter so you can just indent the whole statement and just have the
dangling terminator at the left edge[1] but that doesn't work for
multiline strings in general. Putting here documents in a function of
their own as proposed by Stephen helps, but the indentation is still
inconsistent.

Since initial whitespace doesn't matter in SQL, I'd just write that as:

sub foo {
    some;
    code;
    here;
    if (bla) {
        more;
        code;
        here;
        my $sql = q{
            SELECT
                jo.departure_date  AS  departure
            ,   eq.name            AS  equipment
            ,   dp.full_city       AS  departure_city
            ,   ap.full_city       AS  arrival_city
            ,   ca.name            AS  carrier_name
            ,   jo.number
            [...]
        };
        even;
        more;
    }
    code;
    here;
}

(actually, I'd put the commas at the end of the lines)

As an aside, the SPL programming language[2] allows the terminator of a
here document to be indented and to strip off everything up to and
including some character from each line, so that could be written like
this:

function foo() {
    some;
    code;
    here;
    if (bla) {
        more;
        code;
        here;
        var sql = >>SQL|
            |SELECT
            |    jo.departure_date  AS  departure
            |,   eq.name            AS  equipment
            |,   dp.full_city       AS  departure_city
            |,   ap.full_city       AS  arrival_city
            |,   ca.name            AS  carrier_name
            |,   jo.number
            |[...]
        SQL;
        even;
        more;
    }
    code;
    here;
}

> BTW, some editors recognize certain heredoc-delimiters (like SQL) and
> switch language for color coding, autocompletion, etc.

That's a neat feature of course and an argument for using
here-documents.

        hp


[1] No, I don't think »my $sql = <<'        SQL';« is a good idea.

[2] http://www.clifford.at/spl


-- 
   _  | Peter J. Holzer    | If I wanted to be "academically correct",
|_|_) | Sysadmin WSR       | I'd be programming in Java.
| |   | [EMAIL PROTECTED]      | I don't, and I'm not.
__/   | http://www.hjp.at/ |   -- Jesse Erlbaum on dbi-users

Attachment: pgpnuYyeCkdLs.pgp
Description: PGP signature

Reply via email to