Skip to content

Commit 10ea6d8

Browse files
committed
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.
1 parent f9d1ecd commit 10ea6d8

File tree

1 file changed

+172
-115
lines changed

1 file changed

+172
-115
lines changed

pod/perlop.pod

Lines changed: 172 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -2648,6 +2648,8 @@ X<tr> X<y> X<transliterate> X</c> X</d> X</s>
26482648

26492649
=item C<y/I<SEARCHLIST>/I<REPLACEMENTLIST>/cdsr>
26502650

2651+
=back
2652+
26512653
These transliterate all occurrences of the characters found (or not found
26522654
if the C</c> modifier is specified) in the search list with the
26532655
positionally corresponding character in the replacement list, possibly
@@ -2875,85 +2877,153 @@ must use an C<eval()>:
28752877

28762878
eval "tr/$oldlist/$newlist/, 1" or die $@;
28772879

2878-
=item C<< <<I<EOF> >>
2879-
X<here-doc> X<heredoc> X<here-document> X<<< << >>>
2880+
=head3 Here-docs
2881+
X<here-doc> X<here-docs> X<heredoc> X<here-document> X<<< << >>>
2882+
2883+
=over 4
2884+
2885+
=item C<< <<I<EOT> >>
2886+
2887+
=item C<< <<~I<EOT> >>
2888+
2889+
=back
28802890

28812891
A line-oriented form of quoting is based on the shell "here-document"
28822892
syntax. Following a C<< << >> you specify a string to terminate
28832893
the quoted material, and all lines following the current line down to
28842894
the terminating string are the value of the item.
28852895

2886-
Prefixing the terminating string with a C<~> specifies that you
2887-
want to use L</Indented Here-docs> (see below).
2896+
=over 4
28882897

2889-
The terminating string may be either an identifier (a word), or some
2890-
quoted text. An unquoted identifier works like double quotes.
2891-
There may not be a space between the C<< << >> and the identifier,
2892-
unless the identifier is explicitly quoted. The terminating string
2893-
must appear by itself (unquoted and with no surrounding whitespace)
2894-
on the terminating line.
2898+
=item Plain Here-docs
28952899

2896-
If the terminating string is quoted, the type of quotes used determine
2897-
the treatment of the text.
2900+
An example is
28982901

2899-
=over 4
2902+
my $endng = <<WHIMPER;
2903+
This is the way the text ends.
2904+
This is the way the text ends.
2905+
Not with a bang, but with a
2906+
WHIMPER
29002907

2901-
=item Double Quotes
2908+
In this case, the terminator is an identifier, the word "WHIMPER". Most
2909+
usually, people capitalize the identifier, just so it stands out, but
2910+
this is just a convention that isn't necessary. There may not be a
2911+
space between the C<< << >> and the identifier,
29022912

2903-
Double quotes indicate that the text will be interpolated using exactly
2904-
the same rules as normal double quoted strings.
2913+
The terminator may be enclosed in quotes, as detailed below, but without
2914+
them, the text of the here-doc acts exactly as if it were double-quoted.
29052915

2906-
print <<EOF;
2907-
The price is $Price.
2908-
EOF
2916+
my $person = 'John';
29092917

2910-
print << "EOF"; # same as above
2911-
The price is $Price.
2912-
EOF
2918+
print uc << EOT;
2919+
Hello, $person!
2920+
And the text goes on.
2921+
EOT
29132922

2923+
This yields:
29142924

2915-
=item Single Quotes
2925+
HELLO, JOHN!
2926+
AND THE TEXT GOES ON.
29162927

2917-
Single quotes indicate the text is to be treated literally with no
2918-
interpolation of its content. This is similar to single quoted
2919-
strings except that backslashes have no special meaning, with C<\\>
2920-
being treated as two backslashes and not one as they would in every
2921-
other quoting construct.
2928+
The parentheses in the C<uc> function call don't have to be omitted:
29222929

2923-
Just as in the shell, a backslashed bareword following the C<<< << >>>
2924-
means the same thing as a single-quoted string does:
2930+
print uc(<<EOT);
2931+
Hello, $person!
2932+
And the text goes on.
2933+
EOT
29252934

2926-
$cost = <<'VISTA'; # hasta la ...
2927-
That'll be $10 please, ma'am.
2928-
VISTA
2935+
HELLO, JOHN!
2936+
AND THE TEXT GOES ON.
29292937

2930-
$cost = <<\VISTA; # Same thing!
2931-
That'll be $10 please, ma'am.
2932-
VISTA
2938+
And you can intermix a here-document with other things:
29332939

