Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have this cgi that I build a sql statement in, pass to dbi and do stuff with the results. I want to make the code readable but the following complains if I indent the ending tag.

$sql = <<EOSQL; update employee set fname = '$NS::fname', lname = '$NS::lname', team = '$NS::team' EOSQL >- script complains if this is indented.

Why does EOSQL have to be in column 0 for the above assignment to work? Why can't it just read till it sees EOSQL?

TIA
Travis

2001-03-03 Edit by Corion : Changed PRE to CODE tags, fixed heredoc syntax.

Replies are listed 'Best First'.
Re: Why does $sql = EOSQL; require EOSQL to be in column 0?
by yakko (Friar) on Feb 23, 2001 at 00:39 UTC
    That's the nature of a "here" doc: the end of the doc has to be on the first column. But, according to this FAQ, you can have embedded spaces if you really want them.

    --
    Me spell chucker work grate. Need grandma chicken.

Re: Why does $sql = EOSQL; require EOSQL to be in column 0?
by dws (Chancellor) on Feb 23, 2001 at 00:40 UTC
    To indent, try
    $sql = <<" EOSQL"; ... EOSQL
    Alternatively, you can break at the first blank line by doing
    $sql = <<""; update employee set ...
    Though that's a bit controversial to some.

    Also, I suggest that you consider using bind parameters in the query. E.g.,

    update employee set fname = ?, lname = ?, team = ?
    and then pass the bindings to execute().
      Whenever you have variables in your heredoc, you will want to use double quotes in the example dws suggests. Otherwise the here doc will be single-quotish and the variables will not interpolate:
      $sql = <<" EOSQL"; ... EOSQL
      This technique breaks of course if you change the indenting on the heredoc itself without changing the indent on the '<<' line. So you will need to decide if the readability is sufficient advantage to make up for the maintenance issue.
        FYI, if you leave off the quotes around your here-doc terminator, then you get the double-quotish, not single-quotish, behavior. To get a single-quotish here-doc, you have to explicitly use single quotes.
        $name = 'Bob'; my $text = <<EOT; Hello, $name! EOT print $text;