Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Can I do Server-Side-Includes in Perl?

by rodry (Beadle)
on Apr 06, 2000 at 10:59 UTC ( #7011=perlquestion: print w/replies, xml ) Need Help??

rodry has asked for the wisdom of the Perl Monks concerning the following question: (cgi programming)

I have a script invoked from a form's "submit" button. I will print out the line
<!--#include file="file.ext"-->
but the #include does not work. It simply prints it out as a comment.

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: Can I do Server-Side-Includes in Perl?
by chromatic (Archbishop) on Apr 06, 2000 at 18:40 UTC
    Instead, why not just open and print the file to the web browser? I like something like this:
    { local *INPUT; open (INPUT, "file.ext") or warn "Cannot open file: $!" && return; print while (<INPUT>); close INPUT; }
    Saves on memory that way. If you're not in a subroutine, you'll get an error message with the return statement. You can avoid it this way:
    { local *INPUT; if (open(INPUT, "file.ext")) { print while (<INPUT>); close INPUT; } # for diagnostic purposes, uncomment these lines # else { # print "Couldn't open file: $!"; # } }
Re: Can I do Server-Side-Includes in Perl?
by brother ab (Scribe) on Sep 15, 2000 at 11:30 UTC

    If you are using mod_perl you can try Apache::Filter+Apache::SSI or Apache::SSIChain (based on Apache::OutputChain and Apache::SSI). The second variant permits you to filter almost all - plain HTML, output from other mod_perl scripts or from usual CGI scripts written in any language, and so on.

    Both variants are fully mod_include complaint and have two additional powerful features (well, Apache::SSI have them):

    • calling perl subs and including results
    • really good if..elif..else construct)
Re: Can I do Server-Side-Includes in Perl?
by clunis (Initiate) on Apr 21, 2000 at 09:37 UTC
    I wrote a module I call 'Text::SSITemplate' to get around the fact that mod_include (or whatever you use to parse your SSIs) never sees the output of a CGI program. This is mostly an implementation of Apache's mod_include without 'exec':

    SSITemplates.tar.gz

    The tarball includes pod documentation, an example cgi, example templates, and the SSITemplate library. I'd be really interested to hear what folks think of this (and whether I should put it on the CPAN).

    This does not require mod_perl, but if you run mod_perl there is an Apache::SSI on the CPAN that you may want to look at.

Re: Can I do Server-Side-Includes in Perl?
by saintbrie (Scribe) on Mar 28, 2003 at 19:08 UTC
    Most of the better templating systems allow you to do this fairly easily. With Template::Toolkit, it is as simple as putting
    [% INCLUDE filename %]
    in the template
      If you aren't using a templating system, there are a couple of modules that will allow you to do SSI from inside CGI.
Re: Can I do Server-Side-Includes in Perl?
by btrott (Parson) on Apr 06, 2000 at 11:22 UTC
    SSIs don't work from within Perl scripts (least I've never known them too). They only work within static documents, because the web server, when it gets a request for a Perl script, hands off control to perl, which is then expected to basically just write its output back to the client.

    The server doesn't actually parse the HTML written by the Perl script (as compared to when you use SSIs in a server-parsed HTML file, when the server parses the file to find any SSIs), so your SSIs won't get expanded if you try to include them in the HTML outputted by a Perl script.

Re: Can I do Server-Side-Includes in Perl?
by cianoz (Friar) on Aug 26, 2000 at 20:28 UTC
    mod_include.c cannot catch the output of a CGI so you have to process SSI directives from inside the script. try with CGI::SSI (available on CPAN)
Re: Can I do Server-Side-Includes in Perl?
by Anonymous Monk on Jan 24, 2001 at 02:24 UTC
    Yes you can actually. You can install mod_filter and have the perl scripts run through mod_include. Take a look at http://tangent.org/mod_filter/
Re: Can I do Server-Side-Includes in Perl?
by QwertyD (Pilgrim) on Jun 10, 2001 at 22:15 UTC
    Instead of outputting to the browser, couldn't the CGI write a .shtml file, and send an HTTP "Location:" header to the browser that redirects to the outputted file? The downside is that you'ld have to have it write unique files each time it's accessed to prevent conflicts if one person hits the script before another has finished downloading the output (Maybe a filename based on the time?). You'ld also have to have some way of flushing out the .shtml files every so often. (Maybe this isn't such a great idea.)

    Standard disclaimer: I'm new to Perl. I'm still learning Perl. If what I say about Perl contradicts what someone else said, I'm probably wrong.
Re: Can I do Server-Side-Includes in Perl?
by transmission_err (Initiate) on Nov 25, 2001 at 22:34 UTC
    why do all that just open the html document you would have normally put in the include tag and print it out... ie.
    open(B, "any.html"); while(<B>){ $line = $_; print $line; }
    its simple, and very easy to understand.
      open(FILE, "filename") or die "$!\n"; print while <FILE>;
      also works, and it's a bit more concise. You will want to change the die part for your specific application, but it's better than no error handling.

      Quinn Slack
      perl -e 's ssfhjok qupyrqs&&tr sfjkohpyuqrspitnrapj"hs&&eval&&s&&&'
Re: Can I do Server-Side-Includes in Perl?
by Anonymous Monk on Sep 25, 2001 at 05:11 UTC
    I would assume you could - its just that you don't probably want to use an include as you would in html - I am sure that perl lets you include external files in the output of a print statement. I am just trying to figure out how (and maybe it needs to be a whole function which parses and properly spits out the include file in the right place -- in order to get the formating correct - ie all the newlines in the right place)

      Modules such as Template Toolkit fall into an area half-way between pure CGI programming and Active Server Pages but there is also Perlscript which can be used as an ASP language. However it is a bit short of features and I cannot remember if anyone is still maintaining it.

      $japh->{'Caillte'} = $me;

Re: Can I do Server-Side-Includes in Perl?
by injunjoel (Priest) on Sep 23, 2003 at 22:57 UTC
    Are you sure include is what you want? You might solve your problem with require. That way the code you are including (with require) will get executed as apposed to just included.

    Originally posted as a Categorized Answer.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://7011]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2022-09-30 02:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer my indexes to start at:




    Results (125 votes). Check out past polls.

    Notices?