Forum Moderators: coopster & phranque

Message Too Old, No Replies

Using SSI from within Perl?

Can it be done? Easily?

         

jcmoon

8:43 pm on Aug 2, 2005 (gmt 0)

10+ Year Member



I've got a site which uses shtml includes for the overall design template. I've also got some CGI & Perl scripts, and for those dynamic pages, the SSI files are duplicated within the code

i.e.

print qq~ <html><head>...</head><body>...initial template stuff... ~q;
then some code doing some stuff;
print qq~ ...latter template stuff...</body></html> ~;

What I want is for the CGI / Perl scripts to just call the shtml include files, instead of duplicating them. It's all about efficiency and less work when updating things.

My question is: how do I successfully do this?

My attempts haven't worked, and a Google search shows I probably need to install some updates to Perl / Apache. I'm willing to do this, but would rather ensure there isn't a simpler way, first.

KevinADC

1:18 am on Aug 3, 2005 (gmt 0)

10+ Year Member



well, the server has to "see" the .shtml extension in order to parse out the SSI tags in the document. When you use a perl script thats not going to happen, but you can just open and read the same file that the .shtml page is calling from the SSI tag and display it using perl.

WWMike

5:17 am on Aug 3, 2005 (gmt 0)

10+ Year Member



Yeah, what he said - it can't be done. Here's why:

[httpd.apache.org...]

jcmoon

1:36 pm on Aug 3, 2005 (gmt 0)

10+ Year Member



Like what Brett mentioned here [webmasterworld.com]? I'll give that a try today. Thanks.

Thanks, by the way, for that documentation. It was enlightening.

WWMike

2:23 pm on Aug 3, 2005 (gmt 0)

10+ Year Member



Like what KevinADC said above...

You can't get SSI to work on pages generated from PERL scripts because Apache purposefully ignores it.

If your page is not time/user sensitive one quick solution is to modify the PERL script to write the page to disk and then print "Location: [domain.com...] to switch over to the page in which case the SSI will work.

Another option is to invert the SSI and HTML by manually creating an HTML page with the SSI coded inline and then call the PERL script inline with an SSI virtual:

<!--#include virtual="/cgi-bin/myscript.cgi" -->

Yet another option (mentioned by KevinADC above) is to open/read/print the SSI file inline as you generate your dynamic page, but SSI commands don't translate 100% equivalent to PERL commands so they may need additional processing.

The viability of these options depends on what you're script is actually doing but either way, it's probably going to be a major bummer.

I struggled with trying to figure out why for 6 months before I stumbled on that doc and found out that it just can't be done.

jcmoon

2:45 pm on Aug 3, 2005 (gmt 0)

10+ Year Member



I succeeded with the open/read/print version, and it works as my SSI's are rather simple template files. If I were trying to do more, then you're correct: I'd have to alter some things.

One thing I've noticed: when generating a dynamic page like this, it must be done just right, else I get the "malformed/bad header" error. Simply having it say

print qq~ <html><head><title>Hi</title></head><body> ~;
...
print qq~ </body></html> ~;

just doesn't work at all. What does work, however, is

print qq~ 
<html>
<head>
<title>Hi</title>
</head>
<body>
~;
...
print qq~
</body>
</html>
~;

I don't quite know why, but it seems to matter. Other such tiny seeming-trivialities also seem to affect whether it works or not. Live and learn ...

moltar

2:51 pm on Aug 3, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Have a look at CGI::SSI [search.cpan.org] module.

WWMike

3:09 pm on Aug 3, 2005 (gmt 0)

10+ Year Member



You need to have at least one header line or you'll get a 500 error. I usually put a print "Content-type:" line at the beginning of every script unless I'm going to "print "Location:". The following works:

#!/usr/local/bin/perl

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

print qq~ <html><head><title>Hi</title></head><body> ~;

print qq~ </body></html> ~;

exit;

KevinADC

5:07 pm on Aug 3, 2005 (gmt 0)

10+ Year Member



Have a look at CGI::SSI module.

That is an option, but make sure to read the documentation, it has it's limitations and can only be used on Apache HTTP servers I believe.

WWMike

8:49 pm on Aug 3, 2005 (gmt 0)

10+ Year Member



I'd like to try it but I get a 500 error and my host insists that it is installed. Has anyone out there gotten it to run?

#!/usr/local/bin/perl
use CGI::SSI;
print "Content-type: text/html\n\nHello World";
exit;

KevinADC

6:54 am on Aug 4, 2005 (gmt 0)

10+ Year Member



You will need other modules too that the CGI::SI module uses:

use HTML::SimpleParse;
use File::Spec::Functions; # catfile()
use FindBin;
use LWP::UserAgent;
use HTTP::Response;
use HTTP::Cookies;
use URI;
use Date::Format;

KevinADC

6:55 am on Aug 4, 2005 (gmt 0)

10+ Year Member



use:

use CGI::Carp qw/fatalsToBrowser/;

in your script to see which module is not installed

WWMike

3:04 pm on Aug 4, 2005 (gmt 0)

10+ Year Member



I also suspect missing modules. I tried using Carp but I still get a 500 error and yet the script runs fine when I comment out the use CGI::SSI;

#!/usr/local/bin/perl
use CGI::Carp qw/fatalsToBrowser/;
use CGI::SSI;
print "Content-type: text/html\n\nDONE";
exit;

moltar

4:46 pm on Aug 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's not a standard module and you need to install it.

KevinADC

5:05 pm on Aug 4, 2005 (gmt 0)

10+ Year Member



hmmm.... if CGI::Carp is installed, you should get an error message instead of a 500 ISE page. Are you sure you uploaded that script in ASCII mode and set permissions correctly?

WWMike

5:12 pm on Aug 4, 2005 (gmt 0)

10+ Year Member



My host has assured me (twice) that CGI:SSI is installed.

I looked at the code for CGI:SSI and noticed that it also needs additional modules (as reported in the previous post).

Assuming that CGI::SSI actually IS installed, I suspect that at least one of the modules it tries to use is not installed but I'd like to know exactly which one(s) before I just send the entire list to my host.

I keep getting a 500 error and I can't determine where the fault is because Carp isn't catching it.

If I comment out the use CGI::SSI line the script runs fine.

You can see (a few posts back) that the script is basically just a "Hello World" with some additional uses included.

Why doesn't Carp catch the 500 error to help me find out which module isn't installed (if that's even it)?

KevinADC

8:02 pm on Aug 4, 2005 (gmt 0)

10+ Year Member



I don't know why, run PerlDiver on your server if you want to and see if CGI::SSI is really installed and is in @INC somewhere:

[scriptsolutions.com...]

WWMike

10:49 pm on Aug 4, 2005 (gmt 0)

10+ Year Member



Thanks, I tried it but I'm getting a 500 error on that and it's far too complex for me to debug plus this is getting off topic from the original post so I'll consider starting a new thread to address the basic issue at hand.

KevinADC

12:44 am on Aug 5, 2005 (gmt 0)

10+ Year Member



Now I'm starting to think its your server, perldiver is a sinch to install, you just change the shebang line if need be, upload and set the permission to execute. There is nothing else to edit in the script.

WWMike

3:57 am on Aug 5, 2005 (gmt 0)

10+ Year Member



It runs, but it blows up soon after it gets started.

I appreciate the suggestion though.

Thanks.

KevinADC

4:16 am on Aug 5, 2005 (gmt 0)

10+ Year Member



let me know who your web host is so I can avoid them! **wink**