ch11_06.htm

来自「by Randal L. Schwartz and Tom Phoenix I」· HTM 代码 · 共 924 行 · 第 1/3 页

HTM
924
字号
<html><head><title>File Tests (Learning Perl, 3rd Edition)</title><link rel="stylesheet" type="text/css" href="../style/style1.css" /><meta name="DC.Creator" content="Randal L. Schwartz and Tom Phoenix" /><meta name="DC.Format" content="text/xml" scheme="MIME" /><meta name="DC.Language" content="en-US" /><meta name="DC.Publisher" content="O'Reilly &amp; Associates, Inc." /><meta name="DC.Source" scheme="ISBN" content="0596001320L" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="Learning Perl, 3rd Edition" /><meta name="DC.Type" content="Text.Monograph" /></head><body bgcolor="#ffffff"><img alt="Book Home" border="0" src="gifs/smbanner.gif" usemap="#banner-map" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Learning Perl, 3rd Edition" /><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch11_05.htm"><img alt="Previous" border="0" src="../gifs/txtpreva.gif" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"></a></td><td align="right" valign="top" width="228"><a href="ch11_07.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div><h2 class="sect1">11.6. File Tests</h2><p><a name="INDEX-798" />Now <a name="INDEX-799" />you know how to open a filehandle foroutput. Normally, that will create a new file, wiping out anyexisting file with the same name. Perhaps you want to check thatthere isn't a file by that name. Perhaps you need to know howold a given file is. Or perhaps you want to go through a list offiles to find which ones are larger than a certain number of bytesand not accessed for a certain amount of time. Perl has a completeset of tests you can use to find out information about files.</p><p>Let's try that first example, where we need to check that agiven file doesn't exist, so that we don't accidentallyoverwrite a vital spreadsheet data file, or that important birthdaycalendar. For this, we need the <tt class="literal">-e</tt> file test,testing for existence:</p><blockquote><pre class="code">die "Oops! A file called '$filename' already exists.\n"  if -e $filename;</pre></blockquote><p>Notice that we don't include <tt class="literal">$!</tt> in this<tt class="literal">die</tt> message, since we're not reportingthat the system refused a request in this case. Here's anexample of checking whether a file is being kept up-to-date.Let's say that our program's configuration file should beupdated every week or two. (Maybe it's checking for computerviruses, say.) If the file hasn't been modified in the past 28days, then something is wrong:</p><blockquote><pre class="code">warn "Config file is looking pretty old!\n"  if -M CONFIG &gt; 28;</pre></blockquote><p>The third example is more complex. Here, let's say that diskspace is filling up and rather than buy more disks, we'vedecided to move any large, useless files to the backup tapes. Solet's go through our list of files<a href="#FOOTNOTE-264">[264]</a> to see which of them are larger than 100 K. But even if afile is large, we shouldn't move it to the backup tapes unlessit hasn't been accessed in the last 90 days (so we know thatit's not used too often):<a href="#FOOTNOTE-265">[265]</a></p><blockquote class="footnote"> <a name="FOOTNOTE-264" /><p>[264]It'smore likely that, instead of having the list of files in an array, asour example shows, you'll read it directly from the filesystemusing a glob or directory handle, as shown in <a href="ch12_01.htm">Chapter 12, "Directory Operations"</a>. Since we haven't seen that yet,we'll just start with the list and go from there.</p></blockquote><blockquote class="footnote"> <a name="FOOTNOTE-265" /><p>[265]There's a wayto make this example more efficient, as we'll see by the end ofthe chapter.</p> </blockquote><blockquote><pre class="code">my @original_files = qw/ fred barney betty wilma pebbles dino bamm-bamm /;my @big_old_files;  # The ones we want to put on backup tapesforeach my $filename (@original_files) {  push @big_old_files, $_    if -s $filename &gt; 100_000 and -A $filename &gt; 90;}</pre></blockquote><p>This is the first time that we've seen it, so maybe you noticedthat the control variable of the <tt class="literal">foreach</tt> loop is a<tt class="literal">my</tt> variable. That declares it to have the scope ofthe loop itself, so this example should work under <tt class="literal">usestrict</tt>. Without the <tt class="literal">my</tt> keyword, thiswould be using the global <tt class="literal">$filename</tt>.</p><p>The file tests all look like a hyphen and a letter, which is the nameof the test, followed by either a filename or a filehandle to betested. Many of them return a true/false value, but several givesomething more interesting. See <a href="ch11_06.htm#lperl3-CHP-11-TABLE-1">Table 11-1</a> for thecomplete list, and then read the following discussion to learn moreabout the special cases.</p><a name="lperl3-CHP-11-TABLE-1" /><h4 class="objtitle">Table 11-1. File tests and their meanings </h4><table border="1"><tr><th><p>File test</p></th><th><p>Meaning</p></th></tr><tr><td><blockquote><pre class="code">-r</pre></blockquote></td><td><p><a name="INDEX-800" />File ordirectory is readable by this (effective) user or group</p></td></tr><tr><td><blockquote><pre class="code">-w</pre></blockquote></td><td><p><a name="INDEX-801" />File ordirectory is writable by this (effective) user or group</p></td></tr><tr><td><blockquote><pre class="code">-x</pre></blockquote></td><td><p>File or directory is executable by this (effective) user or group</p></td></tr><tr><td><blockquote><pre class="code">-o</pre></blockquote></td><td><p>File or directory is owned by this (effective) user</p></td></tr><tr><td><blockquote><pre class="code">-R</pre></blockquote></td><td><p>File or directory is readable by this real user or group</p></td></tr><tr><td><blockquote><pre class="code">-W</pre></blockquote></td><td><p>File or directory is writable by this real user or group</p></td></tr><tr><td><blockquote><pre class="code">-X</pre></blockquote></td><td><p>File or directory is executable by this real user or group</p></td></tr><tr><td><blockquote><pre class="code">-O</pre></blockquote></td><td><p>File or directory is owned by this real user</p></td></tr><tr><td><blockquote><pre class="code">-e</pre></blockquote></td><td><p>File or directory name exists</p></td></tr><tr><td><blockquote><pre class="code">-z</pre></blockquote></td><td><p>File exists and has zero size (always false for directories)</p></td></tr><tr><td><blockquote><pre class="code">-s</pre></blockquote></td><td><p>File or directory exists and has nonzero size (the value is the sizein bytes)</p></td></tr><tr><td><blockquote><pre class="code">-f</pre></blockquote></td><td><p>Entry is a plain file</p></td></tr><tr><td><blockquote><pre class="code">-d</pre></blockquote></td><td><p>Entry is a directory</p></td></tr><tr><td><blockquote><pre class="code">-l</pre></blockquote></td><td><p>Entry is a symbolic link</p></td></tr><tr><td><blockquote><pre class="code">-S</pre></blockquote></td><td><p>Entry is a socket</p></td></tr><tr><td><blockquote><pre class="code">-p</pre></blockquote></td><td><p>Entry is a named pipe (a "fifo")</p></td></tr><tr><td><blockquote><pre class="code">-b</pre></blockquote></td><td><p>Entry is a block-special file (like a mountable disk)</p></td></tr><tr><td><blockquote><pre class="code">-c</pre></blockquote></td><td><p>Entry is a character-special file (like an I/O device)</p></td></tr><tr><td><blockquote><pre class="code">-u</pre></blockquote></td><td><p>File or directory is setuid</p></td></tr><tr><td><blockquote><pre class="code">-g</pre></blockquote></td><td><p>File or directory is setgid</p></td></tr><tr><td><blockquote><pre class="code">-k</pre></blockquote></td><td><p>File or directory has the sticky bit set</p></td></tr><tr><td><blockquote><pre class="code">-t</pre></blockquote></td><td><p>The filehandle is a TTY (as reported by the <tt class="literal">isatty()</tt> system function; filenames can't be tested by thistest)</p></td></tr><tr><td><blockquote><pre class="code">-T</pre></blockquote></td><td><p>File looks like a "text" file</p></td></tr><tr><td><blockquote><pre class="code">-B</pre></blockquote></td><td><p>File looks like a "binary" file</p></td></tr><tr><td><blockquote><pre class="code">-M</pre></blockquote></td><td><p>Modification age (measured in days)</p></td>

⌨️ 快捷键说明

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