Skip to content

Commit ffa23cb

Browse files
tbrowderJJ
authored andcommitted
Expand info on Heredocs
1 parent 60fd5e0 commit ffa23cb

File tree

1 file changed

+35
-3
lines changed

1 file changed

+35
-3
lines changed

doc/Language/quoting.pod6

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ string
472472
END
473473
=end code
474474
475-
The contents of the heredoc always begin on the next line, so you can (and
475+
The contents of the I<heredoc> always begin on the next line, so you can (and
476476
should) finish the line.
477477
478478
=begin code :preamble<sub my-escaping-function { $^x }>
@@ -483,7 +483,7 @@ TERMINATOR
483483
=end code
484484
485485
If the terminator is indented, that amount of indention is removed from the
486-
string literals. Therefore this heredoc
486+
string literals. Therefore this I<heredoc>
487487
488488
=begin code
489489
say q:to/END/;
@@ -503,7 +503,7 @@ some multi line
503503
504504
=end code
505505
506-
Heredocs include the newline from before the terminator.
506+
I<Heredocs> include the newline from before the terminator.
507507
508508
To allow interpolation of variables use the C<qq> form, but you will then have
509509
to escape metacharacters C<\{> as well as C<$> if it is not the sigil for a
@@ -525,6 +525,38 @@ option {
525525
};
526526
=end code
527527
528+
Some other situations to pay attention to are innocent-looking ones
529+
where the text looks like a Raku expression. For example, the
530+
following generates an error:
531+
532+
=begin code :lang<text>
533+
my $title = 'USAFA Class of 1965';
534+
say qq:to/HERE/;
535+
<a href='https://usafa-1965.org'>$title</a>
536+
HERE
537+
# Output:
538+
Type Str does not support associative indexing.
539+
in block <unit> at here.raku line 2
540+
=end code
541+
542+
The right angle bracket makes '$title' look like a hash call
543+
to Raku when it is actually a Str variable, hence the error message.
544+
The cure is to enclose the scalar with curly braces:
545+
546+
=begin code :lang<text>
547+
say qq:to/HERE/;
548+
<a href='https://usafa-1965.org'>{$title}</a>
549+
HERE
550+
=end code
551+
552+
Because a I<heredoc> can be very long but is stll interpreted by Raku
553+
as a single line, finding the source of an error can sometimes be
554+
difficult. One crude way to debug the error is by starting with the
555+
first visible line in the code and treating is as a I<heredoc> with
556+
that line only. Then, until you get an error, add each line in turn.
557+
(Creating a Raku program to do that is left as an exercise for the
558+
reader.)
559+
528560
You can begin multiple Heredocs in the same line.
529561
530562
=begin code

0 commit comments

Comments
 (0)