CGI and Perl

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

Closing Up

To end your HTML document, you should call the end_html() method, which inserts the </BODY> and </HTML> tags for you.

print $q->end_html();

Heady StuffGenerating More Advanced (Possibly Dynamic) HTML

The concepts you have seen so far are fairly straightforward. Sometimes, you might want to do more than just display a few fields and process the Submit button. The next step is to take the initial building blocks and produce more complicated forms using other HTML tags. This section discusses some more advanced HTML generation using the Perl5 libraries.

Other Elements Within Elements

You also might want to incorporate form fields within other types of tags. This is easily done by combining the HTML for your special tags together with the output from these methods to produce formatted forms. This section discusses these approaches to work around some limitations. Using Tables for Better Field Layout One major drawback in HTML is the lack of ability to align fields. Suppose you wish to lay out your fields along a grid. If you desire this kind of effect, the trick is to use tables, which are available in HTML 3.2. Tables are defined using the <TABLE> and </TABLE> tags. Rows within a table are defined using the <TR> and </TR> tags. Columns within the row are defined using the <TD> and </TD> tags. Header columns within the row are defined using the <TH> and </TH> tags. Let's take the restaurant questionnaire form and make it look a bit nicer using a table. Although the <TABLE> tag provides a BORDER option, you probably won't want to use borders around your form fields. Borders are more suitable for displaying a table of data. The text prompts should go in the first column, and the others should go in the second column. You no longer need the line breaks because these are inferred by the table row definitions. The Perl code will now look something like Listing 4.4.

by BrainBellupdated
Advertisement: