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

📄 ch19_14.htm

📁 By Tom Christiansen and Nathan Torkington ISBN 1-56592-243-3 First Edition, published August 1998
💻 HTM
字号:
<HTML><HEAD><TITLE>Recipe 19.13. Saving a Form to a File or Mail Pipe (Perl Cookbook)</TITLE><METANAME="DC.title"CONTENT="Perl Cookbook"><METANAME="DC.creator"CONTENT="Tom Christiansen &amp; Nathan Torkington"><METANAME="DC.publisher"CONTENT="O'Reilly &amp; Associates, Inc."><METANAME="DC.date"CONTENT="1999-07-02T01:45:45Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-243-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch19_01.htm"TITLE="19. CGI Programming"><LINKREL="prev"HREF="ch19_13.htm"TITLE="19.12. Writing a Multiscreen CGI Script"><LINKREL="next"HREF="ch19_15.htm"TITLE="19.14. Program: chemiserie"></HEAD><BODYBGCOLOR="#FFFFFF"><img alt="Book Home" border="0" src="gifs/smbanner.gif" usemap="#banner-map" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Perl Cookbook"><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><p><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch19_13.htm"TITLE="19.12. Writing a Multiscreen CGI Script"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 19.12. Writing a Multiscreen CGI Script"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"><ACLASS="chapter"REL="up"HREF="ch19_01.htm"TITLE="19. CGI Programming"></A></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch19_15.htm"TITLE="19.14. Program: chemiserie"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 19.14. Program: chemiserie"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch19-chap19_saving_0">19.13. Saving a Form to a File or Mail Pipe</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch19-pgfId-1478">Problem<ACLASS="indexterm"NAME="ch19-idx-1000005510-0"></A><ACLASS="indexterm"NAME="ch19-idx-1000005510-1"></A><ACLASS="indexterm"NAME="ch19-idx-1000005510-2"></A><ACLASS="indexterm"NAME="ch19-idx-1000005510-3"></A></A></H3><PCLASS="para">Your CGI script needs to save or mail the entire form contents to a file.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch19-pgfId-1484">Solution</A></H3><PCLASS="para">To store a form, use the CGI module's <CODECLASS="literal">save_parameters</CODE><ACLASS="indexterm"NAME="ch19-idx-1000005511-0"></A> function or <CODECLASS="literal">save</CODE> method, which take a filehandle argument. You can save to a file:</P><PRECLASS="programlisting"># first open and exclusively lock the fileopen(FH, &quot;&gt;&gt;/tmp/formlog&quot;)              or die &quot;can't append to formlog: $!&quot;;flock(FH, 2)                            or die &quot;can't flock formlog: $!&quot;;# either using the procedural interfaceuse CGI qw(:standard);save_parameters(*FH);                   # with CGI::save# or using the object interfaceuse CGI;$query = CGI-&gt;new();$query-&gt;save(*FH);close(FH)                               or die &quot;can't close formlog: $!&quot;;</PRE><PCLASS="para">Or, save to a pipe, such as one connected to a mailer process:</P><PRECLASS="programlisting">use CGI qw(:standard);open(MAIL, &quot;|/usr/lib/sendmail -oi -t&quot;) or die &quot;can't fork sendmail: $!&quot;;print MAIL &lt;&lt;EOF;From: $0 (your cgi script)To: hisname\@hishost.comSubject: mailed form submissionEOFsave_parameters(*MAIL);close(MAIL)                             or die &quot;can't close sendmail: $!&quot;; </PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch19-pgfId-1540">Discussion</A></H3><PCLASS="para">Sometimes all you want to do with form data is to save it for later use. The <CODECLASS="literal">save_parameters</CODE> function and <CODECLASS="literal">save</CODE> method in CGI.pm write form parameters to an open filehandle. That filehandle can be attached to an open file (preferably one opened in append mode and locked, as in the solution), or to a pipe whose other end is a mail program.</P><PCLASS="para">File entries are stored one per line as <CODECLASS="literal">variable=value</CODE> pairs, with any funny characters URL-escaped. Each record is separated by a line with a single equals sign. These are typically read back by calling the <CODECLASS="literal">CGI-&gt;new</CODE> method with a filehandle argument that manages all the unescaping automatically, as described below.</P><PCLASS="para">If you want to add extra information to your query before you save it, the <CODECLASS="literal">param</CODE> function (or method, if you're using the object-oriented interface) can be called with more than one argument, setting the value(s) of a form parameter. For example, here's how you would save a time stamp and the entire environment:</P><PRECLASS="programlisting">param(&quot;_timestamp&quot;, scalar localtime);param(&quot;_environs&quot;, %ENV);</PRE><PCLASS="para">Once you've got the forms in a file, process them by using the object interface.</P><PCLASS="para">To load a query object from a filehandle, call the <CODECLASS="literal">new</CODE> method with a filehandle argument. Each time you do this, it returns a complete form. When end of file is hit, the returned form has no parameters. The following code demonstrates this approach. It keeps a running total of all the <CODECLASS="literal">&quot;items</CODE> <CODECLASS="literal">request&quot;</CODE> parameters, but only if the form was not submitted from a <EMCLASS="emphasis">perl.com</EM> site. Remember, we added the <CODECLASS="literal">_environs</CODE> and <CODECLASS="literal">_timestamp</CODE> parameters when we wrote the file.</P><PRECLASS="programlisting">use CGI;open(FORMS, &quot;&lt; /tmp/formlog&quot;)       or die &quot;can't read formlog: $!&quot;;flock(FORMS, 1)                     or die &quot;can't lock formlog: $!&quot;;while ($query = CGI-&gt;new(*FORMS)) {    last unless $query-&gt;param();     # means end of file    %his_env = $query-&gt;param('_environs');    $count  += $query-&gt;param('items requested')                unless $his_env{REMOTE_HOST} =~ /(^|\.)perl\.com$/}print &quot;Total orders: $count\n&quot;;</PRE><PCLASS="para">File ownership and access permissions are an issue here, as they are in any files created by CGI script. <ACLASS="indexterm"NAME="ch19-idx-1000005513-0"></A><ACLASS="indexterm"NAME="ch19-idx-1000005513-1"></A><ACLASS="indexterm"NAME="ch19-idx-1000005513-2"></A><ACLASS="indexterm"NAME="ch19-idx-1000005513-3"></A></P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch19-pgfId-1580">See Also</A></H3><PCLASS="para"><ACLASS="xref"HREF="ch18_04.htm"TITLE="Sending Mail">Recipe 18.3</A>; <ACLASS="xref"HREF="ch19_04.htm"TITLE="Fixing a 500 Server Error">Recipe 19.3</A></P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch19_13.htm"TITLE="19.12. Writing a Multiscreen CGI Script"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 19.12. Writing a Multiscreen CGI Script"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="book"HREF="index.htm"TITLE="Perl Cookbook"><IMGSRC="../gifs/txthome.gif"ALT="Perl Cookbook"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch19_15.htm"TITLE="19.14. Program: chemiserie"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 19.14. Program: chemiserie"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">19.12. Writing a Multiscreen CGI Script</TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="index"HREF="index/index.htm"TITLE="Book Index"><IMGSRC="../gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228">19.14. Program: chemiserie</TD></TR></TABLE><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><FONTSIZE="-1"></DIV<!-- LIBRARY NAV BAR --> <img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p> <a href="copyrght.htm">Copyright &copy; 2002</a> O'Reilly &amp; Associates. All rights reserved.</font> </p> <map name="library-map"> <area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map> </BODY></HTML>

⌨️ 快捷键说明

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