Reputation: 23
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
Upvotes: 1
Views: 2258
Reputation: 62109
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;
Upvotes: 6
Reputation: 2341
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
Upvotes: 0
Reputation: 4709
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.
Upvotes: 1