Skip to content

Commit e8f1141

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 e8f1141

File tree

1 file changed

+165
-121
lines changed

1 file changed

+165
-121
lines changed

pod/perlop.pod

Lines changed: 165 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -2875,85 +2875,147 @@ must use an C<eval()>:
28752875

28762876
eval "tr/$oldlist/$newlist/, 1" or die $@;
28772877

2878-
=item C<< <<I<EOF> >>
2879-
X<here-doc> X<heredoc> X<here-document> X<<< << >>>
2878+
=back
2879+
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> >>
28802888

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

2886-
Prefixing the terminating string with a C<~> specifies that you
2887-
want to use L</Indented Here-docs> (see below).
2894+
An example is
28882895

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.
2896+
my $endng = <<WHIMPER;
2897+
This is the way the text ends.
2898+
This is the way the text ends.
2899+
Not with a bang, but with a
2900+
WHIMPER
28952901

2896-
If the terminating string is quoted, the type of quotes used determine
2897-
the treatment of the text.
2902+
In this case, the terminator is an identifier, the word "WHIMPER". Most
2903+
usually, people capitalize the identifier, just so it stands out, but
2904+
this is just a convention that isn't necessary. There may not be a
2905+
space between the C<< << >> and the identifier,
28982906

2899-
=over 4
2907+
The terminator may be enclosed in quotes, as detailed below, but without
2908+
them, the text of the here-doc acts exactly as if it were double-quoted.
29002909

2901-
=item Double Quotes
2910+
my $person = 'John';
29022911

2903-
Double quotes indicate that the text will be interpolated using exactly
2904-
the same rules as normal double quoted strings.
2912+
print uc << EOT;
2913+
Hello, $person!
2914+
And the text goes on.
2915+
EOT
29052916

2906-
print <<EOF;
2907-
The price is $Price.
2908-
EOF
2917+
This yields:
29092918

2910-
print << "EOF"; # same as above
2911-
The price is $Price.
2912-
EOF
2919+
HELLO, JOHN!
2920+
AND THE TEXT GOES ON.
29132921

2922+
The parentheses in the C<uc> function call don't have to be omitted:
29142923

2915-
=item Single Quotes
2924+
print uc(<<EOT);
2925+
Hello, $person!
2926+
And the text goes on.
2927+
EOT
29162928

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.
2929+
HELLO, JOHN!
2930+
AND THE TEXT GOES ON.
29222931

2923-
Just as in the shell, a backslashed bareword following the C<<< << >>>
2924-
means the same thing as a single-quoted string does:
2932+
And you can intermix a here-document with other things:
29252933

2926-
$cost = <<'VISTA'; # hasta la ...
2927-
That'll be $10 please, ma'am.
2928-
VISTA
2934+
print <<EOT, "Followed by the next argument\n";
2935+
Hello, $person!
2936+
And the text goes on.
2937+
EOT
29292938

2930-
$cost = <<\VISTA; # Same thing!
2931-
That'll be $10 please, ma'am.
2932-
VISTA
2939+
Hello, John!
2940+
And the text goes on.
2941+
Followed by the next argument
29332942

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.
2943+
And you can have multiple here-documents:
29372944

2938-
=item Backticks
2945+
print <<EOT1, <<EOT2;
2946+
Hello, $person!
2947+
And the text goes on.
2948+
EOT1
2949+
Followed by the next argument
2950+
EOT2
29392951

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.
2952+
Hello, John!
2953+
And the text goes on.
2954+
Followed by the next argument
29442955

2945-
print << `EOC`; # execute command and get results
2946-
echo hi there
2947-
EOC
2956+
The terminator doesn't have to be a single word; it may also be some
2957+
quoted text,
29482958

2949-
=back
2959+
my $pagliaci = << "La Commedia e finita!";
2960+
On stage, the actor playing the jealous husband stabs for real the
2961+
actress who is both his real wife and playing the part; then he stabs
2962+
her lover who runs from the audience to defend her. Both die.
2963+
La Commedia e finita!
29502964

2951-
=over 4
2965+
When the terminator is quoted, there may be space between it and the
2966+
C<<< << >>>, as demonstrated in the example just above. Quoting rules
2967+
for it are unrelated to Perl's quoting rules. C<q()>, C<qq()>, and the
2968+
like are not supported in place of C<""> and C<''>, and the only
2969+
interpolation is for backslashing the quoting character:
2970+
2971+
print << "abc\"def";
2972+
testing...
2973+
abc"def
2974+
2975+
The terminating string must appear by itself (unquoted and with no
2976+
surrounding whitespace) on the terminating line. And, it cannot span
2977+
multiple lines. The general rule is that the identifier must be a
2978+
string literal. Stick with that, and you should be safe.
29522979

2953-
=item Indented Here-docs
2980+
Don't forget that you have to put a semicolon on the end to finish the
2981+
statement, as Perl doesn't know you're not going to try to do this:
29542982

