KBD

Keith Devens .com

Thursday, March 18, 2010 Flag waving
Follow the path. – me
← magpiebrain - A comparison of Django with RailsJoin Java, concurrency extensions to Java →

Daily link icon Wednesday, August 17, 2005

Heredoc syntax

Because I always forget...

Perl heredoc syntax:

$_ = <<EOF;
text goes here
EOF

PHP heredoc syntax:

<?php
$str 
= <<<EOF
text goes here
EOF;
?>

Ruby heredoc syntax:

str = <<EOF
text goes here
EOF

Of course, Python's heredoc syntax is easy to remember:

str = """text goes here
"""

(text is on the same line as the """ for parity with the other examples)

← magpiebrain - A comparison of Django with RailsJoin Java, concurrency extensions to Java →

Comments XML gif

quackeroo wrote:

And in Ruby:

str = <<EOF
text goes here
EOF
∴ quackeroo | 17-Aug-2005 2:47pm est | #8132

Keith (http://keithdevens.com/) wrote:

Hey, thanks. I updated the main post. I need to find a link to the Ruby documentation for that so I can link it too...

Keith | 17-Aug-2005 3:04pm est | http://keithdevens.com/ | #8133

quackeroo wrote:

Hmm. Can't find any good direct links. It's in the PickAxe on this page under the Strings section, though.

∴ quackeroo | 18-Aug-2005 5:05am est | #8136

joesb wrote:

In Python case it doesn't look like heredoc to me.
How do you represent """ in Python's heredoc without escaping it?

∴ joesb | 3-Mar-2006 4:49am est | #9238

Keith (http://keithdevens.com/) wrote:

How do you represent """ in Python's heredoc without escaping it?

You don't. You need to escape one of the quotes. But how does that make it not like heredoc?

How do you represent "EOF" at the beginning of a line in the Perl heredoc? You don't. You'd have to choose a different delimiter. The Python triple-quoted strings, in comparison, can represent all possible strings.

Keith | 3-Mar-2006 11:59am est | http://keithdevens.com/ | #9241

81.56.229.109 wrote:

joesb is right. what makes heredoc syntax appart is that you nether have to quote anything inside the text. you just have to choose your delimiters so that they do not occur in the textual content.
Python's triple quoting is just a like single quoting, but the delimiter being longuer, you'r less likely to find it in the text, so you have less odds to need escaping.
one could design a language where text is enclosed by 'WAZAA!!TEXT_HERE'put you text here'WAZAA!!TEXT_HERE', that's not heredoc syntax.

∴ 81.56.229.109 | 25-Mar-2006 6:58pm est | #9354

Feel free to post a comment below. Please see my comment policy.

Formatting Rules (No HTML):

  • **bold**, *italic*, _underlined_, --strikeout--
  • "text"="url" creates a link, and URLs are auto-highlighted
  • Blockquote: Like e-mail, begin paragraph with > (greater-than sign)
  • Lists: begin paragraph with *,-, or + (unordered), or # (ordered)
  • Code block: ?!code:language=perl|php|sql|javascript|etc.{\n}...{\n}?!/code

:
(will be your IP address if blank)
: (optional)
(Will not be shown on site)

: (optional)
:

March 2010
SunMonTueWedThuFriSat
 123456
78910111213
14151617181920
21222324252627
28293031 



RSS feed RSS feed for Keith's Weblog
Atom feed Atom feed for Keith's Weblog
Weblog archive
Recent comments
  on 2 posts

Recent comments XML

I hate ASP.NET

I hate ASP... I was doing wonders​with PHP, then suddenly one of my​clients...

Johnies: Mar 17, 6:14am

Quantum physics and free will

I knew you were going to say that....

Tom Massey: Mar 15, 9:26pm

Generated in about 0.102s.

(Used 8 db queries)