2934-
This is the only form of quoting in perl where there is no need
2935-
to worry about escaping content, something that code generators
2936-
can and do make good use of.
2940+
print <<EOT, "Followed by the next argument\n";
2941+
Hello, $person!
2942+
And the text goes on.
2943+
EOT
29372944

2938-
=item Backticks
2945+
Hello, John!
2946+
And the text goes on.
2947+
Followed by the next argument
29392948

2940-
The content of the here doc is treated just as it would be if the
2941-
string were embedded in backticks. Thus the content is interpolated
2942-
as though it were double quoted and then executed via the shell, with
2943-
the results of the execution returned.
2949+
And you can have multiple here-documents:
29442950

2945-
print << `EOC`; # execute command and get results
2946-
echo hi there
2947-
EOC
2951+
print <<EOT1, <<EOT2;
2952+
Hello, $person!
2953+
And the text goes on.
2954+
EOT1
2955+
Followed by the next argument
2956+
EOT2
29482957

2949-
=back
2958+
Hello, John!
2959+
And the text goes on.
2960+
Followed by the next argument
29502961

2951-
=over 4
2962+
The terminator doesn't have to be a single word; it may also be some
2963+
quoted text,
2964+
2965+
my $pagliaci = << "La Commedia e finita!";
2966+
On stage, the actor playing the jealous husband stabs for real the
2967+
actress who is both his real wife and playing the part; then he stabs
2968+
her lover who runs from the audience to defend her. Both die.
2969+
La Commedia e finita!
2970+
2971+
When the terminator is quoted, there may be space between it and the
2972+
C<<< << >>>, as demonstrated in the example just above. Quoting rules
2973+
for it are unrelated to Perl's quoting rules. C<q()>, C<qq()>, and the
2974+
like are not supported in place of C<""> and C<''>, and the only
2975+
interpolation is for backslashing the quoting character:
2976+
2977+
print << "abc\"def";
2978+
testing...
2979+
abc"def
2980+
2981+
The terminating string must appear by itself (unquoted and with no
2982+
surrounding whitespace) on the terminating line. And, it cannot span
2983+
multiple lines. The general rule is that the identifier must be a
2984+
string literal. Stick with that, and you should be safe.
2985+
2986+
Don't forget that you have to put a semicolon on the end to finish the
2987+
statement, as Perl doesn't know you're not going to try to do this:
2988+
2989+
print <<ABC
2990+
179231
2991+
ABC
2992+
+ 20;
2993+
2994+
If you want to remove the line terminator from your here-docs,
2995+
use C<chomp()>.
2996+
2997+
chomp($string = <<'END');
2998+
This is a string.
2999+
END
3000+
3001+
If you use a here-doc within a delimited construct, such as in C<s///eg>,
3002+
the quoted material must still come on the line following the
3003+
C<<< <<FOO >>> marker, which means it may be inside the delimited
3004+
construct:
3005+
3006+
s/this/<<E . 'that'
3007+
the other
3008+
E
3009+
. 'more '/eg;
3010+
3011+
It works this way as of Perl 5.18. Historically, it was inconsistent, and
3012+
you would have to write
3013+
3014+
s/this/<<E . 'that'
3015+
. 'more '/eg;
3016+
the other
3017+
E
3018+
3019+
outside of string evals.
29523020

29533021
=item Indented Here-docs
29543022

2955-
The here-doc modifier C<~> allows you to indent your here-docs to make
2956-
the code more readable:
3023+
A problem with the original Here-doc syntax is that it must be at the
3024+
left margin of your program, messing up the indentation. Starting in
3025+
Perl v5.26, the tilde C<~> modifier allows you to indent your here-docs
3026+
to make the code more readable.
29573027

29583028
if ($some_var) {
29593029
print <<~EOF;
@@ -2989,91 +3059,78 @@ delimiter) will be preserved:
29893059
This text is indented with two tabs
29903060
EOF
29913061

2992-
Finally, the modifier may be used with all of the forms
2993-
mentioned above:
3062+
=back
29943063

2995-
<<~\EOF;
2996-
<<~'EOF'
2997-
<<~"EOF"
2998-
<<~`EOF`
3064+
If the terminating string is quoted, the type of quotes used determine
3065+
the treatment of the text.
29993066

3000-
And whitespace may be used between the C<~> and quoted delimiters:
3067+
=over 4
30013068

