find.html

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

HTML
467
字号
<?xml version="1.0" ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<!-- saved from url=(0017)http://localhost/ -->
<script language="JavaScript" src="../../displayToc.js"></script>
<script language="JavaScript" src="../../tocParas.js"></script>
<script language="JavaScript" src="../../tocTab.js"></script>
<link rel="stylesheet" type="text/css" href="../../scineplex.css">
<title>File::Find - Traverse a directory tree.</title>
<link rel="stylesheet" href="../../Active.css" type="text/css" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rev="made" href="mailto:" />
</head>

<body>

<script>writelinks('__top__',2);</script>
<h1><a>File::Find - Traverse a directory tree.</a></h1>
<p><a name="__index__"></a></p>

<!-- INDEX BEGIN -->

<ul>

	<li><a href="#name">NAME</a></li>
	<li><a href="#synopsis">SYNOPSIS</a></li>
	<li><a href="#description">DESCRIPTION</a></li>
	<ul>

		<li><a href="#_options">%options</a></li>
		<li><a href="#the_wanted_function">The wanted function</a></li>
	</ul>

	<li><a href="#warnings">WARNINGS</a></li>
	<li><a href="#caveat">CAVEAT</a></li>
	<li><a href="#notes">NOTES</a></li>
	<li><a href="#bugs_and_caveats">BUGS AND CAVEATS</a></li>
	<li><a href="#history">HISTORY</a></li>
</ul>
<!-- INDEX END -->

<hr />
<p>
</p>
<h1><a name="name">NAME</a></h1>
<p>File::Find - Traverse a directory tree.</p>
<p>
</p>
<hr />
<h1><a name="synopsis">SYNOPSIS</a></h1>
<pre>
    <span class="keyword">use</span> <span class="variable">File::Find</span><span class="operator">;</span>
    <span class="variable">find</span><span class="operator">(\&amp;</span><span class="variable">wanted</span><span class="operator">,</span> <span class="variable">@directories_to_search</span><span class="operator">);</span>
    <span class="keyword">sub</span><span class="variable"> wanted </span><span class="operator">{</span> <span class="operator">...</span> <span class="operator">}</span>
</pre>
<pre>
    <span class="keyword">use</span> <span class="variable">File::Find</span><span class="operator">;</span>
    <span class="variable">finddepth</span><span class="operator">(\&amp;</span><span class="variable">wanted</span><span class="operator">,</span> <span class="variable">@directories_to_search</span><span class="operator">);</span>
    <span class="keyword">sub</span><span class="variable"> wanted </span><span class="operator">{</span> <span class="operator">...</span> <span class="operator">}</span>
</pre>
<pre>
    <span class="keyword">use</span> <span class="variable">File::Find</span><span class="operator">;</span>
    <span class="variable">find</span><span class="operator">(</span><span class="operator">{</span> <span class="string">wanted</span> <span class="operator">=&gt;</span> <span class="operator">\&amp;</span><span class="variable">process</span><span class="operator">,</span> <span class="string">follow</span> <span class="operator">=&gt;</span> <span class="number">1</span> <span class="operator">}</span><span class="operator">,</span> <span class="string">'.'</span><span class="operator">);</span>
</pre>
<p>
</p>
<hr />
<h1><a name="description">DESCRIPTION</a></h1>
<p>These are functions for searching through directory trees doing work
on each file found similar to the Unix <em>find</em> command.  File::Find
exports two functions, <a href="#item_find"><code>find</code></a> and <a href="#item_finddepth"><code>finddepth</code></a>.  They work similarly
but have subtle differences.</p>
<dl>
<dt><strong><a name="item_find"><strong>find</strong></a></strong>

<dd>
<pre>
  <span class="variable">find</span><span class="operator">(\&amp;</span><span class="variable">wanted</span><span class="operator">,</span>  <span class="variable">@directories</span><span class="operator">);</span>
  <span class="variable">find</span><span class="operator">(\</span><span class="variable">%options</span><span class="operator">,</span> <span class="variable">@directories</span><span class="operator">);</span>
