1

i am trying to run javascript in a perl CGI file. The code is as follows

#!C:\wamp\bin\perl\bin\perl.exe

$html = "Content-Type: text/html

<HTML>
<HEAD>
<TITLE>Hello World</TITLE>
<SCRIPT TYPE="TEXT/JAVASCRIPT">
alert("i am here");
</SCRIPT>
</HEAD>
<BODY>
<H4>Hello World</H4>
<P>
Your IP Address is $ENV{REMOTE_ADDR}
<P>
<H5>Have a nice day</H5>
</BODY>
</HTML>";

print $html;

I am getting an internal server error.

The code with just the html works fine Please let me know what has to be done to include javascript in Perl

3 Answers 3

6

You can't use double quotes inside your double-quoted string without escaping them. The internal server error is caused by Perl trying to tell you that

$html = "..."TEXT/JAVASCRIPT"..."i am here"...";

is not valid Perl code. If you check your server's error log, you'll see something like "Bareword found where operator expected at...".

The simpler solution is to use a here document:

#!C:\wamp\bin\perl\bin\perl.exe

use strict;
use warnings;

my $html = <<"END HTML";
Content-Type: text/html

<HTML>
<HEAD>
<TITLE>Hello World</TITLE>
<SCRIPT TYPE="TEXT/JAVASCRIPT">
alert("i am here");
</SCRIPT>
</HEAD>
<BODY>
<H4>Hello World</H4>
<P>
Your IP Address is $ENV{REMOTE_ADDR}
<P>
<H5>Have a nice day</H5>
</BODY>
</HTML>
END HTML

print $html;
Sign up to request clarification or add additional context in comments.

Comments

1

Use qq() when outputing HTML or JavaScript.

#!C:\wamp\bin\perl\bin\perl.exe
use warnings;
use strict;

my $html = qq(Content-Type: text/html

<HTML>
<HEAD>
<TITLE>Hello World</TITLE>
<SCRIPT TYPE="TEXT/JAVASCRIPT">
alert("i am here");
</SCRIPT>
</HEAD>
<BODY>
<H4>Hello World</H4>
<P>
Your IP Address is $ENV{REMOTE_ADDR}
<P>
<H5>Have a nice day</H5>
</BODY>
</HTML>);

print $html;

You can use {} instead of () as delimiters. see the document.

a {} represents any pair of delimiters you choose.

Comments

0

As already pointed out the problem is the erroneous quoting.

As a small tip (besides fixing the quoting as mentioned above)

Add

use warnings;
use strict;

to the top of your script, so you can always check the syntax by executing perl -c

in your case:

C:\wamp\bin\perl\bin\perl.exe -c <filename>

this would have shown you the error immidiately.

HTH Georg

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.