1

iam using perl to print some data but it is giving me error as Can't find string terminator "EOF" anywhere before EOF at

The code is:

#!/usr/local/bin/perl -w
print <<EOF;
hello
EOF

ERROR:

[Mon May 23 11:32:12 2016] [error] [client 192.168.10.117] Directory index forbidden by Options directive: /var/www/, referer: http://192.168.10.100/ [Mon May 23 11:32:12 2016] [error] [client 192.168.10.117] malformed header from script. Bad header=hello: testeof.cgi [Mon May 23 11:32:18 2016] [error] [client 192.168.10.117] Directory index forbidden by Options directive: /var/www/, referer: http://192.168.10.100/ [Mon May 23 11:32:18 2016] [error] [client 192.168.10.117] Can't find string terminator "EOF" anywhere before EOF at /var/www/sandeep/testeof.cgi line 2. [Mon May 23 11:32:18 2016] [error] [client 192.168.10.117] Premature end of script headers: testeof.cgi

I tried putting EOF in single quotes (print <<'EOF';) as shown in this answer, but the error is same. Printing by this method is working in otherr files in the same directory.
I also referred this question (Why am I getting “Can't find string terminator ”'“ anywhere before EOF at -e line 1” when I try to run a Perl one-liner on Windows?) but in that question OP is using different method to print and iam using linux(UBUNTU).

Please guide where I am doing wrong?

Community
  • 1
  • 1
Sandeep
  • 1,504
  • 7
  • 22
  • 32

1 Answers1

3

Your code is correct.

Ensure that your EOF is on a single line with no spaces around it.

Use quotes to designate your terminator:

print <<_EOF_;
hi $$
_EOF_

print <<"_EOF_";
hi $$
_EOF_

Both are exactly the same (print hi 12345 where 12345 is the process id of the current process), but the second one is more clear compared to single quotes:

print <<'_EOF_';
hi $$
_EOF_

This one will print hi $$, because no variable replacing is done with singles quotes.

Web-script always require a header (at least for Apache). Add this line as the first output line to get rid of the malformed header from script error:

print "Content-type: text/html\r\n\r\n";
Sebastian
  • 2,472
  • 1
  • 18
  • 31