Here document: Difference between revisions

From Rosetta Code
Content added Content deleted
(→‎{{header|UNIX Shell}}: Sorry for the bowldlerizing...)
Line 70: Line 70:


<lang sh>#!/bin/sh
<lang sh>#!/bin/sh
cat << DAMMIT
cat << ANARBITRARYTOKEN
The river was deep but I swam it, Janet.
The river was deep but I swam it, Janet.
The future is ours so let's plan it, Janet.
The future is ours so let's plan it, Janet.
Line 76: Line 76:
I've one thing to say and that's ...
I've one thing to say and that's ...
Dammit. Janet, I love you.
Dammit. Janet, I love you.
ANARBITRARYTOKEN
DAMMIT
</lang>
</lang>

Revision as of 11:06, 15 May 2011

Here document is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

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.

J

<lang j> here=:0 :0

 0 :0 will be replaced by the text on the following lines.
 This is three tokens: two instances of the number 0 and
 one instance of the explicit definition token ':'.
 Any indentation in the here document will be retained in the result.
 There must be a space to the left of : or it will combine with the
 0 on its left to form the token 0: which is something completely 
 different.
 The here document is terminated by a line which contains only a
 single right parenthesis ')' and optional white space.  In J's
 documentation the family of entities which include here documents
 (and verb definitions and so on) are called 'scripts'.
 Typically, such values are assigned a name so that they can be
 used later.  However, they may also be discarded and/or ignored, in
 which case they are logically equivalent to multi-line comments.

)

and_here=:noun define

 'noun define' is an alternative and perhaps more "user friendly"
 way of declaring a here document.  It achieves the same thing as
 0 :0 and in fact 'noun' has the value 0 and 'define' has the value :0
 And, of course, there must be a space between the word 'noun' and
 the word 'define'.
 Other useful alternatives include verb (which has the value 3)
 and dyad (which has the value 4), and adverb (which has the value 1).
 However, all of these names are normal variables which can
 be declared to have different values by the developer.  And, of course,
 note that this mechanism is significantly more verbose than using
 the underlying 0 :0 mechanism directly.

)</lang>

Perl

In perl, there must not be a space between the quackquack and the token string. The ending token must always be the entire 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.

Works with: Bourne Shell
Works with: Bourne Again SHell

<lang sh>#!/bin/sh cat << ANARBITRARYTOKEN 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. ANARBITRARYTOKEN </lang>