Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does ENDOFTEXT mean in this Perl code?

Tags:

perl

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);
like image 423
Rebol Tutorial Avatar asked Jul 11 '10 16:07

Rebol Tutorial


People also ask

Do I need to call EOF () in Perl?

As the documentation of eof also points out, you almost never need to call eof () in Perl. In most cases operations that read from file-handles will return undef when they reach the end of the file or when they reach the end of the data available.

What is Perl programming?

Perl is a free-form language which means it can be written, formatted and indented as per user’s requirement. A Perl program consists of a sequence of statements, loops, subroutines, etc. that allows moving around within the code easily. Every statement in a Perl code must end with a semicolon (;).

What are the modes of writing Perl code?

These modes can be Run on the command line with the use of perl keyword or on the Online IDEs in the form of a block of code. Perl also provides an inbuilt IDE of its own along with the installation package. Interactive mode of writing a Perl code means the direct interaction with the interpreter.

When does the readline operator in Perl return UNDEF?

<$fh>, the "readline" operator in Perl returns undef when there is no more to read from the file-handle: The same is true for the diamond operator, which is just a special case of the "readline" operator: In this case even checking for undef is unnecessary as Perl will just end the operation when the input is exhausted.


2 Answers

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.

like image 149
Danny Avatar answered Oct 15 '22 23:10

Danny


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.)

like image 37
Donal Fellows Avatar answered Oct 15 '22 22:10

Donal Fellows