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

I have a small question on the syntax of what the Camel book calls "here" documents. I have these lines of Perl...

$strSQL =<<END_SQL;
SELECT * FROM TABLE
END_SQL

Now if I place these against the left of the perl file, it works fine, but if I put a tab to move them over formatted in my if statement, perl gives me this...

Can't find string terminator "END_SQL" anywhere before EOF <BR
I just read in the Camel book the section on here document (page 66) and it states...

"The terminating string must appear by itself; unquoted and with no extra white space on either side, on the terminating side."

So, I'm assuming the tabs are why this is happening, but not sure if there's a way around it? I have some huge sql statements and it's a lot easier than putting the doublequotes and the formatting that I'd have to do if not using the <<END_SQL format. I'd rather not have to give up my formatting for this but if I have to it's worth it. Is there a way around this or is this the way it is?

Thanks in advance perlmonks.

Replies are listed 'Best First'.
Re: here documents formatting
by Masem (Monsignor) on Apr 07, 2001 at 04:13 UTC
    You probably have it like:
    while () { print <<END; my text END }
    when, as you are recognizing, needs to be
    while () { print <<END; my text END }
    While you can modify the here document starting point, such as
    print << END;
    where that space is a tab character, this would look worse.

    I've run into the same problem, and for things like sql statements, I find it better to use q() and friends, such as:

    $sth=$dbi->prepare( q/ SELECT * FROM my_table WHERE AVERAGE( cost ) < ? /) or die DBI->errstr;
    The indents in this code will rarely affect SQL, and probably only a few select other cases will it be a problem.


    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
      Ok, that's fine with me. Using the q\\ is still much better than...

      $strSQL = "SELECT * " .
      "FROM TABLE";

      So, I changed it to this...
      $strSQL = q|
      SELECT *
      FROM TABLE|;

      ...and that worked fine. Now at least I know that the other method isn't viable other than up against the left margin of the perl file.

      Thanks for the replies monks.
Re: here documents formatting
by dws (Chancellor) on Apr 07, 2001 at 04:15 UTC
    Try (using spaces):
    $strSQL = <<" END_SQL"; SELECT * FROM TABLE END_SQL
    You can also try $strSQL = <<"\tEND_SQL"; but you're starting to play with fire, since some text editors covert tabs to spaces, mysteriously breaking your scripts in that wonderful "but I didn't touch anything there!" fashion.