Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Embed/Run PERL in HTML

Status
Not open for further replies.

Chivo

Programmer
Oct 25, 2006
15
GB
Hi all,

I've created a small site and would like to add some new functionality. It's a football site and what I plan to do is add a match results page. The results I have in excel format but can easily convert to plain text, csv, access, etc.

I know how to write the PERL to connect to a DB and extract the results I need. The problem I have though is utilising this in HTML. I'm new to PERL and web design so alot of things I know now how to do but cannot tie up together.

I've created a page named stats.shtml and a PERL script named results.pl. So far I've tried two methods to utilise the script in HTML and neither have worked-

<!-- exec cgi = "results.pl" -->

and

<!--#include virtual="results.pl" -->

I'm hoping there is a glaring error there that someone will point out and point me in the right direction. The PERL file is at the moment just a test file with a simple print statement in it. If I access the file via the full URL it works fine but if I access the HTML file by the full URL I get the following message:

[an error occurred while processing this directive]

Any help would be appreciated.

Cheers

Chivo

 
If you want the HTML page to be "pure" HTML (with a .html extension), you may need to embed Perl JavaScripts...

Code:
<script type="text/javascript" src="perlscript.cgi"></script>

There's a thing called PerlScript which acts like JavaScript in that you can embed Perl code, but not many servers support this:

Code:
<script type="text/perlscript">
$window->document->write ("Hello world!");
</script>

The best way to do it though would be to use something like HTML::Template. You'll ultimately need to have a CGI to print out the HTML pages, but it would be able to "include" other pages in the way you have described.

-------------
Kirsle.net | Kirsle's Programs and Projects
 
If you're getting the "[an error occurred while processing this directive]" error, it means that SSI is trying to do something anyway. Firstly, are you sure you can run results.pl directly (i.e. by typing its URL into the address bar of your browser), rather than calling it using SSI. If you can't, then your problem is with the script and not your SSI directive.
 
Thanks for the replies.

@Kirsle- thanks for the explanation as I wasn't sure what way to implement PERL.

@ishnid- I think I have been a bit silly and the shtml page may have been cached as it now works.

I've started from scratch again with a simple setup. The PL file consists of just 1 print line. Now something really strange has happened. The shtml file now works with:
<!--#include virtual="results.pl"-->
but the html file doesn't work with:
<!--#include virtual="results.pl"-->
<script type="text/javascript" src="results.pl"></script>
The latter method suggested by Kirsle is the way I would like to go but for some reason it is not working.

Any help would be appreciated, here are the links:


As you will see the html document isn't working as it should.

Cheers
 
For the javascript version, you're using a <script> tag and specifying that the browser should expect javascript output from results.pl. However, results.pl only prints "Football Results", which certainly isn't Javascript. I don't know the details of how people usually do it this way, but you definitely do need to output javascript for it to work properly.
 
Ishnid,

Cheers. I see what you mean. I'll go off and try that out now and hopefully have some success.

Chivo
 
Your results.pl is now printing out an entire HTML document, but you need JavaScript.

Here's an example of a CGI script that works for me when called via a <javascript> tag.

Code:
#!/usr/bin/perl -w
use strict;
use CGI;

print CGI::header( 'text/javascript' );
print "document.write( 'Hello World' )\n";
The output of your Perl script has to be valid JavaScript code. Personally, I think this hugely overcomplicates the entire process and makes maintaining your scripts quite a bit more difficult. If I had SSI available, I'd just stick with that.
 
Hi ishnid,

Thanks for that. Now, eventually, it's sinking in to my brain and I understand. That works a treat.

Again thanks for taking the time and having the patience to explain it further. I'm now on my way. :D

Chivo
 
The PL file consists of just 1 print line.

that was the problem. Even though the .shtml page may have printed some type of http headers, the perl script still needs to print an appropriate header for the type of content you will be printing from the perl script, at minimum for text/html:

Code:
print "Content-type: text/html\n\n";
print "Hello World";

Ishnids code is doing the same thing using the CGI module but I would not load up that module just to print a content-type header, you could just as easily do this:

Code:
print "Content-type: text/javascript\n\n";
print "document.write( 'Hello World' )\n";

but that is really very awkward and should only be used if there is a good reason to do it like that.

- Kevin, perl coder unexceptional!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top