⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch04.htm

📁 Web_Programming_with_Perl5,一个不错的Perl语言教程。
💻 HTM
📖 第 1 页 / 共 4 页
字号:
of information is the MIME type. It is very important that you specify the correct



MIME type for your HTML data. The first line of your output should always be <TT>Content-Type:



text/html;</TT>. This line tells the browser what kind of data to expect. Fortunately,



by using <TT>CGI::Form</TT>, you need not worry about this detail because it is the



default type. Simply calling the method <TT>header</TT> returns the correct MIME



type for you.</P>



<P>In addition to the document header, you will also need to correctly specify information



about the types of any forms that are included in the document. This section will



describe the two different types of forms as well.</P>



<PRE><FONT COLOR="#0066FF">print $q-&gt;header();



</FONT></PRE>



<P><B><TT>The HTML Header</TT></B> The next step is to construct the HTML header.



The HTML header usually consists of just a title but may also include things that



go into the <TT>&lt;BODY&gt;</TT> tag, such as background and foreground colors.



Using the <TT>start_html</TT> method in <TT>CGI::Form</TT> provides a nice way to



incorporate all of your HTML header information. In this example, I am starting an



HTML document with a nice lavender background color (specified as a hexadecimal RGB



triplet).</P>



<PRE><FONT COLOR="#0066FF">print $q-&gt;start_html(-title=&gt;&quot;Customer Survey Page&quot;,-author=&gt;'bdeng@adobe.com',



                    -BGCOLOR=&gt; &quot;DDDDFF&quot;);



</FONT></PRE>



<P>Not included in this function, however, is the new HTML 3.2<TT> &lt;!DOCTYPE&gt;</TT>



directive. This directive is what all HTML 3.2 conforming documents should begin



with. This directive is actually part of SGML, but it is used to identify the HTML



document as such. More information about HTML and SGML can be found at <TT>http://www.w3.org/pub/WWW/MarkUp</TT>.



Hopefully, the <TT>start_html</TT> method will incorporate this standard at some



point. <B><TT>Types of Forms</TT></B> Two different MIME types are currently defined



for HTML forms. The first is <TT>application/ x-www-form-urlencoded.</TT> This is



the original type defined for simple forms. The second is <TT>multipart/form-data</TT>.



This type was introduced by Netscape 2.0 for the transfer of large chunks of data.



It is important that you choose your form type appropriately. Generally speaking,



if you need to support many different types of browsers, some of which may not handle



<TT>multipart/ form-data</TT>, choose the original MIME type. However, if you want



to use such fields as the file upload field, you must choose <TT>multipart/form-data</TT>.



Eventually, all browsers should support <TT>multipart/form-data</TT>, at which point,



it would be the obvious choice.</P>



<P>There are two methods within <TT>CGI::Form</TT> for starting your form--<TT>startform()</TT>



and <TT>start_multipart_form()</TT>. You should use whichever one corresponds to



the type of form you are creating. These methods take the optional parameters <TT>$method</TT>,



<TT>$request</TT>, and <TT>$encoding</TT>. You shouldn't need to worry about the



<TT>$encoding</TT> parameter because the method you choose will specify the appropriate



encoding mechanism for your form. <TT>$method</TT> defaults to <TT>POST</TT>, and



<TT>$action</TT> defaults to the current CGI script, which is generally what you



want.



<H4 ALIGN="CENTER"><A NAME="Heading20"></A><FONT COLOR="#000077">Constructing Elements</FONT></H4>



<P>You have seen a few examples of how to define fields using <TT>CGI::Form</TT>.



Now let's look at an example of a complete form. What you'll do is create a general



questionnaire form for a restaurant. You'll ask the user these things: What did you



order? (text field)</P>



<P>How was the service? (radio group)</P>



<P>How did the food taste? (radio group)</P>



<P>What is your age group? (popup menu)</P>



<PRE><FONT COLOR="#0066FF">



Would you come back again? (checkbox)



</FONT></PRE>



