Re: Multi-line string extension

Tim Peters (tim@ksr.com)
Fri, 15 Apr 94 15:42:21 -0400

> [don, wanting to ease multi-line strings]
> ...
> big_string = \
> %(
> this is a really big multi line string that
> has embedded "double quotes" and 'single quotes'
> %)
>
> the resulting string would be equivalent to
>
> big_string = \
> 'this is a really big multi line string that \n' +\
> 'has embedded \"double quotes\" and \'single qoutes\''

This is a little surprising, because your equivalent doesn't have a
newline at the end of the "has" line (but does at the end of the "this"
line).

Note that Perl has three syntactically horrific (but pragmatically
delightful!) variations on the theme, all illustrated in this Perl
fragment:

$substitution = 'piece of substituted text';

print <<HERE1, <<'HERE2', <<`HERE3`;
this is a really big multi line string that
has embedded "double quotes" and 'single quotes'
and a $substitution
HERE1
and this is another chunk
but $substitution is suppressed because of the
single quotes on 'HERE2'
HERE2
echo "and here's yet another"
echo produced by shell command `echo SUBSTITUTION | tr A-Z a-z`
echo because of the backticks on HERE3
HERE3

The "<<" notation is kinda like shell "here documents", except more
flexible, and (as in the above) stackable. Running the above prints

this is a really big multi line string that
has embedded "double quotes" and 'single quotes'
and a piece of substituted text
and this is another chunk
but $substitution is suppressed because of the
single quotes on 'HERE2'
and here's yet another
produced by shell command substitution
because of the backticks on HERE3

I happened to print out the string blocks directly there, but the

... <<LABEL ...
line
line
line
...
LABEL

device works anywhere a string literal is allowed.

Judged by what these mechanisms _do_ and allow, they're just about
perfect. Or are in the context of Perl <wink>.

> ... And as an extra bonus, it would generate code that was much more
> efficient.

But this point's weak; Python's implementation could be fiddled to
collapse catenated string literals at translation time, if anyone cared
enough to bother doing it.

some-things-are-better-in-perl-precisely-*because*-perl's-syntax-is-
an-anarchic-mess<0.9-grin>-ly y'rs - tim

Tim Peters tim@ksr.com
not speaking for Kendall Square Research Corp