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

📄 ch07_06.htm

📁 By Tom Christiansen and Nathan Torkington ISBN 1-56592-243-3 First Edition, published August 1998
💻 HTM
字号:
<HTML><HEAD><TITLE>Recipe 7.5. Creating Temporary Files (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:36:08Z"><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="ch07_01.htm"TITLE="7. File Access"><LINKREL="prev"HREF="ch07_05.htm"TITLE="7.4. Making Perl Report Filenames in Errors"><LINKREL="next"HREF="ch07_07.htm"TITLE="7.6. Storing Files Inside Your Program Text"></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="ch07_05.htm"TITLE="7.4. Making Perl Report Filenames in Errors"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 7.4. Making Perl Report Filenames in Errors"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"><ACLASS="chapter"REL="up"HREF="ch07_01.htm"TITLE="7. File Access"></A></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch07_07.htm"TITLE="7.6. Storing Files Inside Your Program Text"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 7.6. Storing Files Inside Your Program Text"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch07-60155">7.5. Creating Temporary Files</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch07-pgfId-550">Problem<ACLASS="indexterm"NAME="ch07-idx-1000009606-0"></A><ACLASS="indexterm"NAME="ch07-idx-1000009606-1"></A><ACLASS="indexterm"NAME="ch07-idx-1000009606-2"></A><ACLASS="indexterm"NAME="ch07-idx-1000009606-3"></A></A></H3><PCLASS="para">You need to create a temporary file and have it automatically deleted when your program exits. For instance, you want to write a temporary configuration file to feed a program you launch. In this case, you want to know the temporary file's name to tell the utility program. In other cases, you may just want a temporary file to write to and read from, and don't need to know its filename.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch07-pgfId-556">Solution</A></H3><PCLASS="para">If you don't need to know the file's name, use the <CODECLASS="literal">new_tmpfile</CODE><ACLASS="indexterm"NAME="ch07-idx-1000009607-0"></A> class method from the IO::File module to get a filehandle opened for reading and writing:</P><PRECLASS="programlisting">use IO::File;$fh = IO::File-&gt;new_tmpfile        or die &quot;Unable to make new temporary file: $!&quot;;</PRE><PCLASS="para">If you need to know the file's name, use the <CODECLASS="literal">tmpnam</CODE><ACLASS="indexterm"NAME="ch07-idx-1000009608-0"></A> function from the POSIX module to get a filename that you then open yourself:</P><PRECLASS="programlisting">use IO::File;use POSIX qw(tmpnam);# try new temporary filenames until we get one that didn't already existdo { $name = tmpnam() }    until $fh = IO::File-&gt;new($name, O_RDWR|O_CREAT|O_EXCL);# install atexit-style handler so that when we exit or die,# we automatically delete this temporary fileEND { unlink($name) or die &quot;Couldn't unlink $name : $!&quot; }# now go on to use the file ...</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch07-pgfId-596">Discussion</A></H3><PCLASS="para">If you only need scratch space, the IO::File module's <CODECLASS="literal">new_tmpfile</CODE> class method returns a filehandle connected to a temporary file that's been opened read-write by using the following code:</P><PRECLASS="programlisting">for (;;) {    $name = tmpnam();    sysopen(TMP, $tmpnam, O_RDWR | O_CREAT | O_EXCL) &amp;&amp; last;}unlink $tmpnam;</PRE><PCLASS="para">This file will be automatically deleted when your program finally exits or the file is closed. You cannot find its name to tell another process, because it doesn't have a name. In fact, on systems that support such semantics, the filename was already deleted before the method returned. A child process could inherit the open file descriptor, however.[<ACLASS="footnote"HREF="#ch07-pgfId-1000001139">2</A>]</P><BLOCKQUOTECLASS="footnote"><DIVCLASS="footnote"><PCLASS="para"><ACLASS="footnote"NAME="ch07-pgfId-1000001139">[2]</A> But you'd better set <CODECLASS="literal">$^F</CODE> to at least <CODECLASS="literal">fileno($fh)</CODE> before you <CODECLASS="literal">exec</CODE> anything.</P></DIV></BLOCKQUOTE><PCLASS="para">This shows <CODECLASS="literal">new_tmpfile</CODE> in action. We create a temporary file, write to it, rewind, and print what we wrote:</P><PRECLASS="programlisting">use IO::File;$fh = IO::File-&gt;new_tmpfile             or die &quot;IO::File-&gt;new_tmpfile: $!&quot;;$fh-&gt;autoflush(1);print $fh &quot;$i\n&quot; while $i++ &lt; 10;seek($fh, 0, 0)                         or die &quot;seek: $!&quot;;print &quot;Tmp file has: &quot;, &lt;$fh&gt;;</PRE><PCLASS="para">The second solution gets a temporary file whose name you can give to another process. We use the <CODECLASS="literal">POSIX::tmpnam</CODE> function, open the file ourselves, and delete it when we're done. We don't test whether a file of that name exists before opening it because that would introduce a race condition &nbsp;-  someone might create the file between our checking whether it exists and our creating the file.[<ACLASS="footnote"HREF="#ch07-pgfId-1000006777">3</A>] Instead, we wrap <CODECLASS="literal">tmpnam</CODE> in a loop to make sure we get a new file and don't accidentally clobber someone else's. Two processes shouldn't get the same filename from <CODECLASS="literal">new_tmpfile</CODE>, in theory.<ACLASS="indexterm"NAME="ch07-idx-1000009610-0"></A><ACLASS="indexterm"NAME="ch07-idx-1000009610-1"></A><ACLASS="indexterm"NAME="ch07-idx-1000009610-2"></A><ACLASS="indexterm"NAME="ch07-idx-1000009610-3"></A><ACLASS="indexterm"NAME="ch07-idx-1000009610-4"></A></P><BLOCKQUOTECLASS="footnote"><DIVCLASS="footnote"><PCLASS="para"><ACLASS="footnote"NAME="ch07-pgfId-1000006777">[3]</A> Race conditions are explained in <ACLASS="xref"HREF="ch19_05.htm"TITLE="Writing a Safe CGI Program">Recipe 19.4</A>.</P></DIV></BLOCKQUOTE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch07-pgfId-1000009508">See Also</A></H3><PCLASS="para">The documentation for the standard IO::File and POSIX modules (also in <ACLASS="olink"HREF="../prog/ch07_01.htm">Chapter 7</A> of <ACLASS="citetitle"HREF="../prog/index.htm"TITLE="Programming Perl"><CITECLASS="citetitle">Programming Perl</CITE></A>); <ACLASS="xref"HREF="ch07_20.htm"TITLE="Opening and Closing File Descriptors by Number">Recipe 7.19</A>; your system's <ICLASS="filename">tmpnam</I> (3) manpage</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="ch07_05.htm"TITLE="7.4. Making Perl Report Filenames in Errors"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 7.4. Making Perl Report Filenames in Errors"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="ch07_07.htm"TITLE="7.6. Storing Files Inside Your Program Text"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 7.6. Storing Files Inside Your Program Text"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">7.4. Making Perl Report Filenames in Errors</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">7.6. Storing Files Inside Your Program Text</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 + -