Nice to know about – I’ve got a few python scripts that’ll help clean up a bit.
(Note also the <<- here-doc variant, which is similarly convenient when writing shell scripts.)
Here docs/strings are awesome.
Another use case I like (beyond ascii art) is embedding test text file contents in a string along with the test itself.
When you come back to it later, instead of the indirection of looking up the contents of an external file and cluttering up the file system, you have it right there with the test.
Perl has __DATA__, which is places at the end of the code in the file, and everything which comes after you can read with the DATA filehandle (I don’t know where Larry stole this idea from).
So a file looks like:
print "here be dragons!\n"
and you can read that last bit by passing a filehandle along. Pretty nice to embed simple stuff in test files, for example.
Note that the <<- heredoc form only works on text indented with tabs, other kinds of white space will be ignored.
I think Ruby does this a little better, with the so-called “squiggly heredoc” added in 2.3:
Because the boss knows
that what the boss says goes
if the boss's suffered losses
then that's what the boss chose!