[racket] Embedding multiline shell scipts

From: Eli Barzilay (eli at barzilay.org)
Date: Sun Feb 6 19:11:57 EST 2011

6 hours ago, Jens Axel Søgaard wrote:
> 2011/2/6 Jukka Tuominen <jukka.tuominen at finndesign.fi>:
> > I'm trying to embed multiline shell scripts within a scheme function, but
> > I'm not quite sure how to do it.
> 
> Just a quick, ransom tip: Multi line strings can be written
> conveniently with "here strings",
> 
> #<<SCRIPT
> line1
> line2
> ...
> SCRIPT

The scribble reader is generally better for these kind of things.  For
example, the equivalent of a here-string would be something like:

  (define script @string-append{
                   line1
                   line2
                   ...})

that's already better than here strings in that you don't get the
prefix spaces in there (usually you don't care about them, but when
you do, here-strings can be very annoying in that they break the usual
indentation of the code).

Another advantage is that you get "string interpolation" for free.
(In fact, it's not just string interpolation -- it's a little better
than that.)  For example:

  (define some-line "line1")
  (define script @string-append{
                   line1
                   @some-line
                   ...})

As for how things are quoted -- using balanced {}s works fine, and
since it's for literal pieces of text in your code you always know if
that's enough or not.  For rare cases when you'd want a "}" in the
text, you can use an alternative syntax for the delimiters:

  (define some-line "line1")
  (define script @string-append|{
                   line1
                   |@some-line
                   ...}|)

and in the middle of these (the "|{" "|@" and "}|") you can put more
punctuations for extra "safety":

  (define some-line "line1")
  (define script @string-append|=-={
                   line1
                   |=-=@some-line
                   ...}=-=|)

-- 
          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                    http://barzilay.org/                   Maze is Life!



Posted on the users mailing list.