📄 ch03_10.htm
字号:
otherwise documented, it returns <tt class="literal">1</tt> for true and<tt class="literal">""</tt> for false, or the undefined value if the filedoesn't exist or is otherwise inaccessible. Currently implemented filetest operators are listed in <a href="ch03_10.htm#perl3-tab-filetest">Table 3-4</a>.</p><a name="perl3-tab-filetest"></a><h4 class="objtitle">Table 3.4. File Test Operators</h4><table border="1"><tr><th>Operator</th><th>Meaning</th></tr><tr><td><tt class="literal">-r</tt></td><td>File is readable by effective UID/GID.</td></tr><tr><td><tt class="literal">-w</tt></td><td>File is writable by effective UID/GID.</td></tr><tr><td><tt class="literal">-x</tt></td><td>File is executable by effective UID/GID.</td></tr><tr><td><tt class="literal">-o</tt></td><td>File is owned by effective UID.</td></tr><tr><td><tt class="literal">-R</tt></td><td>File is readable by real UID/GID.</td></tr><tr><td><tt class="literal">-W</tt></td><td>File is writable by real UID/GID.</td></tr><tr><td><tt class="literal">-X</tt></td><td>File is executable by real UID/GID.</td></tr><tr><td><tt class="literal">-O</tt></td><td>File is owned by real UID.</td></tr><tr><td><tt class="literal">-e</tt></td><td>File exists.</td></tr><tr><td><tt class="literal">-z</tt></td><td>File has zero size.</td></tr><tr><td><tt class="literal">-s</tt></td><td>File has nonzero size (returns size).</td></tr><tr><td><tt class="literal">-f</tt></td><td>File is a plain file.</td></tr><tr><td><tt class="literal">-d</tt></td><td>File is a directory.</td></tr><tr><td><tt class="literal">-l</tt></td><td>File is a symbolic link.</td></tr><tr><td><tt class="literal">-p</tt></td><td>File is a named pipe (FIFO).</td></tr><tr><td><tt class="literal">-S</tt></td><td>File is a socket.</td></tr><tr><td><tt class="literal">-b</tt></td><td>File is a block special file.</td></tr><tr><td><tt class="literal">-c</tt></td><td>File is a character special file.</td></tr><tr><td><tt class="literal">-t</tt></td><td>Filehandle is opened to a tty.</td></tr><tr><td><tt class="literal">-u</tt></td><td>File has setuid bit set.</td></tr><tr><td><tt class="literal">-g</tt></td><td>File has setgid bit set.</td></tr><tr><td><tt class="literal">-k</tt></td><td>File has sticky bit set.</td></tr><tr><td><tt class="literal">-T</tt></td><td>File is a text file.</td></tr><tr><td><tt class="literal">-B</tt></td><td>File is a binary file (opposite of <tt class="literal">-T</tt>).</td></tr><tr><td><tt class="literal">-M</tt></td><td>Age of file (at startup) in days since modification.</td></tr><tr><td><tt class="literal">-A</tt></td><td>Age of file (at startup) in days since last access.</td></tr><tr><td><tt class="literal">-C</tt></td><td>Age of file (at startup) in days since inode change.</td></tr></table><p>Note that <tt class="literal">-s/a/b/</tt> does not do a negated substitution. Saying<tt class="literal">-exp($foo)</tt> still works as expected, however--only single lettersfollowing a minus are interpreted as file tests.</p><p><a name="INDEX-915"></a><a name="INDEX-916"></a>The interpretation of the file permission operators<tt class="literal">-r</tt>, <tt class="literal">-R</tt>, <tt class="literal">-w</tt>,<tt class="literal">-W</tt>, <tt class="literal">-x</tt>, and<tt class="literal">-X</tt> is based solely on the mode of the file and theuser and group IDs of the user. There may be other reasons you can'tactually read, write, or execute the file, such as Andrew File System(AFS) access control lists.<a href="#FOOTNOTE-3">[3]</a>Also notethat for the superuser, <tt class="literal">-r</tt>, <tt class="literal">-R</tt>,<tt class="literal">-w</tt>, and <tt class="literal">-W</tt> always return 1, and<tt class="literal">-x</tt> and <tt class="literal">-X</tt> return 1 if anyexecute bit is set in the mode. Thus, scripts run by the superusermay need to do a <tt class="literal">stat</tt> in order to determine theactual mode of the file or temporarily set the UID to something else.</p><blockquote class="footnote"><a name="FOOTNOTE-3"></a><p>[3]You may, however, overridethe built-in semantics with the <tt class="literal">use filetest</tt>pragma. See <a href="ch31_01.htm">Chapter 31, "Pragmatic Modules"</a>.</p></blockquote><p>The other file test operators don't care who you are. Anybody can usethe test for "regular" files:<blockquote><pre class="programlisting">while (<>) { chomp; next unless -f $_; # ignore "special" files ...}</pre></blockquote><a name="INDEX-917"></a><a name="INDEX-918"></a></p><p>The <tt class="literal">-T</tt> and <tt class="literal">-B</tt> switches work asfollows. The first block or so of the file is examined for strangecharacters such as control codes or bytes with the high bit set (thatdon't look like UTF-8). If more than a third of the bytes appear tobe strange, it's a binary file; otherwise, it's a text file. Also,any file containing ASCII NUL (<tt class="literal">\0</tt>) in the firstblock is considered a binary file. If <tt class="literal">-T</tt> or<tt class="literal">-B</tt> is used on a filehandle, the current input(standard I/O or "stdio") buffer is examined rather than the firstblock of the file. Both <tt class="literal">-T</tt> and<tt class="literal">-B</tt> return true on an empty file, or on a file atEOF (end-of-file) when testing a filehandle. Because Perl has to reada file to do the <tt class="literal">-T</tt> test, you don't want to use<tt class="literal">-T</tt> on special files that might hang or give youother kinds of grief. So on most occasions you'll want to test with a<tt class="literal">-f</tt> first, as in:<blockquote><pre class="programlisting">next unless -f $file && -T $file;</pre></blockquote>If any of the file tests (or either the <tt class="literal">stat</tt> or <tt class="literal">lstat</tt> operator)are given the special filehandle consisting of a solitary underline,then the <em class="emphasis">stat</em> structure of the previous file test (or <tt class="literal">stat</tt>operator) is used, thereby saving a system call. (This doesn't workwith <tt class="literal">-t</tt>, and you need to remember that <tt class="literal">lstat</tt> and <tt class="literal">-l</tt> will leavevalues in the <em class="emphasis">stat</em> structure for the symbolic link, not the realfile. Likewise, <tt class="literal">-l _</tt> will always be false after a normal<tt class="literal">stat</tt>.)</p><p>Here are a couple of examples:<blockquote><pre class="programlisting">print "Can do.\n" if -r $a || -w _ || -x _;stat($filename);print "Readable\n" if -r _;print "Writable\n" if -w _;print "Executable\n" if -x _;print "Setuid\n" if -u _;print "Setgid\n" if -g _;print "Sticky\n" if -k _;print "Text\n" if -T _;print "Binary\n" if -B _;</pre></blockquote><a name="INDEX-919"></a><a name="INDEX-920"></a><a name="INDEX-921"></a><a name="INDEX-922"></a><a name="INDEX-923"></a><a name="INDEX-924"></a><a name="INDEX-925"></a><a name="INDEX-926"></a></p><p>File ages for <tt class="literal">-M</tt>, <tt class="literal">-A</tt>, and<tt class="literal">-C</tt> are returned in days (including fractional days)since the script started running. This time is stored in the specialvariable <tt class="literal">$^T</tt> (<tt class="literal">$BASETIME</tt>). Thus,if the file changed after the script started, you would get a negativetime. Note that most time values (86,399 out of 86,400, on average)are fractional, so testing for equality with an integer without usingthe <tt class="literal">int</tt> function is usually futile. Examples:<blockquote><pre class="programlisting">next unless -M $file > .5; # files are older than 12 hours&newfile if -M $file < 0; # file is newer than process&mailwarning if int(-A) == 90; # file ($_) was accessed 90 days ago today</pre></blockquote>To reset the script's start time to the current time, say this:<blockquote><pre class="programlisting">$^T = time;</pre></blockquote></p><a name="INDEX-927"></a><!-- BOTTOM NAV BAR --><hr width="515" align="left"><div class="navbar"><table width="515" border="0"><tr><td align="left" valign="top" width="172"><a href="ch03_09.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0"></a></td><td align="right" valign="top" width="172"><a href="ch03_11.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr><tr><td align="left" valign="top" width="172">3.9. Shift Operators</td><td align="center" valign="top" width="171"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0"></a></td><td align="right" valign="top" width="172">3.11. Relational Operators</td></tr></table></div><hr width="515" align="left"><!-- LIBRARY NAV BAR --><img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p><font size="-1"><a href="copyrght.htm">Copyright © 2001</a> O'Reilly & Associates. All rights reserved.</font></p><map name="library-map"> <area shape="rect" coords="2,-1,79,99" href="../index.htm"><area shape="rect" coords="84,1,157,108" href="../perlnut/index.htm"><area shape="rect" coords="162,2,248,125" href="../prog/index.htm"><area shape="rect" coords="253,2,326,130" href="../advprog/index.htm"><area shape="rect" coords="332,1,407,112" href="../cookbook/index.htm"><area shape="rect" coords="414,2,523,103" href="../sysadmin/index.htm"></map><!-- END OF BODY --></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -