temp.html

来自「perl教程」· HTML 代码 · 共 971 行 · 第 1/4 页

HTML
971 行
字号
</pre>
</dd>
<dd>
<p>Returns the name of the temporary directory created.
Returns undef on failure.</p>
</dd>
<dd>
<p>Directory must be removed by the caller.</p>
</dd>
</li>
<dt><strong><a name="item_mktemp"><strong>mktemp</strong></a></strong>

<dd>
<p>Returns a valid temporary filename but does not guarantee
that the file will not be opened by someone else.</p>
</dd>
<dd>
<pre>
  <span class="variable">$unopened_file</span> <span class="operator">=</span> <span class="variable">mktemp</span><span class="operator">(</span><span class="variable">$template</span><span class="operator">);</span>
</pre>
</dd>
<dd>
<p>Template is the same as that required by mkstemp().</p>
</dd>
</li>
</dl>
<p>
</p>
<hr />
<h1><a name="posix_functions">POSIX FUNCTIONS</a></h1>
<p>This section describes the re-implementation of the <a href="#item_tmpnam"><code>tmpnam()</code></a>
and <a href="#item_tmpfile"><code>tmpfile()</code></a> functions described in <a href="../../lib/POSIX.html">the POSIX manpage</a>
using the <a href="#item_mkstemp"><code>mkstemp()</code></a> from this module.</p>
<p>Unlike the <a href="../../lib/POSIX.html">POSIX</a> implementations, the directory used
for the temporary file is not specified in a system include
file (<code>P_tmpdir</code>) but simply depends on the choice of <code>tmpdir()</code>
returned by <a href="../../lib/File/Spec.html">File::Spec</a>. On some implementations this
location can be set using the <code>TMPDIR</code> environment variable, which
may not be secure.
If this is a problem, simply use <a href="#item_mkstemp"><code>mkstemp()</code></a> and specify a template.</p>
<dl>
<dt><strong><a name="item_tmpnam"><strong>tmpnam</strong></a></strong>

<dd>
<p>When called in scalar context, returns the full name (including path)
of a temporary file (uses mktemp()). The only check is that the file does
not already exist, but there is no guarantee that that condition will
continue to apply.</p>
</dd>
<dd>
<pre>
  <span class="variable">$file</span> <span class="operator">=</span> <span class="variable">tmpnam</span><span class="operator">();</span>
</pre>
</dd>
<dd>
<p>When called in list context, a filehandle to the open file and
a filename are returned. This is achieved by calling <a href="#item_mkstemp"><code>mkstemp()</code></a>
after constructing a suitable template.</p>
</dd>
<dd>
<pre>
  <span class="operator">(</span><span class="variable">$fh</span><span class="operator">,</span> <span class="variable">$file</span><span class="operator">)</span> <span class="operator">=</span> <span class="variable">tmpnam</span><span class="operator">();</span>
</pre>
</dd>
<dd>
<p>If possible, this form should be used to prevent possible
race conditions.</p>
</dd>
<dd>
<p>See <a href="../../lib/File/Spec.html#tmpdir">tmpdir in the File::Spec manpage</a> for information on the choice of temporary
directory for a particular operating system.</p>
</dd>
</li>
<dt><strong><a name="item_tmpfile"><strong>tmpfile</strong></a></strong>

<dd>
<p>Returns the filehandle of a temporary file.</p>
</dd>
<dd>
<pre>
  <span class="variable">$fh</span> <span class="operator">=</span> <span class="variable">tmpfile</span><span class="operator">();</span>
</pre>
</dd>
<dd>
<p>The file is removed when the filehandle is closed or when the program
exits. No access to the filename is provided.</p>
</dd>
<dd>
<p>If the temporary file can not be created undef is returned.
Currently this command will probably not work when the temporary
directory is on an NFS file system.</p>
</dd>
</li>
</dl>
<p>
</p>
<hr />
<h1><a name="additional_functions">ADDITIONAL FUNCTIONS</a></h1>
<p>These functions are provided for backwards compatibility
with common tempfile generation C library functions.</p>
<p>They are not exported and must be addressed using the full package
name.</p>
<dl>
<dt><strong><a name="item_tempnam"><strong>tempnam</strong></a></strong>

<dd>
<p>Return the name of a temporary file in the specified directory
using a prefix. The file is guaranteed not to exist at the time
the function was called, but such guarantees are good for one
clock tick only.  Always use the proper form of <a href="../../lib/Pod/perlfunc.html#item_sysopen"><code>sysopen</code></a>
with <code>O_CREAT | O_EXCL</code> if you must open such a filename.</p>
</dd>
<dd>
<pre>
  <span class="variable">$filename</span> <span class="operator">=</span> <span class="variable">File::Temp::tempnam</span><span class="operator">(</span> <span class="variable">$dir</span><span class="operator">,</span> <span class="variable">$prefix</span> <span class="operator">);</span>
</pre>
</dd>
<dd>
<p>Equivalent to running <a href="#item_mktemp"><code>mktemp()</code></a> with $dir/$prefixXXXXXXXX
(using unix file convention as an example)</p>
</dd>
<dd>
<p>Because this function uses mktemp(), it can suffer from race conditions.</p>
</dd>
</li>
</dl>
<p>
</p>
<hr />
<h1><a name="utility_functions">UTILITY FUNCTIONS</a></h1>
<p>Useful functions for dealing with the filehandle and filename.</p>
<dl>
<dt><strong><a name="item_unlink0"><strong>unlink0</strong></a></strong>

