find.html

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

HTML
467 行
字号
<em>wanted()</em> function are still tainted. If this option is used while
not in taint-mode, <a href="#item_untaint"><code>untaint</code></a> is a no-op.</p>
</dd>
</li>
<dt><strong><a name="item_untaint_pattern"><code>untaint_pattern</code></a></strong>

<dd>
<p>See above. This should be set using the <code>qr</code> quoting operator.
The default is set to  <code>qr|^([-+@\w./]+)$|</code>.
Note that the parentheses are vital.</p>
</dd>
</li>
<dt><strong><a name="item_untaint_skip"><code>untaint_skip</code></a></strong>

<dd>
<p>If set, a directory which fails the <em>untaint_pattern</em> is skipped,
including all its sub-directories. The default is to 'die' in such a case.</p>
</dd>
</li>
</dl>
<p>
</p>
<h2><a name="the_wanted_function">The wanted function</a></h2>
<p>The <a href="#item_wanted"><code>wanted()</code></a> function does whatever verifications you want on
each file and directory.  Note that despite its name, the <a href="#item_wanted"><code>wanted()</code></a>
function is a generic callback function, and does <strong>not</strong> tell
File::Find if a file is &quot;wanted&quot; or not.  In fact, its return value
is ignored.</p>
<p>The wanted function takes no arguments but rather does its work
through a collection of variables.</p>
<dl>
<dt><strong><a name="item__file__find__dir"><code>$File::Find::dir</code> is the current directory name,</a></strong>

<dt><strong><a name="item___"><code>$_</code> is the current filename within that directory</a></strong>

<dt><strong><a name="item__file__find__name"><code>$File::Find::name</code> is the complete pathname to the file.</a></strong>

</dl>
<p>Don't modify these variables.</p>
<p>For example, when examining the file <em>/some/path/foo.ext</em> you will have:</p>
<pre>
    $File::Find::dir  = /some/path/
    $_                = foo.ext
    $File::Find::name = /some/path/foo.ext</pre>
<p>You are chdir()'d to <a href="#item__file__find__dir"><code>$File::Find::dir</code></a> when the function is called,
unless <a href="#item_no_chdir"><code>no_chdir</code></a> was specified. Note that when changing to
directories is in effect the root directory (<em>/</em>) is a somewhat
special case inasmuch as the concatenation of <a href="#item__file__find__dir"><code>$File::Find::dir</code></a>,
<code>'/'</code> and <a href="#item___"><code>$_</code></a> is not literally equal to <a href="#item__file__find__name"><code>$File::Find::name</code></a>. The
table below summarizes all variants:</p>
<pre>
              $File::Find::name  $File::Find::dir  $_
 default      /                  /                 .
 no_chdir=&gt;0  /etc               /                 etc
              /etc/x             /etc              x</pre>
<pre>
 no_chdir=&gt;1  /                  /                 /
              /etc               /                 /etc
              /etc/x             /etc              /etc/x</pre>
<p>When &lt;follow&gt; or &lt;follow_fast&gt; are in effect, there is
also a <code>$File::Find::fullname</code>.  The function may set
<code>$File::Find::prune</code> to prune the tree unless <a href="#item_bydepth"><code>bydepth</code></a> was
specified.  Unless <a href="#item_follow"><code>follow</code></a> or <a href="#item_follow_fast"><code>follow_fast</code></a> is specified, for
compatibility reasons (find.pl, find2perl) there are in addition the
following globals available: <code>$File::Find::topdir</code>,
<code>$File::Find::topdev</code>, <code>$File::Find::topino</code>,
<code>$File::Find::topmode</code> and <code>$File::Find::topnlink</code>.</p>
<p>This library is useful for the <code>find2perl</code> tool, which when fed,</p>
<pre>
    <span class="variable">find2perl</span> <span class="operator">/</span> <span class="operator">-</span><span class="variable">name</span> <span class="operator">.</span><span class="variable">nfs</span><span class="operator">\*</span> <span class="operator">-</span><span class="variable">mtime</span> <span class="operator">+</span><span class="number">7</span> <span class="operator">\</span>
        <span class="operator">-</span><span class="keyword">exec</span> <span class="variable">rm</span> <span class="keyword">-f</span> <span class="operator">{}</span> <span class="operator">\;</span> <span class="keyword">-o</span> <span class="operator">-</span><span class="variable">fstype</span> <span class="variable">nfs</span> <span class="operator">-</span><span class="variable">prune</span>
