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

I've written a small Perl CMS using CGI::Application and HTML::Template that publishes to static files with any extension that the designer prefers - usually php or shtml, but it could be anything their hearts desire. The CMS publishes to individual files consisting of things like article content, navigation and latest story lists that are all "included" together at view time. Depending on their preferred file extension, the designers will modify the source templates to use their language-specific include statements.

Eveything is working fine until I need to generate dynamic output - say the result of a full text search. If I just return the results then none of the language-specific statements are executed, since the output is generated directly from Perl and passed to the web server. Now, I know that there must be a way around this, because another CMS I've used (Article Manager) does this. Unfortunately, they've scrambled their code, so I don't know what their technique is. I think that they generate a temporary file on the fly, but I'm not sure. Any ideas appreciated

Replies are listed 'Best First'.
Re: How do I present other web extensions?
by jhourcle (Prior) on Mar 17, 2005 at 11:23 UTC

    This is more a function of your webserver, than of CGI or Perl. Webservers by default just act as a switchboard, and can optionally hand off to CGI, ColdFusion, PHP, SSIs, or whatever other text-preprocessor you might have. It is exceedingly difficult to have one preprocessor hand off to another one. Generating a seperate page, and then redirecting to it is one way to get the webserver to use a diffferent interpreter for each step of the process, but if that is out, for whatever reason, it's possible that there might be another way --

    Apache uses a series of handlers, and it might be possible, using mod_perl to adjust the stack. (I don't know if it can be done dynamically, as I've never tried, but I have that general impression after having read Practical mod_perl.). It may be easier with Apache 2, as there are specifically SetInputFilter and SetOutputFilter rather than just SetHandler. If you set mod_perl as the input filter, the webserver may still do its general switchboarding to pass off to another program.

    Another alternative would be to call the other language's engine from within your script, but this would probably ruin any optimizations they might have, and would only be possible with those parsers that can be called from the command line (like PHP), or to have the script use LWP or similar to call the webserver to get the templates that have already been parsed. (you may have to change the order of operations, to have whatever the alternative preprocessor is, to call the script through the webserver, so it executes last, but this could be messy, and need extra files)

    Update: Practical mod_perl is available online, under the creative commons license.

Re: How do I present other web extensions?
by TedPride (Priest) on Mar 17, 2005 at 08:29 UTC
    The only way you're going to get the file interpreted is by feeding it to the server, so yes, a temporary file is a must - whether you put the search results in a file and include it in the templated page (search.php?myhash, where file is named according to myhash) or include the templated page as multiple pieces around the search results (include header.php and footer.php).
      Yeah, I see what you mean by the temporary file. I guess a yucky way is to request the temp file using something like LWP from within search.cgi (my search script), capture the result in a variable, remove the temp file and then output the variable... Thanks