⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch9.htm

📁 this is a book on pearl , simple example with explanation is given here. it could be beneficial for
💻 HTM
📖 第 1 页 / 共 5 页
字号:
    print();}</PRE></BLOCKQUOTE><HR><P>This program displays:<BR><BLOCKQUOTE><PRE>while (&lt;STDIN&gt;) {    print();}while (&lt;&gt;) {    print();}</PRE></BLOCKQUOTE><P>Next, we will take a look at the ways that Perl lets you testfiles, and following that, the fuNCtions that can be used withfiles.<H2><A NAME="FileTestOperators"><FONT SIZE=5 COLOR=#FF0000>File Test Operators</FONT></A></H2><P>Perl has many operators that you can use to test different aspectsof a file. For example, you can use the <TT>-e</TT>operator to ensure that a file exists before deleting it. Or,you can check that a file can be written to before appending toit. By checking the feasibility of the impending file operation,you can reduce the number of errors that your program will eNCounter.Table 9.2 shows a complete list of the operators used to testfiles.<BR><P><CENTER><B>Table 9.2&nbsp;&nbsp;Perl's File Test Operators</B></CENTER><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD WIDTH=121><I>Operator</I></TD><TD WIDTH=469><I>Description</I></TD></TR><TR><TD WIDTH=121><TT>-A</TT> <TT>OPERAND</TT></TD><TD WIDTH=469>Returns the access age of <TT>OPERAND</TT> when the program started.</TD></TR><TR><TD WIDTH=121><TT>-b</TT> <TT>OPERAND</TT></TD><TD WIDTH=469>Tests if <TT>OPERAND</TT> is a block device.</TD></TR><TR><TD WIDTH=121><TT>-B</TT> <TT>OPERAND</TT></TD><TD WIDTH=469>Tests if <TT>OPERAND</TT> is a binary file. If <TT>OPERAND</TT> is a file handle, then the current buffer is examined, instead of the file itself.</TD></TR><TR><TD WIDTH=121><TT>-c OPERAND</TT></TD><TD WIDTH=469>Tests if <TT>OPERAND</TT> is a character device.</TD></TR><TR><TD WIDTH=121><TT>-C</TT> <TT>OPERAND</TT></TD><TD WIDTH=469>Returns the inode change age of <TT>OPERAND</TT> when the program started.</TD></TR><TR><TD WIDTH=121><TT>-d</TT> <TT>OPERAND</TT></TD><TD WIDTH=469>Tests if <TT>OPERAND</TT> is a directory.</TD></TR><TR><TD WIDTH=121><TT>-e</TT> <TT>OPERAND</TT></TD><TD WIDTH=469>Tests if <TT>OPERAND</TT> exists.</TD></TR><TR><TD WIDTH=121><TT>-f</TT> <TT>OPERAND</TT></TD><TD WIDTH=469>Tests if <TT>OPERAND</TT> is a regular  file as opposed to a directory, symbolic link or other type of file.</TD></TR><TR><TD WIDTH=121><TT>-g</TT> <TT>OPERAND</TT></TD><TD WIDTH=469>Tests if <TT>OPERAND</TT> has the setgid bit set.</TD></TR><TR><TD WIDTH=121><TT>-k</TT> <TT>OPERAND</TT></TD><TD WIDTH=469>Tests if <TT>OPERAND</TT> has the sticky bit set.</TD></TR><TR><TD WIDTH=121><TT>-l</TT> <TT>OPERAND</TT></TD><TD WIDTH=469>Tests if <TT>OPERAND</TT> is a symbolic link. Under DOS, this operator always will return false.</TD></TR><TR><TD WIDTH=121><TT>-M</TT> <TT>OPERAND</TT></TD><TD WIDTH=469>Returns the age of <TT>OPERAND</TT> in days when the program started.</TD></TR><TR><TD WIDTH=121><TT>-o</TT> <TT>OPERAND</TT></TD><TD WIDTH=469>Tests if <TT>OPERAND</TT> is owned by the effective uid. Under DOS, it always returns true.</TD></TR><TR><TD WIDTH=121><TT>-O</TT> <TT>OPERAND</TT></TD><TD WIDTH=469>Tests if <TT>OPERAND</TT> is owned by the read uid/gid. Under DOS, it always returns true.</TD></TR><TR><TD WIDTH=121><TT>-p</TT> <TT>OPERAND</TT></TD><TD WIDTH=469>Tests if <TT>OPERAND</TT> is a named pipe.</TD></TR><TR><TD WIDTH=121><TT>-r</TT> <TT>OPERAND</TT></TD><TD WIDTH=469>Tests if <TT>OPERAND</TT> can be read from.</TD></TR><TR><TD WIDTH=121><TT>-R</TT> <TT>OPERAND</TT></TD><TD WIDTH=469>Tests if <TT>OPERAND</TT> can be read from by the real uid/gid. Under DOS, it is identical to -r.</TD></TR><TR><TD WIDTH=121><TT>-s</TT> <TT>OPERAND</TT></TD><TD WIDTH=469>Returns the size of <TT>OPERAND</TT> in bytes. Therefore, it returns true if <TT>OPERAND</TT> is non-zero.</TD></TR><TR><TD WIDTH=121><TT>-S</TT> <TT>OPERAND</TT></TD><TD WIDTH=469>Tests if <TT>OPERAND</TT> is a socket.</TD></TR><TR><TD WIDTH=121><TT>-t</TT> <TT>OPERAND</TT></TD><TD WIDTH=469>Tests if <TT>OPERAND</TT> is opened to a tty.</TD></TR><TR><TD WIDTH=121><TT>-T</TT> <TT>OPERAND</TT></TD><TD WIDTH=469>Tests if <TT>OPERAND</TT> is a text file. If <TT>OPERAND</TT> is a file handle, then the current buffer is examined, instead of the file itself.</TD></TR><TR><TD WIDTH=121><TT>-u</TT> <TT>OPERAND</TT></TD><TD WIDTH=469>Tests if <TT>OPERAND</TT> has the setuid bit set.</TD></TR><TR><TD WIDTH=121><TT>-w</TT> <TT>OPERAND</TT></TD><TD WIDTH=469>Tests if <TT>OPERAND</TT> can be written to.</TD></TR><TR><TD WIDTH=121><TT>-W</TT> <TT>OPERAND</TT></TD><TD WIDTH=469>Tests if <TT>OPERAND</TT> can be written to by the real uid/gid. Under DOS, it is identical to -w.</TD></TR><TR><TD WIDTH=121><TT>-x</TT> <TT>OPERAND</TT></TD><TD WIDTH=469>Tests if <TT>OPERAND</TT> can be executed.</TD></TR><TR><TD WIDTH=121><TT>-X</TT> <TT>OPERAND</TT></TD><TD WIDTH=469>Tests if <TT>OPERAND</TT> can be executed by the real uid/gid. Under DOS, it is identical to -x.</TD></TR><TR><TD WIDTH=121><TT>-z</TT> <TT>OPERAND</TT></TD><TD WIDTH=469>Tests if <TT>OPERAND</TT> size is zero.</TD></TR></TABLE></CENTER><P><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD><B>Note</B></TD></TR><TR><TD><BLOCKQUOTE>If the <TT>OPERAND</TT> is not specified in the file test, the <TT>$ </TT>variable will be used instead.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><P>The operand used by the file tests can be either a file handleor a file name. The file tests work by internally calling theoperating system to determine information about the file in question.The operators will evaluate to true if the test succeeds and falseif it does not.<P>If you need to perform two or more tests on the same file, youuse the special underscore (<TT>_</TT>)file handle. This tells Perl to use the file information for thelast system query and saves time. However, the underscore filehandle does have some caveats. It does not work with the <TT>-t</TT>operator. In addition, the <TT>lstat()</TT>fuNCtion and <TT>-l</TT> test willleave the system buffer filled with information about a symboliclink, not a real file.<P>The <TT>-T</TT> and <TT>-B</TT>file tests will examine the first block or so of the file. Ifmore than 10 percent of the bytes are non-characters or if a nullbyte is eNCountered, then the file is considered a binary file.<I>Binary</I> files are normally data files, as opposed to textor human-readable files. If you need to work with binary files,be sure to use the <TT>binmode()</TT>file fuNCtion, which is described in the section, &quot;Example:Binary Files,&quot; later in this chapter.<H3><A NAME="ExampleUsingFileTests">Example: Using File Tests</A></H3><P>For our first example with file tests, let's examine a list offiles from the command line and determine if each is a regularfile or a special file.<P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><p><BLOCKQUOTE><I>Start a </I><TT><I>foreach</I></TT><I>loop that looks at the command line array. Each element in thearray is assigned to the default loop variable </I><TT><I>$_</I></TT><I>.<BR>Print the file name contained in </I><TT><I>$_</I></TT><I>.<BR>Print a message indicating the type of file by checking the evaluationof the </I><TT><I>-f</I></TT><I> operator.</I></BLOCKQUOTE><HR><BLOCKQUOTE><B>Listing 9.4&nbsp;&nbsp;09LST04.PL-Using the </B><TT><I><B><FONT FACE="Courier">-f</FONT></B></I></TT><B>Operator to Find Regular Files Inside a </B><TT><I><B><FONT FACE="Courier">foreach</FONT></B></I></TT><B>Loop<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE>foreach (@ARGV) {    print;    print((-f) ? &quot; -REGULAR\n&quot; : &quot; -SPECIAL\n&quot;)}</PRE></BLOCKQUOTE><HR><P>When this program is run using the following command line:<BLOCKQUOTE><PRE>perl -w 09lst01.pl \perl5 perl.exe \windows</PRE></BLOCKQUOTE><P>the following is displayed:<BLOCKQUOTE><PRE>09lst01.pl -REGULAR\perl5 -SPECIALperl.exe -REGULAR\windows -SPECIAL</PRE></BLOCKQUOTE><P>Each of the directories listed on the command line were recognizedas special files. If you want to ignore all special files in thecommand line, you do so like this:<P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><p><BLOCKQUOTE><I>Start a </I><TT><I>foreach</I></TT><I>loop that looks at the command line array.<BR>If the current file is special, then skip it and go on to thenext iteration of the </I><TT><I>foreach</I></TT><I>loop.<BR>Print the current file name that is contained in </I><TT><I>$_</I></TT><I>.<BR>Print a message indicating the type of file.</I></BLOCKQUOTE><HR><BLOCKQUOTE><B>Listing 9.5&nbsp;&nbsp;09LST05.PL-Using the </B><TT><I><B><FONT FACE="Courier">-f</FONT></B></I></TT><B>Operator to Find Regular Files Inside a </B><TT><I><B><FONT FACE="Courier">foreach</FONT></B></I></TT><B>Loop<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE>foreach (@ARGV) {    next unless -f;    # ignore all non-normal files.    print;    print((-f) ? &quot; -REGULAR\n&quot; : &quot; -SPECIAL\n&quot;)}</PRE></BLOCKQUOTE><HR><P>When this program is run using the following command line:<BLOCKQUOTE><PRE>perl -w 09lst01.pl \perl perl.exe \windows</PRE></BLOCKQUOTE><P>the following is displayed:<BLOCKQUOTE><PRE>09lst01.pl -REGULARperl.exe -REGULAR</PRE></BLOCKQUOTE><P>Notice that only the regular file names are displayed. The twodirectories on the command line were ignored.<P>As mentioned above, you can use the underscore file handle tomake two tests in a row on the same file so that your programcan execute faster and use less system resources. This could beimportant if your application is time critical or makes many repeatedtests on a large number of files.<P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><p><BLOCKQUOTE><I>Start a </I><TT><I>foreach</I></TT><I>loop that looks at the command line array.<BR>If the current file is special, then skip it and go on to thenext iteration of the </I><TT><I>foreach</I></TT><I>loop.<BR>Determine the number of bytes in the file with the </I><TT><I>-s</I></TT><I>operator using the underscore file handle so that a second operatingsystem call is not needed.<BR>Print a message indicating the name and size of the file.</I></BLOCKQUOTE><HR><BLOCKQUOTE><B>Listing 9.6&nbsp;&nbsp;09LST06.PL-Finding the Size in Bytesof Regular Files Listed on the Command Line<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE>foreach (@ARGV) {    next unless -f;    $fileSize = -s _;    print(&quot;$_ is $fileSize bytes long.\n&quot;);}</PRE></BLOCKQUOTE><HR><P>When this program is run using the following command line:<BLOCKQUOTE><PRE>perl -w 09lst06.pl \perl5 09lst01.pl \windows perl.exe</PRE></BLOCKQUOTE><P>the following is displayed:<BLOCKQUOTE><PRE>09lst01.pl is 36 bytes long.perl.exe is 61952 bytes long.<BR></PRE></BLOCKQUOTE><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD><B>Tip</B></TD></TR><TR><TD><BLOCKQUOTE>Don't get the underscore file handle confused with the <TT>$_</TT> special variable. The underscore file handle tells Perl to use the file information from the last system call and the <TT>$ </TT>variable is used as the default parameter for a variety of fuNCtions.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><H2><A NAME="FileFuNCtionsBR"><FONT SIZE=5 COLOR=#FF0000>File FuNCtions<BR></FONT></A></H2><P><CENTER><B>Table 9.3&nbsp;&nbsp;Perl's File FuNCtions</B></CENTER><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD WIDTH=241><I>FuNCtion</I></TD><TD WIDTH=349><I>Description</I></TD></TR><TR><TD WIDTH=241><TT>binmode</TT>(<TT>FILE_HANDLE</TT>)</TD><TD WIDTH=349>This fuNCtion puts FILE_HANDLE into a binary mode. For more information, see the section, &quot;Example: Binary Files,&quot; later in this chapter.</TD></TR><TR><TD WIDTH=241><TT>chdir</TT>(<TT>DIR_NAME</TT>)</TD><TD WIDTH=349>Causes your program to use DIR_NAME as the current directory. It will return true if the change was successful, false if not.</TD></TR><TR><TD WIDTH=241><TT>chmod</TT>(<TT>MODE</TT>, <TT>FILE_LIST</TT>)</TD><TD WIDTH=349>This UNIX-based fuNCtion changes the permissions for a list of files. A count of the number of files whose permissions was changed is returned. There is no DOS equivalent for this fuNCtion.</TD></TR><TR><TD WIDTH=241><TT>chown</TT>(<TT>UID</TT>, <TT>GID</TT>, <TT>FILE_LIST</TT>)</TD><TD WIDTH=349>This UNIX-based fuNCtion changes the owner and group for a list of files. A count of the number of files whose ownership was changed is returned. There is no DOS equivalent for this fuNCtion.

⌨️ 快捷键说明

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