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);
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.
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 (;).
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.
<$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.
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.
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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With