HEREDOCs

ziggy on 2004-08-27T16:21:46

I'm reading an article about Quixote at the moment, and I came across this Python code, which looks rather ugly to me:

template _q_index (request):
    """
    
    ...
    """
    bug_db = get_bug_database()
    for bug in bug_db.get_all_bugs():
        ...
    """
    ...
    
    """
I've never been a fan of triple-quoted strings. I prefer HEREDOCs, even when they require that extra end token.

Then I started thinking: do HEREDOCs need an end token?:

#!/usr/bin/perl -w

print <<"";
    This is a test.
    This is only a test.

print "\t(you have been warned.)\n";
Apparently not. :-)


Really?

phillup on 2004-08-27T16:30:29

I get:
Can't find string terminator "" anywhere before EOF...

Re:Really?

ziggy on 2004-08-27T16:53:58

What version of Perl? Are you sure there's a blank line between the end of the HEREDOC and the print statement?

I've just tested it with 5.005_03, 5.6.1 and 5.8.0. The script works fine in all three.

Re:Really?

phillup on 2004-08-27T17:38:46

program:
print <<"";
  This is a test.
  This is only a test.
 
print "\t(you have been warned.)\n";
But... I found the problem.

There are two spaces on the blank line due to my editor's "auto indent" feature. (and my stupidity)

I'm using Quanta, which is part of KDE... and I just upgraded it this last weekend and haven't gotten all the kinks worked out in it's configuration.

Sorry for the false alarm.

---

So, should this also work?
#! /usr/bin/perl -w
 
print <<"";
  This is a test.
  This is only a test.
 
my $test = 'what about non-print statements';
print "\t(you have been warned.)\n";
 
print "$test\n";
I suspect tht the HEREDOC (in your example) is really just extending to the end of the file and you got lucky by choosing a print statement. Since you are using double quotes it is getting interpolated and everything works out.

Re:Really?

ziggy on 2004-08-27T17:54:16

I suspect tht the HEREDOC (in your example) is really just extending to the end of the file and you got lucky by choosing a print statement. Since you are using double quotes it is getting interpolated and everything works out.
Um, no. Luck has nothing to do with it.

I don't have an editor that is ever-so-helpfully putting spaces on blank lines when I don't want them there. The HEREDOC ends at the first blank line. Period. There is nothing magical about the second print statement.

Re:Really?

phillup on 2004-08-27T18:19:14

I don't have an editor that is ever-so-helpfully putting spaces on blank lines when I don't want them there. The HEREDOC ends at the first blank line. Period. There is nothing magical about the second print statement.

Dammit! (I did it again... didn't I)

;-)

Confirmed... thanks.

Great minds think alike.

schwern on 2004-08-27T21:37:36

Look inside Class::DBI sometime.

I think it was MJD that I got this from many moons ago. Though I seem to think all good ideas come from MJD, even ones he's denied having. Consider it flattery.

I believe it was at the first YAPC::Europe during my fabulously disasterous live attempt to write a module that I used the "" style in front of an unsuspecting audience. Lots of folks were caught off guard and confused by it causing a lively style debate and general collapse of the talk (probably for the best).

I like it. Especially when your here-doc content contains lots of ALL CAPS like SQL statements as these tend to blend in with the typically ALL CAPS here-doc terminator. In general I'll use an unexpected yet improved style. Stylistic surprises are just another way of expressing an idea that hasn't made the rounds yet, and how else will it get around if its not used? Circular? You betcha.

The common trap is trailing whitespace. "" is not alone in being sensitive to this, normal here-docs are and old POD parsers. This is easily dodged by using an editor that can display trailing whitespace. *emacs can do this, but I forget exactly what variable is you have to flip and fink's xemacs is a bit harebrained. Also avoid autoindent-on-newline, they often do the wrong thing in subtle ways which just leads to tears. Get used to [return][tab].

Re:Great minds think alike.

Aristotle on 2004-08-27T22:19:31

Vim has no problem doing the right thing with autoindent: if you hit [Enter] multiple times, despite Vim placing the cursor at the correct indent on each line, the empty lines will be left completely blank. Only once you enter text or navigate away will the cursor location manifest itself as whitespace.

I've never once had an autoindent-related whitespace surprise.