chriso has asked for the wisdom of the Perl Monks concerning the following question:

I have written a perl script which opens an shtml document and reads it into an array (@htmldoc). It then substitutes a number of placeholders with variables which have been passed to the script from the link and then prints out @htmldoc.

One of the variables passed to the script is $course_desc which in this example represents the value 243desc.txt. The placeholder %%course_desc%% is being substituted with the variable $course_desc.

The shtml document before being read into the perl script has the following line:

 <!--#include virtual="/crs_desc/%%course_desc%%"/ -->

The problem I ran into is that when perl prints out @htmldoc, it does not parse the ssi command. When you view the html code printed by the perl script it shows the following line:

 <!--#include virtual="/crs_desc/243desc.text"/ -->

It interpretes the statement as if it was an html comment, so it doesn't print anything.

I have a variety of external documents which I need to import into the html template, depending on the value of the $course_desc variable. Is there a way to get SSI to work with perl, OR, is there another way of creating the dynamic content using external documents besides using SSI?

I have tried SSI enabling both the cgi-bin directory and the shtml document directory and that doesn't work. I know its not an SSI configuration problem.

Thanks in advance. Chris

Replies are listed 'Best First'.
Re: Perl and SSI
by nite_man (Deacon) on May 02, 2003 at 12:06 UTC
    Try to use CGI::SSI or Apache::SSL if your scripts run under mod_perl.
          
    --------------------------------
    SV* sv_bless(SV* sv, HV* stash);
    
Re: Perl and SSI
by The Mad Hatter (Priest) on May 02, 2003 at 12:03 UTC
    Since this is CGI, the webserver passes the request off to your script and it is then up to you to return data. This means that the server won't parse documents with SSI in them. Search CPAN for SSI; I know there are some modules that can parse the SSI for you from within Perl.
Re: Perl and SSI
by glivings (Scribe) on May 02, 2003 at 12:16 UTC
    The problem is that, AFIAK, Apache won't parse script output. At the point that it executes your script, it washes its hands of the matter and assumes that your script will handle its own output.

    One question I have for you is: if your script is processing these files, why do you need SSI? Why not include the file yourself? You already know the filename, so just slurp it into a list and print it out at the right place.

    There are also modules on CPAN you could have a look at - specifically CGI::SSI.

      Please explain what you mean by putting it in a list and printing it out at the right place. I'm not familiar with this?

      The document that the SSI command will process will change, depending on which link is selected. The shtml document is being used as a template and the course description file ($course_desc variable) is the text that describes the course, which will change depending on which course a student selects. So, this information changes, but the basic format of the html document (from the template) does not change. If there is away of importing this information besides using SSI I'm game.

      Thanks,

        If you're using Text::Template (and I assume other templating modules will allow you to do the same/similar thing), just replace your the SSI in your template: <!--#include virtual="/crs_desc/%%course_desc%%"/ --> with the code to include the file:

        <% my @contents = (); open (FH, "<", "$course_desc") || die "can't open file: $!"; @contents = <FH>; close FH; $OUT .= @contents; %>
        Then you can fill in the template from your script, and your includes will be there:
        my $q = CGI->new; my $course_desc = "/courses/csci_xxx.txt" if ($q->param('course') eq " +csci xxx"); ... my $tmpl = Text::Template->new(TYPE => "FILE", SOURCE => $template_src +, DELIMITERS => ['<%', '%>'], UNTAINT => 1) || die ("Error loading template $filename: $Text::Template::ERROR +"); my $output = $tmpl->fill_in(HASH => { course_desc => $course_desc}); print "Content-type:text/html\n\n", $output;

        Of course, this can get very complicated, especially since virtual includes are used to include the output of commands, etc. This type of very dirty solution is only suitable for very simple cases - and some might argue (successfully) that it should not be used even then.

        To be honest, I've only used this approach for static files, such as including a header or footer in a template. This approach may or may not be suitable for what you want to accomplish, but if you're doing anything complex, I would suggest using CGI::SSI.

Re: Perl and SSI
by Aristotle (Chancellor) on May 02, 2003 at 14:28 UTC