Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Can I do Server-Side-Includes in Perl?

by rodry (Beadle)
on Apr 06, 2000 at 10:59 UTC ( #7011=categorized question: print w/replies, xml ) Need Help??
Contributed by rodry on Apr 06, 2000 at 10:59 UTC
Q&A  > CGI programming


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

Answer: Can I do Server-Side-Includes in Perl?
contributed by chromatic

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: $!"; # } }
Answer: Can I do Server-Side-Includes in Perl?
contributed by brother ab

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)
Answer: Can I do Server-Side-Includes in Perl?
contributed by clunis

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':


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.

Answer: Can I do Server-Side-Includes in Perl?
contributed by saintbrie

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
Answer: Can I do Server-Side-Includes in Perl?
contributed by btrott

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.

Answer: Can I do Server-Side-Includes in Perl?
contributed by cianoz

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)

Answer: Can I do Server-Side-Includes in Perl?
contributed by Anonymous Monk

Yes you can actually. You can install mod_filter and have the perl scripts run through mod_include. Take a look at

Answer: Can I do Server-Side-Includes in Perl?
contributed by QwertyD

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.
Answer: Can I do Server-Side-Includes in Perl?
contributed by transmission_err

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.

Please (register and) log in if you wish to add an answer

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others scrutinizing the Monastery: (3)
    As of 2020-08-08 03:42 GMT
    Find Nodes?
      Voting Booth?
      Which rocket would you take to Mars?

      Results (51 votes). Check out past polls.