3002-
<<~ 'EOF'; # ... "EOF", `EOF`
3069+
=item Double Quotes
30033070

3004-
=back
3071+
Double quotes surrounding the terminating word or string behave as if
3072+
no quotes were there, namely the text will be interpolated using exactly
3073+
the same rules as normal double quoted strings, as in all the examples
3074+
above. So
30053075

3006-
It is possible to stack multiple here-docs in a row:
3076+
my $person = 'John';
30073077

3008-
print <<"foo", <<"bar"; # you can stack them
3009-
I said foo.
3010-
foo
3011-
I said bar.
3012-
bar
3078+
print uc << "EOT";
3079+
Hello, $person!
3080+
And the text goes on.
3081+
EOT
30133082

3014-
myfunc(<< "THIS", 23, <<'THAT');
3015-
Here's a line
3016-
or two.
3017-
THIS
3018-
and here's another.
3019-
THAT
3083+
yields:
30203084

3021-
Just don't forget that you have to put a semicolon on the end
3022-
to finish the statement, as Perl doesn't know you're not going to
3023-
try to do this:
3085+
HELLO, JOHN!
3086+
AND THE TEXT GOES ON.
30243087

3025-
print <<ABC
3026-
179231
3027-
ABC
3028-
+ 20;
3088+
which is the same result as without quotes.
30293089

3030-
If you want to remove the line terminator from your here-docs,
3031-
use C<chomp()>.
3090+
=item Single Quotes
30323091

3033-
chomp($string = <<'END');
3034-
This is a string.
3035-
END
3092+
If instead, single quotes are used, the text is treated literally, with
3093+
no interpolation of its content.
30363094

3037-
If you want your here-docs to be indented with the rest of the code,
3038-
use the C<<< <<~FOO >>> construct described under L</Indented Here-docs>:
3095+
my $person = 'John';
3096+
print uc <<'EOT';
3097+
Hello, $person!
3098+
And the text goes on.
3099+
EOT
30393100

3040-
$quote = <<~'FINIS';
3041-
The Road goes ever on and on,
3042-
down from the door where it began.
3043-
FINIS
3101+
HELLO, $PERSON!
3102+
AND THE TEXT GOES ON.
30443103

3045-
If you use a here-doc within a delimited construct, such as in C<s///eg>,
3046-
the quoted material must still come on the line following the
3047-
C<<< <<FOO >>> marker, which means it may be inside the delimited
3048-
construct:
3104+
The difference between a single-quoted here-doc and a single-quoted
3105+
string is that backslashes have no special meaning, with C<\\> being
3106+
treated as two backslashes and not one as they would in every other
3107+
quoting construct.
30493108

3050-
s/this/<<E . 'that'
3051-
the other
3052-
E
3053-
. 'more '/eg;
3109+
Just as in the shell, a backslashed bareword following the C<<< << >>>
3110+
means the same thing as a single-quoted string does:
30543111

3055-
It works this way as of Perl 5.18. Historically, it was inconsistent, and
3056-
you would have to write
3112+
$cost = <<'VISTA'; # hasta la ...
3113+
That'll be $10 please, ma'am.
3114+
VISTA
30573115

3058-
s/this/<<E . 'that'
3059-
. 'more '/eg;
3060-
the other
3061-
E
3116+
$cost = <<\VISTA; # Same thing!
3117+
That'll be $10 please, ma'am.
3118+
VISTA
30623119

3063-
outside of string evals.
3120+
These two forms are the only ways of quoting in perl where there is no
3121+
need to worry about escaping content, something that code generators can
3122+
and do make good use of.
30643123

3065-
Additionally, quoting rules for the end-of-string identifier are
3066-
unrelated to Perl's quoting rules. C<q()>, C<qq()>, and the like are not
3067-
supported in place of C<''> and C<"">, and the only interpolation is for
3068-
backslashing the quoting character:
3124+
=item Backticks
30693125

3070-
print << "abc\"def";
3071-
testing...
3072-
abc"def
3126+
The content of the here doc is treated just as it would be if the
3127+
string were embedded in backticks. Thus the content is interpolated
3128+
as though it were double quoted and then executed via the shell, with
3129+
the results of the execution returned.
30733130

3074-
Finally, quoted strings cannot span multiple lines. The general rule is
3075-
that the identifier must be a string literal. Stick with that, and you
3076-
should be safe.
3131+
print << `EOC`; # execute command and get results
3132+
echo hi there
3133+
EOC
30773134

30783135
=back
30793136

0 commit comments

Comments
 (0)