Listing 4.3. parsessi.pl CGI program for including and parsing a .shtml file.
use CGI::Form;
$q = new CGI::Form;
print $q->header();
print $q->start_html(-title=>"Include Example",-author=>'bdeng@adobe.com',
-BGCOLOR=>"DDDDFF");
print "<H1>Include Example</H1>\n";
if ($ENV{`PATH_INFO'} ne "") {
&includeHTMLFile($ENV{`PATH_INFO'});
} else {
print "<P>No file was specified to parse";
}
print $q->end_html();
sub includeHTMLFile {
local($theHTMLFile)=@_;
local($thePathToHTMLFile)="$ENV{`DOCUMENT_ROOT'}${theHTMLFile}";
open(IN,"< $thePathToHTMLFile") ||
die "Cannot open file: $thePathToHTMLFile for read!\n";
local(@lines)=<IN>;
close(IN);
foreach(@lines) {
if (/\<!\.\.\#include(.*)\.\.\>/) {
@parms=split(/ /,$1);
foreach(@parms) {
($key,$val)=split(/=/,$_);
$keys{$key}=$val;
}
&includeHTMLFile($keys{`file'});
} elsif (/\<\!\.\.\#echo(.*)\.\.\>/) {
@parms=split(/ /,$1);
foreach(@parms) {
($key,$val)=split(/=/,$_);
$keys{$key}=$val;
}
print $ENV{$keys{`var'}};
} else {
print;
}
}
}
Another method of incorporating raw HTML within your Perl script is to use what's known as here documents. Here documents are generally used to enclose large pieces of raw text and are constructed using ENDOFTEXT, where ENDOFTEXT is a label that you can arbitrarily choose. The rest of the Perl script will not be interpreted, but rather treated as plain text until the string ENDOFTEXT is found.
Note:
ENDOFTEXT must appear at the beginning of the line. All references to it after the first column are ignored.
An example of using a here document for embedding HTML text follows:
print <<ENDOFMYHTML; <H3>Important Information</H3> <P>This information is <B>very</B> important! Everything up until the <BLINK>ENDOFTEXT</BLINK> line will appear in this page. <HR> ENDOFTEXT