

Ask HN: The importance of heredoc - stcredzero

I wish to ask Rubyists and Shell programmers: Does heredoc still have a place?  Is it still worth its syntactic weight?  I can see how it would be great for one-off scripts producing a bit of text.  But I can picture a number of ways to get almost the same effect (readable one-offs producing text) in a scripting language without complicating the syntax.
======
gamache
Oh, heredocs absolutely still have a place. It's the right choice for any
multiline string which might contain quoting characters.

And what's so complicated about the syntax? Even if you have never seen a
heredoc before, it's generally very easy to figure out what's going on from
context.

~~~
stcredzero
The syntax is a complication to _implement_ , not to just program with.

------
mbrubeck
Perl and PHP also use shell-style heredoc syntax. The main alternative is to
provide a newline-friendly quote delimiter, like the """string""" syntax in
Python.

------
villageidiot
I've never had a problem with heredoc. I'm curious what you mean when you say
"I can picture a number of ways to get almost the same effect . . . without
complicating the syntax". Would you care to illustrate your point with an
example so we can make the discussion a little bit more concrete?

~~~
stcredzero
Put the text from a heredoc in a comment with special tags. Have a special
method that lets you retrieve the text in the same source file. This puts
_zero_ syntactic load on the language, but has much the same effect. You can
even have embedded methods, source debugging, regexes. But there's no
additional syntax.

------
firebug
heredocs are great. I use them all the time for metaprogramming. I don't see
them going out of style anytime soon. Not sure what you mean about complicated
syntax.

I supect you may be new to Ruby and confused by the heredoc sytnax. If so,
this post has a review of some of the syntactical issues with heredocs:

[http://log.gmarik.info/2007/12/rubys-here-document-
heredoc-m...](http://log.gmarik.info/2007/12/rubys-here-document-heredoc-
mini.html)

Hope that helps.

~~~
stcredzero
Not confused. Heredoc is one of the things that complicates Ruby syntax from
the implementer's POV.

Use heredoc for metaprogramming? Now I'm starting to believe in the Lisp
dogma.

