Prepend the code below to an HTML file then drop it into the CGI script folder to get served up using the file name in the URL. For example drop index.html into the directory where your CGI scripts are, prepend the header and access it as http://dibbly.wobbly.com/cgi-bin/index.html

Update: add missing use HTML::Template. But see replies in any case! (One ought not rush to print!)

#!Perl -w use strict; use CGI::Pretty qw(:standard :cgi-lib); use HTML::Template; print header(); my $template = HTML::Template->new (filehandle => *DATA); print $template->output (); __DATA__

Replies are listed 'Best First'.
Re: Perl header to serve up a .HTML file from a CGI directory
by davorg (Chancellor) on Mar 13, 2007 at 10:13 UTC

      No need to load all that perl nonsense. I'm using this:

      #!/usr/bin/tail -n+2 Content-Type: text/plain; charset=ISO-8859-2
      Replacing the content type with the approperiate one of course (I'm using this with a PNG file too).

      Or even better:

      #!Perl -w use strict; use CGI qw(header); print header (), <DATA>; __DATA__

      DWIM is Perl's answer to Gödel
Re: Perl header to serve up a .HTML file from a CGI directory
by hangon (Deacon) on Mar 14, 2007 at 10:08 UTC
    This snippet as written may not have much practical use, but it illustrates some interesting concepts. You can add some quick and dirty dynamic aspects to what appears to be a normal web page. For one thing, this technique could be used to insert cookies into an otherwise static html file. A ScriptAlias could be used to make it look like it's being served from a directory other than cgi-bin. Other possibilities can be imagined too, particularly if you want to hide how your site works - kind of a security by obscurity layer.
Re: Perl header to serve up a .HTML file from a CGI directory
by graff (Chancellor) on Mar 14, 2007 at 03:31 UTC
    Okay, Gramps, I know that you're really more clever than me on average, so I must be missing something... :)

    Why would a web-data provider want to put html files in the cgi-bin directory? And/or why would a browser user find it more attractive to use a url like "www.hostname.xxx/cgi-bin/somepage.html", as opposed to something like "www.hostname.xxx/somepurpose/somepage.html" or just "www.hostname.xxx/sompage.html"?

    On a unix-based web server, wouldn't a cgi-bin/foo.html file need to be flagged (chmod) executable? And/or would the web-server's config file need some tweak that enables *.html files to be treated as executable code? I don't doubt that the technique shown here is working for you, but I wonder if there might be some "issues" you haven't discussed that others might trip over.

      It was really just a quick way of dumping a mixture of "plain" html and HTML::Template fodder into the same place as the cgi scripts for testing. It's actually in a Windows Apache context so the executable flag's not an issue, but I wanted to keep the files together to make it a little easier to manage them with Subversion during the prototyping phase.

      I imagine these files are going to get a lot of hacking while I argue over site layout with the person who wants the work done.

      And of course you can factor almost complete ignorance of CGI, Apache and HTML/HTML::Template into all of that too - I've been writing CGI script and running an Apache server for testing for a few weeks.

      More clever at the XP game maybe - but I'm not sure that counts for a huge amount. Sure can't convert XP to cash. :-D


      DWIM is Perl's answer to Gödel