</pre>
<p>produces something like:</p>
<pre>
    <span class="keyword">sub</span><span class="variable"> wanted </span><span class="operator">{</span>
        <span class="operator">/^\.</span><span class="variable">nfs</span><span class="operator">.*\</span><span class="variable">z</span><span class="operator">/</span><span class="regex">s &amp;&amp;
        (($dev, $ino, $mode, $nlink, $uid, $gid) = lstat($_)) &amp;</span><span class="operator">&amp;</span>
        <span class="keyword">int</span><span class="operator">(</span><span class="keyword">-M</span> <span class="variable">_</span><span class="operator">)</span> <span class="operator">&gt;</span> <span class="number">7</span> <span class="operator">&amp;&amp;</span>
        <span class="keyword">unlink</span><span class="operator">(</span><span class="variable">$_</span><span class="operator">)</span>
        <span class="operator">||</span>
        <span class="operator">(</span><span class="variable">$nlink</span> <span class="operator">||</span> <span class="operator">((</span><span class="variable">$dev</span><span class="operator">,</span> <span class="variable">$ino</span><span class="operator">,</span> <span class="variable">$mode</span><span class="operator">,</span> <span class="variable">$nlink</span><span class="operator">,</span> <span class="variable">$uid</span><span class="operator">,</span> <span class="variable">$gid</span><span class="operator">)</span> <span class="operator">=</span> <span class="keyword">lstat</span><span class="operator">(</span><span class="variable">$_</span><span class="operator">)))</span> <span class="operator">&amp;&amp;</span>
        <span class="variable">$dev</span> <span class="operator">&lt;</span> <span class="number">0</span> <span class="operator">&amp;&amp;</span>
        <span class="operator">(</span><span class="variable">$File::Find::prune</span> <span class="operator">=</span> <span class="number">1</span><span class="operator">);</span>
    <span class="operator">}</span>
</pre>
<p>Notice the <code>_</code> in the above <a href="../../lib/Pod/perlfunc.html#item_int"><code>int(-M _)</code></a>: the <code>_</code> is a magical
filehandle that caches the information from the preceding
<a href="../../lib/Pod/perlfunc.html#item_stat"><code>stat()</code></a>, <a href="../../lib/Pod/perlfunc.html#item_lstat"><code>lstat()</code></a>, or filetest.</p>
<p>Here's another interesting wanted function.  It will find all symbolic
links that don't resolve:</p>
<pre>
    <span class="keyword">sub</span><span class="variable"> wanted </span><span class="operator">{</span>
         <span class="keyword">-l</span> <span class="operator">&amp;&amp;</span> <span class="operator">!</span><span class="keyword">-e</span> <span class="operator">&amp;&amp;</span> <span class="keyword">print</span> <span class="string">"bogus link: $File::Find::name\n"</span><span class="operator">;</span>
    <span class="operator">}</span>
</pre>
<p>See also the script <code>pfind</code> on CPAN for a nice application of this
module.</p>
<p>
</p>
<hr />
<h1><a name="warnings">WARNINGS</a></h1>
<p>If you run your program with the <a href="../../lib/Pod/perlrun.html#item__2dw"><code>-w</code></a> switch, or if you use the
<code>warnings</code> pragma, File::Find will report warnings for several weird
situations. You can disable these warnings by putting the statement</p>
<pre>
    <span class="keyword">no</span> <span class="variable">warnings</span> <span class="string">'File::Find'</span><span class="operator">;</span>
</pre>
<p>in the appropriate scope. See <a href="../../lib/Pod/perllexwarn.html">the perllexwarn manpage</a> for more info about lexical
warnings.</p>
<p>
</p>
<hr />
<h1><a name="caveat">CAVEAT</a></h1>
<dl>
<dt><strong><a name="item__dont_use_nlink">$dont_use_nlink</a></strong>

<dd>
<p>You can set the variable <code>$File::Find::dont_use_nlink</code> to 1, if you want to
force File::Find to always stat directories. This was used for file systems
that do not have an <code>nlink</code> count matching the number of sub-directories.
Examples are ISO-9660 (CD-ROM), AFS, HPFS (OS/2 file system), FAT (DOS file
system) and a couple of others.</p>
</dd>
<dd>
<p>You shouldn't need to set this variable, since File::Find should now detect
such file systems on-the-fly and switch itself to using stat. This works even
for parts of your file system, like a mounted CD-ROM.</p>
</dd>
<dd>
<p>If you do set <code>$File::Find::dont_use_nlink</code> to 1, you will notice slow-downs.</p>
</dd>
</li>
<dt><strong><a name="item_symlinks">symlinks</a></strong>

