From 4388411fec6daa020a997f4a52f167215e9cf75d Mon Sep 17 00:00:00 2001 From: Karl Williamson Date: Sat, 31 May 2025 07:27:22 -0600 Subject: [PATCH] perlop: Clean up here-doc documentation The documentation for indented here-docs had a bunch of duplicated concepts with the documentation of plain here-docs. This commit melds them into a single coherent section. --- pod/perlop.pod | 305 +++++++++++++++++++++++++++++-------------------- 1 file changed, 184 insertions(+), 121 deletions(-) diff --git a/pod/perlop.pod b/pod/perlop.pod index a6f0a00bae5f..a1d19b1a6970 100644 --- a/pod/perlop.pod +++ b/pod/perlop.pod @@ -2875,85 +2875,151 @@ must use an C: eval "tr/$oldlist/$newlist/, 1" or die $@; -=item C<< < >> -X X X X<<< << >>> +=back + +=head3 Here-docs +X X X X X<<< << >>> + +=over 4 + +=item C<< < >> + +=item C<< <<~I >> A line-oriented form of quoting is based on the shell "here-document" syntax. Following a C<< << >> you specify a string to terminate the quoted material, and all lines following the current line down to the terminating string are the value of the item. -Prefixing the terminating string with a C<~> specifies that you -want to use L (see below). +An example is -The terminating string may be either an identifier (a word), or some -quoted text. An unquoted identifier works like double quotes. -There may not be a space between the C<< << >> and the identifier, -unless the identifier is explicitly quoted. The terminating string -must appear by itself (unquoted and with no surrounding whitespace) -on the terminating line. + my $endng = < function call don't have to be omitted: -=item Single Quotes + print uc(< -being treated as two backslashes and not one as they would in every -other quoting construct. + HELLO, JOHN! + AND THE TEXT GOES ON. -Just as in the shell, a backslashed bareword following the C<<< << >>> -means the same thing as a single-quoted string does: +And you can intermix a here-document with other things: - $cost = <<'VISTA'; # hasta la ... - That'll be $10 please, ma'am. - VISTA + print <> and the identifier unless +the terminator is quoted, as demonstrated in the example just above. +Quoting rules for the terminator are unrelated to Perl's quoting rules. +Only C<"">, C<''>, and C<``> can be used to quote it, NOT C, +C, and the like. The only interpolation is for backslashing the +quoting character: + + print << "abc\"def"; + testing... + abc"def + +The terminating string must appear by itself (unquoted and with no +surrounding whitespace) on the terminating line. Also, it cannot span +multiple lines. The general rule is that the identifier must be a +string literal. Stick with that, and you should be safe. + +Don't forget that you have to put a semicolon on the end to finish the +statement, as Perl doesn't know you're not going to try to do this: + + print < and no line terminator. + +If you want your here-doc to not have a line terminator on the final +line, use C. + + chomp($string = <<'END'); + This is the first line. + This second line won't end in a \n. + END + +If you use a here-doc within a delimited construct, such as in C, +the quoted material must still come on the line following the +C<<< <>> marker, which means it may be inside the delimited +construct: + + s/this/< allows you to indent your here-docs to make -the code more readable: +outside of string evals.) + +A problem with the Here-doc syntax given so far is that it must be at the +left margin of your program, messing up the indentation. Starting in +Perl v5.26, the tilde C<~> modifier allows you to indent your here-docs +to make the code more readable. if ($some_var) { print <<~EOF; @@ -2983,97 +3049,90 @@ remain tabs and are not expanded. Additional beginning whitespace (beyond what preceded the delimiter) will be preserved: - print <<~EOF; - This text is not indented - This text is indented with two spaces - This text is indented with two tabs - EOF + print <<~EOF; + This text is not indented + This text is indented with two spaces + This line is indented with two tabs, though those may + have been converted to spaces by various filters by + the time you read this. + EOF -Finally, the modifier may be used with all of the forms -mentioned above: +=back - <<~\EOF; - <<~'EOF' - <<~"EOF" - <<~`EOF` +=head4 Quoting the delimiter -And whitespace may be used between the C<~> and quoted delimiters: +As mentioned above, the terminating string may be quoted. There are +three types of quoting possible. The type used determines the treatment +of the text. - <<~ 'EOF'; # ... "EOF", `EOF` +=over 4 -=back +=item Double Quotes -It is possible to stack multiple here-docs in a row: +Double quotes surrounding the terminating word or string behave as if +no quotes were there, namely the text will be interpolated using exactly +the same rules as normal double quoted strings, as in all the examples +above. So - print <<"foo", <<"bar"; # you can stack them - I said foo. - foo - I said bar. - bar + my $person = 'John'; - myfunc(<< "THIS", 23, <<'THAT'); - Here's a line - or two. - THIS - and here's another. - THAT + print uc << "EOT"; + Hello, $person! + And the text goes on. + EOT -Just don't forget that you have to put a semicolon on the end -to finish the statement, as Perl doesn't know you're not going to -try to do this: +yields: - print <. +which is the same result as without quotes. - chomp($string = <<'END'); - This is a string. - END +=item Single Quotes -If you want your here-docs to be indented with the rest of the code, -use the C<<< <<~FOO >>> construct described under L: +If instead, single quotes are used, the text is treated literally, with +no interpolation of its content. - $quote = <<~'FINIS'; - The Road goes ever on and on, - down from the door where it began. - FINIS + my $person = 'John'; + print uc <<'EOT'; + Hello, $person! + And the text goes on. + EOT -If you use a here-doc within a delimited construct, such as in C, -the quoted material must still come on the line following the -C<<< <>> marker, which means it may be inside the delimited -construct: + HELLO, $PERSON! + AND THE TEXT GOES ON. - s/this/< being treated as two backslashes and not one as they would in +every other quoting construct. -It works this way as of Perl 5.18. Historically, it was inconsistent, and -you would have to write +Just as in the shell, a backslashed bareword following the C<<< << >>> +means the same thing as a single-quoted string does: - s/this/<, C, and the like are not -supported in place of C<''> and C<"">, and the only interpolation is for -backslashing the quoting character: +These two forms are the only ways of quoting in Perl where there is no +need to worry about escaping content, something that code generators can +and do make good use of. - print << "abc\"def"; - testing... - abc"def +=item Backticks -Finally, quoted strings cannot span multiple lines. The general rule is -that the identifier must be a string literal. Stick with that, and you -should be safe. +Finally, if instead backticks are used to quote the terminating string, +the content of the here doc is treated just as it would be if it were a +string embedded in backticks. Thus the content is interpolated as +though it were double quoted and then executed via the shell, with the +results of the execution returned. + + print << `EOC`; # execute command and get results + echo hi there + EOC =back @@ -3892,6 +3951,10 @@ only to prevent breaking any pre-existing links to it from outside. This section has been replaced by L +=head2 Indented Here-docs + +This section has been merged into L + =head1 APPENDIX =head2 List of Extra Paired Delimiters