</pre>
</dd>
<dd>
<p><a href="#item_find"><code>find()</code></a> does a depth-first search over the given <code>@directories</code> in
the order they are given.  For each file or directory found, it calls
the <code>&amp;wanted</code> subroutine.  (See below for details on how to use the
<code>&amp;wanted</code> function).  Additionally, for each directory found, it will
<a href="../../lib/Pod/perlfunc.html#item_chdir"><code>chdir()</code></a> into that directory and continue the search, invoking the
<code>&amp;wanted</code> function on each file or subdirectory in the directory.</p>
</dd>
<dt><strong><a name="item_finddepth"><strong>finddepth</strong></a></strong>

<dd>
<pre>
  <span class="variable">finddepth</span><span class="operator">(\&amp;</span><span class="variable">wanted</span><span class="operator">,</span>  <span class="variable">@directories</span><span class="operator">);</span>
  <span class="variable">finddepth</span><span class="operator">(\</span><span class="variable">%options</span><span class="operator">,</span> <span class="variable">@directories</span><span class="operator">);</span>
</pre>
</dd>
<dd>
<p><a href="#item_finddepth"><code>finddepth()</code></a> works just like <a href="#item_find"><code>find()</code></a> except that is invokes the
<code>&amp;wanted</code> function for a directory <em>after</em> invoking it for the
directory's contents.  It does a postorder traversal instead of a
preorder traversal, working from the bottom of the directory tree up
where <a href="#item_find"><code>find()</code></a> works from the top of the tree down.</p>
</dd>
</dl>
<p>
</p>
<h2><a name="_options">%options</a></h2>
<p>The first argument to <a href="#item_find"><code>find()</code></a> is either a code reference to your
<code>&amp;wanted</code> function, or a hash reference describing the operations
to be performed for each file.  The
code reference is described in <a href="#the_wanted_function">The wanted function</a> below.</p>
<p>Here are the possible keys for the hash:</p>
<dl>
<dt><strong><a name="item_wanted"><code>wanted</code></a></strong>

<dd>
<p>The value should be a code reference.  This code reference is
described in <a href="#the_wanted_function">The wanted function</a> below.</p>
</dd>
</li>
<dt><strong><a name="item_bydepth"><code>bydepth</code></a></strong>

<dd>
<p>Reports the name of a directory only AFTER all its entries
have been reported.  Entry point <a href="#item_finddepth"><code>finddepth()</code></a> is a shortcut for
specifying <code>&lt;{ bydepth =</code> 1 }&gt;&gt; in the first argument of <a href="#item_find"><code>find()</code></a>.</p>
</dd>
</li>
<dt><strong><a name="item_preprocess"><code>preprocess</code></a></strong>

<dd>
<p>The value should be a code reference. This code reference is used to
preprocess the current directory. The name of the currently processed
directory is in <a href="#item__file__find__dir"><code>$File::Find::dir</code></a>. Your preprocessing function is
called after <a href="../../lib/Pod/perlfunc.html#item_readdir"><code>readdir()</code></a>, but before the loop that calls the <a href="#item_wanted"><code>wanted()</code></a>
function. It is called with a list of strings (actually file/directory
names) and is expected to return a list of strings. The code can be
used to sort the file/directory names alphabetically, numerically,
or to filter out directory entries based on their name alone. When
<em>follow</em> or <em>follow_fast</em> are in effect, <a href="#item_preprocess"><code>preprocess</code></a> is a no-op.</p>
</dd>
</li>
<dt><strong><a name="item_postprocess"><code>postprocess</code></a></strong>