<dd>
<p>Be aware that the option to follow symbolic links can be dangerous.
Depending on the structure of the directory tree (including symbolic
links to directories) you might traverse a given (physical) directory
more than once (only if <a href="#item_follow_fast"><code>follow_fast</code></a> is in effect).
Furthermore, deleting or changing files in a symbolically linked directory
might cause very unpleasant surprises, since you delete or change files
in an unknown directory.</p>
</dd>
</li>
</dl>
<p>
</p>
<hr />
<h1><a name="notes">NOTES</a></h1>
<ul>
<li>
<p>Mac OS (Classic) users should note a few differences:</p>
<ul>
<li>
<p>The path separator is ':', not '/', and the current directory is denoted
as ':', not '.'. You should be careful about specifying relative pathnames.
While a full path always begins with a volume name, a relative pathname
should always begin with a ':'.  If specifying a volume name only, a
trailing ':' is required.</p>
</li>
<li>
<p><a href="#item__file__find__dir"><code>$File::Find::dir</code></a> is guaranteed to end with a ':'. If <a href="#item___"><code>$_</code></a>
contains the name of a directory, that name may or may not end with a
':'. Likewise, <a href="#item__file__find__name"><code>$File::Find::name</code></a>, which contains the complete
pathname to that directory, and <code>$File::Find::fullname</code>, which holds
the absolute pathname of that directory with all symbolic links resolved,
may or may not end with a ':'.</p>
</li>
<li>
<p>The default <a href="#item_untaint_pattern"><code>untaint_pattern</code></a> (see above) on Mac OS is set to
<code>qr|^(.+)$|</code>. Note that the parentheses are vital.</p>
</li>
<li>
<p>The invisible system file &quot;Icon\015&quot; is ignored. While this file may
appear in every directory, there are some more invisible system files
on every volume, which are all located at the volume root level (i.e.
&quot;MacintoshHD:&quot;). These system files are <strong>not</strong> excluded automatically.
Your filter may use the following code to recognize invisible files or
directories (requires Mac::Files):</p>
<pre>
 <span class="keyword">use</span> <span class="variable">Mac::Files</span><span class="operator">;</span>
</pre>
<pre>
 <span class="comment"># invisible() --  returns 1 if file/directory is invisible,</span>
 <span class="comment"># 0 if it's visible or undef if an error occurred</span>
</pre>
<pre>
 <span class="keyword">sub</span><span class="variable"> invisible</span><span class="operator">(</span>$<span class="operator">)</span> <span class="operator">{</span>
   <span class="keyword">my</span> <span class="variable">$file</span> <span class="operator">=</span> <span class="keyword">shift</span><span class="operator">;</span>
   <span class="keyword">my</span> <span class="operator">(</span><span class="variable">$fileCat</span><span class="operator">,</span> <span class="variable">$fileInfo</span><span class="operator">);</span>
   <span class="keyword">my</span> <span class="variable">$invisible_flag</span> <span class="operator">=</span>  <span class="number">1</span> <span class="operator">&lt;&lt;</span> <span class="number">14</span><span class="operator">;</span>
</pre>
<pre>
   <span class="keyword">if</span> <span class="operator">(</span> <span class="variable">$fileCat</span> <span class="operator">=</span> <span class="variable">FSpGetCatInfo</span><span class="operator">(</span><span class="variable">$file</span><span class="operator">)</span> <span class="operator">)</span> <span class="operator">{</span>
     <span class="keyword">if</span> <span class="operator">(</span><span class="variable">$fileInfo</span> <span class="operator">=</span> <span class="variable">$fileCat</span><span class="operator">-&gt;</span><span class="variable">ioFlFndrInfo</span><span class="operator">()</span> <span class="operator">)</span> <span class="operator">{</span>
       <span class="keyword">return</span> <span class="operator">((</span><span class="variable">$fileInfo</span><span class="operator">-&gt;</span><span class="variable">fdFlags</span> <span class="operator">&amp;</span> <span class="variable">$invisible_flag</span><span class="operator">)</span> <span class="operator">&amp;&amp;</span> <span class="number">1</span><span class="operator">);</span>
     <span class="operator">}</span>
   <span class="operator">}</span>
   <span class="keyword">return</span> <span class="keyword">undef</span><span class="operator">;</span>
    <span class="operator">}</span>
</pre>
<p>Generally, invisible files are system files, unless an odd application
decides to use invisible files for its own purposes. To distinguish
such files from system files, you have to look at the <strong>type</strong> and <strong>creator</strong>
file attributes. The MacPerl built-in functions <code>GetFileInfo(FILE)</code> and
<code>SetFileInfo(CREATOR, TYPE, FILES)</code> offer access to these attributes
(see MacPerl.pm for details).</p>
<p>Files that appear on the desktop actually reside in an (hidden) directory
named &quot;Desktop Folder&quot; on the particular disk volume. Note that, although
all desktop files appear to be on the same &quot;virtual&quot; desktop, each disk
volume actually maintains its own &quot;Desktop Folder&quot; directory.</p>
</li>
</ul>
</ul>
<p>
</p>
<hr />
<h1><a name="bugs_and_caveats">BUGS AND CAVEATS</a></h1>
<p>Despite the name of the <a href="#item_finddepth"><code>finddepth()</code></a> function, both <a href="#item_find"><code>find()</code></a> and
<a href="#item_finddepth"><code>finddepth()</code></a> perform a depth-first search of the directory
hierarchy.</p>
<p>
</p>
<hr />
<h1><a name="history">HISTORY</a></h1>
<p>File::Find used to produce incorrect results if called recursively.
During the development of perl 5.8 this bug was fixed.
The first fixed version of File::Find was 1.01.</p>

</body>

</html>

⌨️ 快捷键说明

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