1. 35
  1.  

  2. 3

    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.)

    1. 3

      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.

      1. 5

        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:

        #!/usr/bin/env perl
        
        print "here be dragons!\n"
        __DATA__
        {
           "id": 42
        }
        

        and you can read that last bit by passing a filehandle along. Pretty nice to embed simple stuff in test files, for example.

      2. 1

        Note that the <<- heredoc form only works on text indented with tabs, other kinds of white space will be ignored.

      3. 2

        I think Ruby does this a little better, with the so-called “squiggly heredoc” added in 2.3:

        puts <<~HEREDOC
          Because the boss knows
          that what the boss says goes
          if the boss's suffered losses
          then that's what the boss chose!
        HEREDOC
        

        Relevant documentation.