2955-
The here-doc modifier C<~> allows you to indent your here-docs to make
2956-
the code more readable:
2983+
print <<ABC
2984+
179231
2985+
ABC
2986+
+ 20;
2987+
2988+
If you want to remove the line terminator from your here-docs,
2989+
use C<chomp()>.
2990+
2991+
chomp($string = <<'END');
2992+
This is a string.
2993+
END
2994+
2995+
If you use a here-doc within a delimited construct, such as in C<s///eg>,
2996+
the quoted material must still come on the line following the
2997+
C<<< <<FOO >>> marker, which means it may be inside the delimited
2998+
construct:
2999+
3000+
s/this/<<E . 'that'
3001+
the other
3002+
E
3003+
. 'more '/eg;
3004+
3005+
It works this way as of Perl 5.18. Historically, it was inconsistent, and
3006+
you would have to write
3007+
3008+
s/this/<<E . 'that'
3009+
. 'more '/eg;
3010+
the other
3011+
E
3012+
3013+
outside of string evals.
3014+
3015+
A problem with the Here-doc syntax given so far is that it must be at the
3016+
left margin of your program, messing up the indentation. Starting in
3017+
Perl v5.26, the tilde C<~> modifier allows you to indent your here-docs
3018+
to make the code more readable.
29573019

29583020
if ($some_var) {
29593021
print <<~EOF;
@@ -2989,91 +3051,69 @@ delimiter) will be preserved:
29893051
This text is indented with two tabs
29903052
EOF
29913053

2992-
Finally, the modifier may be used with all of the forms
2993-
mentioned above:
3054+
If the terminating string is quoted, the type of quotes used determine
3055+
the treatment of the text.
29943056

2995-
<<~\EOF;
2996-
<<~'EOF'
2997-
<<~"EOF"
2998-
<<~`EOF`
3057+
Double quotes surrounding the terminating word or string behave as if
3058+
no quotes were there, namely the text will be interpolated using exactly
3059+
the same rules as normal double quoted strings, as in all the examples
3060+
above. So
29993061

3000-
And whitespace may be used between the C<~> and quoted delimiters:
3062+
my $person = 'John';
30013063

3002-
<<~ 'EOF'; # ... "EOF", `EOF`
3064+
print uc << "EOT";
3065+
Hello, $person!
3066+
And the text goes on.
3067+
EOT
30033068

3004-
=back
3069+
yields:
30053070

3006-
It is possible to stack multiple here-docs in a row:
3071+
HELLO, JOHN!
3072+
AND THE TEXT GOES ON.
30073073

3008-
print <<"foo", <<"bar"; # you can stack them
3009-
I said foo.
3010-
foo
3011-
I said bar.
3012-
bar
3074+
which is the same result as without quotes.
30133075

3014-
myfunc(<< "THIS", 23, <<'THAT');
3015-
Here's a line
3016-
or two.
3017-
THIS
3018-
and here's another.
3019-
THAT
3076+
If instead, single quotes are used, the text is treated literally, with
3077+
no interpolation of its content.
30203078

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:
3079+
my $person = 'John';
3080+
print uc <<'EOT';
3081+
Hello, $person!
3082+
And the text goes on.
3083+
EOT
30243084

3025-
print <<ABC
3026-
179231
3027-
ABC
3028-
+ 20;
3085+
HELLO, $PERSON!
3086+
AND THE TEXT GOES ON.
30293087

3030-
If you want to remove the line terminator from your here-docs,
3031-
use C<chomp()>.
3088+
The difference between a single-quoted here-doc and a single-quoted
3089+
string is that backslashes have no special meaning in a here-doc, with
3090+
C<\\> being treated as two backslashes and not one as they would in
3091+
every other quoting construct.
30323092

3033-
chomp($string = <<'END');
3034-
This is a string.
3035-
END
3036-
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>:
3039-
3040-
$quote = <<~'FINIS';
3041-
The Road goes ever on and on,
3042-
down from the door where it began.
3043-
FINIS
3044-
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:
3049-
3050-
s/this/<<E . 'that'
3051-
the other
3052-
E
3053-
. 'more '/eg;
3054-
3055-
It works this way as of Perl 5.18. Historically, it was inconsistent, and
3056-
you would have to write
3093+
Just as in the shell, a backslashed bareword following the C<<< << >>>
3094+
means the same thing as a single-quoted string does:
30573095

3058-
s/this/<<E . 'that'
3059-
. 'more '/eg;
3060-
the other
3061-
E
3096+
$cost = <<'VISTA'; # hasta la ...
3097+
That'll be $10 please, ma'am.
3098+
VISTA
30623099

3063-
outside of string evals.
3100+
$cost = <<\VISTA; # Same thing!
3101+
That'll be $10 please, ma'am.
3102+
VISTA
30643103

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:
3104+
These two forms are the only ways of quoting in Perl where there is no
3105+
need to worry about escaping content, something that code generators can
3106+
and do make good use of.
30693107

3070-
print << "abc\"def";
3071-
testing...
3072-
abc"def
3108+
Finally, if instead backticks are used to quote the terminating string,
3109+
the content of the here doc is treated just as it would be if it were a
3110+
string embedded in backticks. Thus the content is interpolated as
3111+
though it were double quoted and then executed via the shell, with the
3112+
results of the execution returned.
30733113

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.
3114+
print << `EOC`; # execute command and get results
3115+
echo hi there
3116+
EOC
30773117

30783118
=back
30793119

@@ -3892,6 +3932,10 @@ only to prevent breaking any pre-existing links to it from outside.
38923932

38933933
This section has been replaced by L</Simpler Quote-Like Operators>
38943934

3935+
=head2 Indented Here-docs
3936+
3937+
This section has been merged into by L</Here-docs>
3938+
38953939
=head1 APPENDIX
38963940

38973941
=head2 List of Extra Paired Delimiters

0 commit comments

Comments
 (0)