<P>The Perl code in Listing 4.1 generates these fields using a <TT>multipart/form-data</TT>



type form.



<H3 ALIGN="CENTER"><A NAME="Heading21"></A><FONT COLOR="#000077">Listing 4.1. Perl



code for customer questionnaire.</FONT></H3>



<PRE><FONT COLOR="#0066FF">



use CGI::Form;



$q = new CGI::Form;







print &quot;&lt;H1&gt;Customer Survey&lt;/H1&gt;\n&quot;;



print $q-&gt;start_multipart_form();



print &quot;What did you order? &quot;;



print $q-&gt;textfield( -name=&gt;'order', -value=&gt;&quot;&quot;, -maxlength=&gt;30, -size=&gt;30 );



print &quot;&lt;BR&gt;&quot;; # Line break



print &quot;How was the service? (5-good,1-bad) &quot;;



print $q-&gt;radio_group( -name=&gt;'serviceRating', -values=&gt;[`1','2','3','4','5'],



                       -default=&gt;'3' );



print &quot;&lt;BR&gt;&quot;;



print &quot;How did the food taste? (5-good,1-bad) &quot;;



print $q-&gt;radio_group( -name=&gt;'tasteRating', -values=&gt;[`1','2','3','4','5'],



                       -default=&gt;'3' );



print &quot;&lt;BR&gt;&quot;;



print &quot;What age group are you in? &quot;;



@ages=( `child', `teen', `20s', `30s', `40s', `50s', `senior' );



print $q-&gt;popup_menu( -name=&gt;'ageGroup', -values=&gt;\@ages );



print &quot;&lt;BR&gt;&quot;;



print $q-&gt;checkbox( -name=&gt;'comeAgain', -checked=&gt;'checked', -value=&gt;'yes',



                    -label=&gt;&quot;I would come back again &quot; );



print &quot;&lt;BR&gt;&lt;BR&gt;&quot;;



print $q-&gt;submit( -name=&gt;'Action', -value=&gt;'Submit' );



print $q-&gt;reset();



print $q-&gt;endform();



</FONT></PRE>



<P>This form appears in the browser, as shown in Figure 4.1.</P>



<P>You can include more than one form on a single HTML page. Each form can be associated



with separate actions or the same action. <BR>



<BR>



<B><TT>Figure 4.1.</TT></B><TT> </TT>The questionnaire as it appears in the browser.



<H4 ALIGN="CENTER"><A NAME="Heading22"></A><FONT COLOR="#000077">Included Files</FONT></H4>



<P>One feature that is provided by some Web servers is server-side includes. You



can configure your Web server in such a way that files of certain extensions (let's



use the common one, <TT>.shtml</TT>) are parsed by the server before they are sent



down to the client. There are two directives that tell the server to perform a specific



operation with the file.</P>



<PRE><FONT COLOR="#0066FF">&lt;!..#echo....&gt; 



</FONT></PRE>



<P>and</P>



<PRE><FONT COLOR="#0066FF">&lt;!..#include...&gt;



</FONT></PRE>



<P>The server-side include feature is used to dynamically include nested HTML files.



This feature enables some limited dynamically created content. The <TT>include</TT>



directive is used to include other HTML files, much like a C program including header



files. The <TT>echo</TT> directive allows you to dynamically display certain variables.



The variables that can be displayed using the <TT>echo</TT> directive are as follows:</P>



<PRE><FONT COLOR="#0066FF">DATE_GMT



DATE_LOCAL



DOCUMENT_NAME



DOCUMENT_URI



LAST_MODIFIED



QUERY_STRING_UNESCAPED



</FONT></PRE>



<P>For more information on SSIs, refer to HTML &amp; CGI Unleashed (Sams, 1995).</P>



<P>As you saw previously, all of the HTML that you generate from your Perl script



is dynamic. It would be very easy to incorporate this kind of information using simple



print statements. For example, to print the local date, instead of using an <TT>echo</TT>



directive you might write the following:</P>



<PRE><FONT COLOR="#0066FF">print &quot;The current local time is: &quot; . localtime() . &quot;\n&quot;;



</FONT></PRE>



<P>Or, to print Greenwich Mean Time, you could write:</P>



<PRE><FONT COLOR="#0066FF">print &quot;The current time in Greenwich is: &quot; . gmtime() . &quot;\n&quot;;



</FONT></PRE>



<P>The <TT>include</TT> directive of server-side includes can be easily implemented



in Perl code, as in List-ing 4.2.



<H3 ALIGN="CENTER"><A NAME="Heading23"></A><FONT COLOR="#000077">Listing 4.2. Perl



subroutine for including another HTML file.</FONT></H3>



<PRE><FONT COLOR="#0066FF">



sub includeHTMLFile {



    local($theHTMLFile)=@_;



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



    open(IN,&quot;&lt; $thePathToHTMLFile&quot;) ||



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



    print(&lt;IN&gt;);



    close(IN);



}



</FONT></PRE>



<P>In this example, the CGI environment variable <TT>DOCUMENT_ROOT</TT> determines



the location of the included file. This is one of several well-defined environment



variables for use in CGI programming.</P>



<P>You can extend the function in Listing 4.2 to process <TT>.shtml</TT> files and



perform the proper action on the directives. Listing 4.3 shows a CGI program that



uses the <TT>PATH_INFO</TT> environment variable to specify the <TT>.shtml</TT> file



to parse. This CGI program can be specified as <TT>http://foo.bar.com/cgi-bin/parsessi.pl/included.shtml</TT>.



<TT>PATH_INFO</TT> will contain <TT>/included.shtml</TT>, and that document will



be returned with the proper SSI parsing having taken place.



<H3 ALIGN="CENTER"><A NAME="Heading24"></A><FONT COLOR="#000077">Listing 4.3. parsessi.pl



CGI program for including and parsing a .shtml file.</FONT></H3>



<PRE><FONT COLOR="#0066FF">



use CGI::Form;



$q = new CGI::Form;







print $q-&gt;header();



print $q-&gt;start_html(-title=&gt;&quot;Include Example&quot;,-author=&gt;'bdeng@adobe.com',



                    -BGCOLOR=&gt;&quot;DDDDFF&quot;);



print &quot;&lt;H1&gt;Include Example&lt;/H1&gt;\n&quot;;



if ($ENV{`PATH_INFO'} ne &quot;&quot;) {



   &amp;includeHTMLFile($ENV{`PATH_INFO'});



} else {



   print &quot;&lt;P&gt;No file was specified to parse&quot;;



}



print $q-&gt;end_html();







sub includeHTMLFile {



    local($theHTMLFile)=@_;



    local($thePathToHTMLFile)=&quot;$ENV{`DOCUMENT_ROOT'}${theHTMLFile}&quot;;



    open(IN,&quot;&lt; $thePathToHTMLFile&quot;) ||



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



    local(@lines)=&lt;IN&gt;;



    close(IN);



    foreach(@lines) {



        if (/\&lt;!\.\.\#include(.*)\.\.\&gt;/) {



            @parms=split(/ /,$1);



            foreach(@parms) {



               ($key,$val)=split(/=/,$_);



               $keys{$key}=$val;



            }



            &amp;includeHTMLFile($keys{`file'});



        } elsif (/\&lt;\!\.\.\#echo(.*)\.\.\&gt;/) {



            @parms=split(/ /,$1);



            foreach(@parms) {



               ($key,$val)=split(/=/,$_);



               $keys{$key}=$val;



            }



            print $ENV{$keys{`var'}};



        } else {



            print;



        }



    }



}



</FONT></PRE>



<P>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 <TT>ENDOFTEXT</TT>, where <TT>ENDOFTEXT</TT>



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 <TT>ENDOFTEXT</TT>



is found.







<DL>



	<DT></DT>



</DL>







<H3 ALIGN="CENTER">



<HR WIDTH="83%">



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -