tags:

views:

87

answers:

4

I'd like to know what ENDOFTEXT means in this Perl script:

print <<ENDOFTEXT;
HTTP/1.0 200 OK
Content-Type: text/html

<HTML>
<HEAD><TITLE>Hello World!</TITLE></HEAD>
<BODY>
<H4>Hello World!</H4>
<P>You have reached <a href="$url">$url</a></P>
<P>Your IP Address is $ip</P>
<H5>Have a nice day!</H5>
</BODY>
</HTML>
ENDOFTEXT
exit(0);
+8  A: 

It's a here-document or heredoc. The ENDOFTEXT is just some arbitrary sequence that marks the end of it; it doesn't mean anything in itself. (I would be more inclined to use END but that's just personal taste.)

Donal Fellows
+8  A: 

It is an operator, called a heredoc or here-document. Amusingly enough the reference in perldoc is not as easy to find as it should be. It is useful for being able to quote a large section of text without having to bother with escaping special variables.

You can read the Here document article on wikipedia as well. The entry you are looking for is <<EOF under Quote-and-Quote-like-Operators from perldoc. I'm citing it here for ease of use:

A line-oriented form of quoting is based on the shell "here-document" syntax. Following a << you specify a string to terminate the quoted material, and all lines following the current line down to the terminating string are the value of the item. The terminating string may be either an identifier (a word), or some quoted text. An unquoted identifier works like double quotes. There may not be a space between the << and the identifier, unless the identifier is explicitly quoted. (If you put a space it will be treated as a null identifier, which is valid, and matches the first empty line.)

The terminating string must appear by itself (unquoted and with no surrounding whitespace) on the terminating line.

If the terminating string is quoted, the type of quotes used determine the treatment of the text.

Danny
+3  A: 

The ENDOFTEXT string signifies the beginning and end of a "here-document". It is described in the official Perl documentation (search for EOF): Quote-and-Quote-like-Operators. It is an arbitrary string; the code could have used the string FOO with the same effect. It allows multi-line quoting, and in this case, variables will be interpolated.

toolic
+6  A: 

In addition to what other people said, I should note that the book Perl Best Practices recommends to avoid using bareword here-docs (e.g: "<<EOF") and instead explicitly quote every here-doc as either <<'EOF' or <<"EOF". This is because people often don't know what is the case for the bareword EOF.

Shlomi Fish