Skip to content

Commit d1dfe9f

Browse files
committed
perlop: Add here-doc examples
Fixes #20886
1 parent d85c3a2 commit d1dfe9f

File tree

1 file changed

+47
-4
lines changed

1 file changed

+47
-4
lines changed

pod/perlop.pod

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2842,16 +2842,13 @@ must use an C<eval()>:
28422842
eval "tr/$oldlist/$newlist/, 1" or die $@;
28432843

28442844
=item C<< <<I<EOF> >>
2845-
X<here-doc> X<heredoc> X<here-document> X<<< << >>>
2845+
X<here-doc> X<here-docs> X<heredoc> X<here-document> X<<< << >>>
28462846

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

2852-
Prefixing the terminating string with a C<~> specifies that you
2853-
want to use L</Indented Here-docs> (see below).
2854-
28552852
The terminating string may be either an identifier (a word), or some
28562853
quoted text. An unquoted identifier works like double quotes.
28572854
There may not be a space between the C<< << >> and the identifier,
@@ -2862,6 +2859,52 @@ on the terminating line.
28622859
If the terminating string is quoted, the type of quotes used determine
28632860
the treatment of the text.
28642861

2862+
my $person = 'John';
2863+
2864+
print uc << "EOT";
2865+
Hello, $person!
2866+
And the text goes on.
2867+
EOT
2868+
2869+
This yields
2870+
2871+
HELLO, JOHN!
2872+
AND THE TEXT GOES ON.
2873+
2874+
The same result happens if the double quotes are omitted
2875+
2876+
my $person = 'John';
2877+
print uc <<EOT;
2878+
Hello, $person!
2879+
And the text goes on.
2880+
EOT
2881+
2882+
HELLO, JOHN!
2883+
AND THE TEXT GOES ON.
2884+
2885+
If instead, single quotes are used, we get
2886+
2887+
my $person = 'John';
2888+
print uc <<'EOT';
2889+
Hello, $person!
2890+
And the text goes on.
2891+
EOT
2892+
2893+
HELLO, $PERSON!
2894+
AND THE TEXT GOES ON.
2895+
2896+
You can do fancy things like call a function with the here-document
2897+
2898+
print uc(<<EOF);
2899+
abc
2900+
EOF
2901+
2902+
ABC
2903+
2904+
It is usually easier to read the code if the here-document is indented.
2905+
Prefixing the terminating string with a C<~> specifies that you
2906+
want to use L</Indented Here-docs> (see below).
2907+
28652908
=over 4
28662909

28672910
=item Double Quotes

0 commit comments

Comments
 (0)