temp.html
来自「perl教程」· HTML 代码 · 共 971 行 · 第 1/4 页
HTML
971 行
<dl>
<dt><strong><a name="item_tempfile"><strong>tempfile</strong></a></strong>
<dd>
<p>This is the basic function to generate temporary files.
The behaviour of the file can be changed using various options:</p>
</dd>
<dd>
<pre>
<span class="variable">$fh</span> <span class="operator">=</span> <span class="variable">tempfile</span><span class="operator">();</span>
<span class="operator">(</span><span class="variable">$fh</span><span class="operator">,</span> <span class="variable">$filename</span><span class="operator">)</span> <span class="operator">=</span> <span class="variable">tempfile</span><span class="operator">();</span>
</pre>
</dd>
<dd>
<p>Create a temporary file in the directory specified for temporary
files, as specified by the <code>tmpdir()</code> function in <a href="../../lib/File/Spec.html">the File::Spec manpage</a>.</p>
</dd>
<dd>
<pre>
<span class="operator">(</span><span class="variable">$fh</span><span class="operator">,</span> <span class="variable">$filename</span><span class="operator">)</span> <span class="operator">=</span> <span class="variable">tempfile</span><span class="operator">(</span><span class="variable">$template</span><span class="operator">);</span>
</pre>
</dd>
<dd>
<p>Create a temporary file in the current directory using the supplied
template. Trailing `X' characters are replaced with random letters to
generate the filename. At least four `X' characters must be present
at the end of the template.</p>
</dd>
<dd>
<pre>
($fh, $filename) = tempfile($template, SUFFIX => $suffix)</pre>
</dd>
<dd>
<p>Same as previously, except that a suffix is added to the template
after the `X' translation. Useful for ensuring that a temporary
filename has a particular extension when needed by other applications.
But see the WARNING at the end.</p>
</dd>
<dd>
<pre>
<span class="operator">(</span><span class="variable">$fh</span><span class="operator">,</span> <span class="variable">$filename</span><span class="operator">)</span> <span class="operator">=</span> <span class="variable">tempfile</span><span class="operator">(</span><span class="variable">$template</span><span class="operator">,</span> <span class="string">DIR</span> <span class="operator">=></span> <span class="variable">$dir</span><span class="operator">);</span>
</pre>
</dd>
<dd>
<p>Translates the template as before except that a directory name
is specified.</p>
</dd>
<dd>
<pre>
<span class="operator">(</span><span class="variable">$fh</span><span class="operator">,</span> <span class="variable">$filename</span><span class="operator">)</span> <span class="operator">=</span> <span class="variable">tempfile</span><span class="operator">(</span><span class="variable">$template</span><span class="operator">,</span> <span class="string">UNLINK</span> <span class="operator">=></span> <span class="number">1</span><span class="operator">);</span>
</pre>
</dd>
<dd>
<p>Return the filename and filehandle as before except that the file is
automatically removed when the program exits (dependent on
$KEEP_ALL). Default is for the file to be removed if a file handle is
requested and to be kept if the filename is requested. In a scalar
context (where no filename is returned) the file is always deleted
either (depending on the operating system) on exit or when it is
closed (unless $KEEP_ALL is true when the temp file is created).</p>
</dd>
<dd>
<p>Use the object-oriented interface if fine-grained control of when
a file is removed is required.</p>
</dd>
<dd>
<p>If the template is not specified, a template is always
automatically generated. This temporary file is placed in <code>tmpdir()</code>
(<a href="../../lib/File/Spec.html">the File::Spec manpage</a>) unless a directory is specified explicitly with the
DIR option.</p>
</dd>
<dd>
<pre>
<span class="variable">$fh</span> <span class="operator">=</span> <span class="variable">tempfile</span><span class="operator">(</span> <span class="variable">$template</span><span class="operator">,</span> <span class="string">DIR</span> <span class="operator">=></span> <span class="variable">$dir</span> <span class="operator">);</span>
</pre>
</dd>
<dd>
<p>If called in scalar context, only the filehandle is returned and the
file will automatically be deleted when closed on operating systems
that support this (see the description of <a href="#item_tmpfile"><code>tmpfile()</code></a> elsewhere in this
document). This is the preferred mode of operation, as if you only
have a filehandle, you can never create a race condition by fumbling
with the filename. On systems that can not unlink an open file or can
not mark a file as temporary when it is opened (for example, Windows
NT uses the <code>O_TEMPORARY</code> flag) the file is marked for deletion when
the program ends (equivalent to setting UNLINK to 1). The <code>UNLINK</code>
flag is ignored if present.</p>
</dd>
<dd>
<pre>
<span class="operator">(</span><span class="keyword">undef</span><span class="operator">,</span> <span class="variable">$filename</span><span class="operator">)</span> <span class="operator">=</span> <span class="variable">tempfile</span><span class="operator">(</span><span class="variable">$template</span><span class="operator">,</span> <span class="string">OPEN</span> <span class="operator">=></span> <span class="number">0</span><span class="operator">);</span>
</pre>
</dd>
<dd>
<p>This will return the filename based on the template but
will not open this file. Cannot be used in conjunction with
UNLINK set to true. Default is to always open the file
to protect from possible race conditions. A warning is issued
if warnings are turned on. Consider using the <a href="#item_tmpnam"><code>tmpnam()</code></a>
and <a href="#item_mktemp"><code>mktemp()</code></a> functions described elsewhere in this document
if opening the file is not required.</p>
</dd>
<dd>
<p>Options can be combined as required.</p>
</dd>
</li>
<dt><strong><a name="item_tempdir"><strong>tempdir</strong></a></strong>
<dd>
<p>This is the recommended interface for creation of temporary directories.
The behaviour of the function depends on the arguments:</p>
</dd>
<dd>
<pre>
<span class="variable">$tempdir</span> <span class="operator">=</span> <span class="variable">tempdir</span><span class="operator">();</span>
</pre>
</dd>
<dd>
<p>Create a directory in <code>tmpdir()</code> (see <a href="../../lib/File/Spec.html">File::Spec</a>).</p>
</dd>
<dd>
<pre>
<span class="variable">$tempdir</span> <span class="operator">=</span> <span class="variable">tempdir</span><span class="operator">(</span> <span class="variable">$template</span> <span class="operator">);</span>
</pre>
</dd>
<dd>
<p>Create a directory from the supplied template. This template is
similar to that described for tempfile(). `X' characters at the end
of the template are replaced with random letters to construct the
directory name. At least four `X' characters must be in the template.</p>
</dd>
<dd>
<pre>
<span class="variable">$tempdir</span> <span class="operator">=</span> <span class="variable">tempdir</span> <span class="operator">(</span> <span class="string">DIR</span> <span class="operator">=></span> <span class="variable">$dir</span> <span class="operator">);</span>
</pre>
</dd>
<dd>
<p>Specifies the directory to use for the temporary directory.
The temporary directory name is derived from an internal template.</p>
</dd>
<dd>
<pre>
<span class="variable">$tempdir</span> <span class="operator">=</span> <span class="variable">tempdir</span> <span class="operator">(</span> <span class="variable">$template</span><span class="operator">,</span> <span class="string">DIR</span> <span class="operator">=></span> <span class="variable">$dir</span> <span class="operator">);</span>
</pre>
</dd>
<dd>
<p>Prepend the supplied directory name to the template. The template
should not include parent directory specifications itself. Any parent
directory specifications are removed from the template before
prepending the supplied directory.</p>
</dd>
<dd>
<pre>
<span class="variable">$tempdir</span> <span class="operator">=</span> <span class="variable">tempdir</span> <span class="operator">(</span> <span class="variable">$template</span><span class="operator">,</span> <span class="string">TMPDIR</span> <span class="operator">=></span> <span class="number">1</span> <span class="operator">);</span>
</pre>
</dd>
<dd>
<p>Using the supplied template, create the temporary directory in
a standard location for temporary files. Equivalent to doing</p>
</dd>
<dd>
<pre>
<span class="variable">$tempdir</span> <span class="operator">=</span> <span class="variable">tempdir</span> <span class="operator">(</span> <span class="variable">$template</span><span class="operator">,</span> <span class="string">DIR</span> <span class="operator">=></span> <span class="variable">File::Spec</span><span class="operator">-></span><span class="variable">tmpdir</span><span class="operator">);</span>
</pre>
</dd>
<dd>
<p>but shorter. Parent directory specifications are stripped from the
template itself. The <code>TMPDIR</code> option is ignored if <code>DIR</code> is set
explicitly. Additionally, <code>TMPDIR</code> is implied if neither a template
nor a directory are supplied.</p>
</dd>
<dd>
<pre>
<span class="variable">$tempdir</span> <span class="operator">=</span> <span class="variable">tempdir</span><span class="operator">(</span> <span class="variable">$template</span><span class="operator">,</span> <span class="string">CLEANUP</span> <span class="operator">=></span> <span class="number">1</span><span class="operator">);</span>
</pre>
</dd>
<dd>
<p>Create a temporary directory using the supplied template, but
attempt to remove it (and all files inside it) when the program
exits. Note that an attempt will be made to remove all files from
the directory even if they were not created by this module (otherwise
why ask to clean it up?). The directory removal is made with
the <code>rmtree()</code> function from the <a href="../../lib/File/Path.html">File::Path</a> module.
Of course, if the template is not specified, the temporary directory
will be created in <code>tmpdir()</code> and will also be removed at program exit.</p>
</dd>
</li>
</dl>
<p>
</p>
<hr />
<h1><a name="mktemp_functions">MKTEMP FUNCTIONS</a></h1>
<p>The following functions are Perl implementations of the
<a href="#item_mktemp"><code>mktemp()</code></a> family of temp file generation system calls.</p>
<dl>
<dt><strong><a name="item_mkstemp"><strong>mkstemp</strong></a></strong>
<dd>
<p>Given a template, returns a filehandle to the temporary file and the name
of the file.</p>
</dd>
<dd>
<pre>
<span class="operator">(</span><span class="variable">$fh</span><span class="operator">,</span> <span class="variable">$name</span><span class="operator">)</span> <span class="operator">=</span> <span class="variable">mkstemp</span><span class="operator">(</span> <span class="variable">$template</span> <span class="operator">);</span>
</pre>
</dd>
<dd>
<p>In scalar context, just the filehandle is returned.</p>
</dd>
<dd>
<p>The template may be any filename with some number of X's appended
to it, for example <em>/tmp/temp.XXXX</em>. The trailing X's are replaced
with unique alphanumeric combinations.</p>
</dd>
</li>
<dt><strong><a name="item_mkstemps"><strong>mkstemps</strong></a></strong>
<dd>
<p>Similar to mkstemp(), except that an extra argument can be supplied
with a suffix to be appended to the template.</p>
</dd>
<dd>
<pre>
<span class="operator">(</span><span class="variable">$fh</span><span class="operator">,</span> <span class="variable">$name</span><span class="operator">)</span> <span class="operator">=</span> <span class="variable">mkstemps</span><span class="operator">(</span> <span class="variable">$template</span><span class="operator">,</span> <span class="variable">$suffix</span> <span class="operator">);</span>
</pre>
</dd>
<dd>
<p>For example a template of <code>testXXXXXX</code> and suffix of <code>.dat</code>
would generate a file similar to <em>testhGji_w.dat</em>.</p>
</dd>
<dd>
<p>Returns just the filehandle alone when called in scalar context.</p>
</dd>
</li>
<dt><strong><a name="item_mkdtemp"><strong>mkdtemp</strong></a></strong>
<dd>
<p>Create a directory from a template. The template must end in
X's that are replaced by the routine.</p>
</dd>
<dd>
<pre>
<span class="variable">$tmpdir_name</span> <span class="operator">=</span> <span class="variable">mkdtemp</span><span class="operator">(</span><span class="variable">$template</span><span class="operator">);</span>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?