Here document: Difference between revisions
(updated wording) |
|||
Line 5: | Line 5: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
In perl, there must not be a space between the quackquack and the token string. The ending token must always be at the beginning of the end line for it to be recognised. |
|||
<lang perl> |
<lang perl> |
||
$address = <<END; |
$address = <<END; |
||
Line 12: | Line 14: | ||
WM4 5HD. |
WM4 5HD. |
||
END |
END |
||
</lang> |
|||
If the token string contains spaces, the token after the quackquack must be quoted: |
|||
<lang perl> |
|||
$pancake = <<"NO MORE INGREDIENTS"; |
|||
egg |
|||
milk |
|||
flour |
|||
NO MORE INGREDIENTS |
|||
</lang> |
</lang> |
||
Revision as of 23:13, 14 May 2011
A here document is a way of specifying a text block, preserving the line breaks, indentation and other whitespace within the text. Depending on the language being used a here document is constructed using a command followed by a quackquack (or some other symbol) followed by a token string. The text block will then start on the next line, and will be followed by the chosen token at the beginning of the following line, which is used to mark the end of the textblock.
The task is to demonstrate the use of here documents within the language.
Perl
In perl, there must not be a space between the quackquack and the token string. The ending token must always be at the beginning of the end line for it to be recognised.
<lang perl> $address = <<END; 1, High Street, SMALLTOWN, West Midlands. WM4 5HD. END </lang>
If the token string contains spaces, the token after the quackquack must be quoted: <lang perl> $pancake = <<"NO MORE INGREDIENTS"; egg milk flour NO MORE INGREDIENTS </lang>
UNIX Shell
In the shell, here document act as input to the command, rather than providing a string definition.
<lang sh>#!/bin/sh cat << DAMMIT The river was deep but I swam it, Janet. The future is ours so let's plan it, Janet. So please don't tell me to can it, Janet. I've one thing to say and that's ... Dammit. Janet, I love you. DAMMIT </lang>