KBD

Keith Devens .com

Monday, September 8, 2008 Flag waving
All non-trivial abstractions, to some degree, are leaky. – Joel Spolsky (The Law of Leaky Abstractions)
← 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)
:

September 2008
SunMonTueWedThuFriSat
 123456
78910111213
14151617181920
21222324252627
282930 



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

Recent comments XML

new⇒Girls, please don't get breast implants

I got implants this year and it was​the biggest mistake I ever made.  I​am ...

75.156.208.88: Sep 7, 8:03pm

Trogdor tattoo!!

Oh, so cute, wowoowow....

Kathy Mead: Sep 7, 6:07am

I hate Norton Antivirus

Long long live AVG I love you!...

kevin sands: Sep 6, 7:31pm

I hate ASP.NET

CF, why pick that piece of trash?​Cold Confusion. Is it finally​really a OO...

ColdConfusion: Sep 5, 8:36pm

Maps of Iraq

This is for Linda, I will be​visiting that site some time in the​near futur...

Bob: Sep 5, 1:20pm

Spider solitaire

I don't think the question was​necessarily if there are unbeatable​games.  ...

Jared: Sep 4, 12:44pm

Convert Pantone Colors to RGB and Hex - Color Conversion Chart

The colors on those website don't​seem to relate to the pantone data​we hav...

blah: Sep 3, 10:12am

Generated in about 0.18s.

(Used 8 db queries)

mobile phone