6

In JavaScript (ES6), you can use template literals (``) to create multiline strings as shown in the following example:

const html = `
  <div>
    <p>Raku is <b>ofun</b>.</p>
  </div>
`

What's the Raku equivalent of this?

uzluisf
  • 2,586
  • 1
  • 9
  • 27

1 Answers1

11
my constant html = '
  <div>
    <p>Raku is <b>ofun</b>.</p>
  </div>
';

works fine in Raku.

However, you probably want to use a so-called heredoc:

my constant html = q:to/HTML/;
  <div>
    <p>Raku is <b>ofun</b>.</p>
  </div>
HTML

omits the first newline but is otherwise the exact equivalent. If you want to interpolate variables, you can change q to qq:

my $lang = <Raku Rust Ruby>.pick;
my $html = qq:to/HTML/;
  <div>
    <p>$lang is <b>ofun</b>.</p>
  </div>
HTML

For way more than you probably want to know about quoting, see Quoting Constructs

raiph
  • 31,607
  • 3
  • 62
  • 111
Elizabeth Mattijsen
  • 25,654
  • 3
  • 75
  • 105
  • 3
    Also, if you don't want the final newline, you can do `qq:to/HTML/.chomp`. Technically, you could also do `.chop`, but I find `.chomp` to be mnemonically better is it indicates a newline being removed. – Elizabeth Mattijsen Jul 06 '23 at 08:33