<dd>
<p>Given an open filehandle and the associated filename, make a safe
unlink. This is achieved by first checking that the filename and
filehandle initially point to the same file and that the number of
links to the file is 1 (all fields returned by <a href="../../lib/Pod/perlfunc.html#item_stat"><code>stat()</code></a> are compared).
Then the filename is unlinked and the filehandle checked once again to
verify that the number of links on that file is now 0.  This is the
closest you can come to making sure that the filename unlinked was the
same as the file whose descriptor you hold.</p>
</dd>
<dd>
<pre>
  <span class="variable">unlink0</span><span class="operator">(</span><span class="variable">$fh</span><span class="operator">,</span> <span class="variable">$path</span><span class="operator">)</span>
     <span class="keyword">or</span> <span class="keyword">die</span> <span class="string">"Error unlinking file $path safely"</span><span class="operator">;</span>
</pre>
</dd>
<dd>
<p>Returns false on error. The filehandle is not closed since on some
occasions this is not required.</p>
</dd>
<dd>
<p>On some platforms, for example Windows NT, it is not possible to
unlink an open file (the file must be closed first). On those
platforms, the actual unlinking is deferred until the program ends and
good status is returned. A check is still performed to make sure that
the filehandle and filename are pointing to the same thing (but not at
the time the end block is executed since the deferred removal may not
have access to the filehandle).</p>
</dd>
<dd>
<p>Additionally, on Windows NT not all the fields returned by <a href="../../lib/Pod/perlfunc.html#item_stat"><code>stat()</code></a> can
be compared. For example, the <code>dev</code> and <code>rdev</code> fields seem to be
different.  Also, it seems that the size of the file returned by <a href="../../lib/Pod/perlfunc.html#item_stat"><code>stat()</code></a>
does not always agree, with <a href="../../lib/Pod/perlfunc.html#item_stat"><code>stat(FH)</code></a> being more accurate than
<a href="../../lib/Pod/perlfunc.html#item_stat"><code>stat(filename)</code></a>, presumably because of caching issues even when
using autoflush (this is usually overcome by waiting a while after
writing to the tempfile before attempting to <a href="#item_unlink0"><code>unlink0</code></a> it).</p>
</dd>
<dd>
<p>Finally, on NFS file systems the link count of the file handle does
not always go to zero immediately after unlinking. Currently, this
command is expected to fail on NFS disks.</p>
</dd>
<dd>
<p>This function is disabled if the global variable $KEEP_ALL is true
and an unlink on open file is supported. If the unlink is to be deferred
to the END block, the file is still registered for removal.</p>
</dd>
</li>
<dt><strong><a name="item_cmpstat"><strong>cmpstat</strong></a></strong>

<dd>
<p>Compare <a href="../../lib/Pod/perlfunc.html#item_stat"><code>stat</code></a> of filehandle with <a href="../../lib/Pod/perlfunc.html#item_stat"><code>stat</code></a> of provided filename.  This
can be used to check that the filename and filehandle initially point
to the same file and that the number of links to the file is 1 (all
fields returned by <a href="../../lib/Pod/perlfunc.html#item_stat"><code>stat()</code></a> are compared).</p>
</dd>
<dd>
<pre>
  <span class="variable">cmpstat</span><span class="operator">(</span><span class="variable">$fh</span><span class="operator">,</span> <span class="variable">$path</span><span class="operator">)</span>
     <span class="keyword">or</span> <span class="keyword">die</span> <span class="string">"Error comparing handle with file"</span><span class="operator">;</span>
</pre>
</dd>
<dd>
<p>Returns false if the stat information differs or if the link count is
greater than 1.</p>
</dd>
<dd>
<p>On certain platofms, eg Windows, not all the fields returned by <a href="../../lib/Pod/perlfunc.html#item_stat"><code>stat()</code></a>
can be compared. For example, the <code>dev</code> and <code>rdev</code> fields seem to be
different in Windows.  Also, it seems that the size of the file
returned by <a href="../../lib/Pod/perlfunc.html#item_stat"><code>stat()</code></a> does not always agree, with <a href="../../lib/Pod/perlfunc.html#item_stat"><code>stat(FH)</code></a> being more
accurate than <a href="../../lib/Pod/perlfunc.html#item_stat"><code>stat(filename)</code></a>, presumably because of caching issues
even when using autoflush (this is usually overcome by waiting a while
after writing to the tempfile before attempting to <a href="#item_unlink0"><code>unlink0</code></a> it).</p>
</dd>
<dd>
<p>Not exported by default.</p>
</dd>
</li>
<dt><strong><a name="item_unlink1"><strong>unlink1</strong></a></strong>

<dd>
<p>Similar to <a href="#item_unlink0"><code>unlink0</code></a> except after file comparison using cmpstat, the
filehandle is closed prior to attempting to unlink the file. This
allows the file to be removed without using an END block, but does
mean that the post-unlink comparison of the filehandle state provided
by <a href="#item_unlink0"><code>unlink0</code></a> is not available.</p>
</dd>
<dd>
<pre>
  <span class="variable">unlink1</span><span class="operator">(</span><span class="variable">$fh</span><span class="operator">,</span> <span class="variable">$path</span><span class="operator">)</span>
     <span class="keyword">or</span> <span class="keyword">die</span> <span class="string">"Error closing and unlinking file"</span><span class="operator">;</span>
</pre>
</dd>
<dd>
<p>Usually called from the object destructor when using the OO interface.</p>
</dd>
<dd>
<p>Not exported by default.</p>
</dd>
<dd>
<p>This function is disabled if the global variable $KEEP_ALL is true.</p>
</dd>
</li>
<dt><strong><a name="item_cleanup"><strong>cleanup</strong></a></strong>

<dd>
<p>Calling this function will cause any temp files or temp directories

⌨️ 快捷键说明

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