<dd>
<p>The value should be a code reference. It is invoked just before leaving
the currently processed directory. It is called in void context with no
arguments. The name of the current directory is in <a href="#item__file__find__dir"><code>$File::Find::dir</code></a>. This
hook is handy for summarizing a directory, such as calculating its disk
usage. When <em>follow</em> or <em>follow_fast</em> are in effect, <a href="#item_postprocess"><code>postprocess</code></a> is a
no-op.</p>
</dd>
</li>
<dt><strong><a name="item_follow"><code>follow</code></a></strong>

<dd>
<p>Causes symbolic links to be followed. Since directory trees with symbolic
links (followed) may contain files more than once and may even have
cycles, a hash has to be built up with an entry for each file.
This might be expensive both in space and time for a large
directory tree. See <em>follow_fast</em> and <em>follow_skip</em> below.
If either <em>follow</em> or <em>follow_fast</em> is in effect:</p>
</dd>
<ul>
<li>
<p>It is guaranteed that an <em>lstat</em> has been called before the user's
<a href="#item_wanted"><code>wanted()</code></a> function is called. This enables fast file checks involving _.
Note that this guarantee no longer holds if <em>follow</em> or <em>follow_fast</em>
are not set.</p>
</li>
<li>
<p>There is a variable <code>$File::Find::fullname</code> which holds the absolute
pathname of the file with all symbolic links resolved.  If the link is
a dangling symbolic link, then fullname will be set to <a href="../../lib/Pod/perlfunc.html#item_undef"><code>undef</code></a>.</p>
</li>
</ul>
<p>This is a no-op on Win32.</p>
<dt><strong><a name="item_follow_fast"><code>follow_fast</code></a></strong>

<dd>
<p>This is similar to <em>follow</em> except that it may report some files more
than once.  It does detect cycles, however.  Since only symbolic links
have to be hashed, this is much cheaper both in space and time.  If
processing a file more than once (by the user's <a href="#item_wanted"><code>wanted()</code></a> function)
is worse than just taking time, the option <em>follow</em> should be used.</p>
</dd>
<dd>
<p>This is also a no-op on Win32.</p>
</dd>
</li>
<dt><strong><a name="item_follow_skip"><code>follow_skip</code></a></strong>

<dd>
<p><code>follow_skip==1</code>, which is the default, causes all files which are
neither directories nor symbolic links to be ignored if they are about
to be processed a second time. If a directory or a symbolic link
are about to be processed a second time, File::Find dies.</p>
</dd>
<dd>
<p><code>follow_skip==0</code> causes File::Find to die if any file is about to be
processed a second time.</p>
</dd>
<dd>
<p><code>follow_skip==2</code> causes File::Find to ignore any duplicate files and
directories but to proceed normally otherwise.</p>
</dd>
</li>
<dt><strong><a name="item_dangling_symlinks"><code>dangling_symlinks</code></a></strong>

<dd>
<p>If true and a code reference, will be called with the symbolic link
name and the directory it lives in as arguments.  Otherwise, if true
and warnings are on, warning &quot;symbolic_link_name is a dangling
symbolic link\n&quot; will be issued.  If false, the dangling symbolic link
will be silently ignored.</p>
</dd>
</li>
<dt><strong><a name="item_no_chdir"><code>no_chdir</code></a></strong>

<dd>
<p>Does not <a href="../../lib/Pod/perlfunc.html#item_chdir"><code>chdir()</code></a> to each directory as it recurses. The <a href="#item_wanted"><code>wanted()</code></a>
function will need to be aware of this, of course. In this case,
<a href="#item___"><code>$_</code></a> will be the same as <a href="#item__file__find__name"><code>$File::Find::name</code></a>.</p>
</dd>
</li>
<dt><strong><a name="item_untaint"><code>untaint</code></a></strong>

<dd>
<p>If find is used in taint-mode (-T command line switch or if EUID != UID
or if EGID != GID) then internally directory names have to be untainted
before they can be chdir'ed to. Therefore they are checked against a regular
expression <em>untaint_pattern</em>.  Note that all names passed to the user's

⌨️ 快捷键说明

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