[Previous] [Contents] [Next]


Listing 4.2. Perl subroutine for including another HTML file.



 sub includeHTMLFile {

     local($theHTMLFile)=@_;

     local($thePathToHTMLFile)="$ENV{`DOCUMENT_ROOT'}/$theHTMLFile";

     open(IN,"< $thePathToHTMLFile") ||

         die "Cannot open file: $thePathToHTMLFile for read!\n";

     print(<IN>);

     close(IN);

 }


In this example, the CGI environment variable DOCUMENT_ROOT determines the location of the included file. This is one of several well-defined environment variables for use in CGI programming.

You can extend the function in Listing 4.2 to process .shtml files and perform the proper action on the directives. Listing 4.3 shows a CGI program that uses the PATH_INFO environment variable to specify the .shtml file to parse. This CGI program can be specified as http://foo.bar.com/cgi-bin/parsessi.pl/included.shtml. PATH_INFO will contain /included.shtml, and that document will be returned with the proper SSI parsing having taken place.

[Previous] [Contents] [Next]