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

📄 ch9.htm

📁 this is a book on pearl , simple example with explanation is given here. it could be beneficial for
💻 HTM
📖 第 1 页 / 共 5 页
字号:
</TD></TR><TR><TD WIDTH=241><TT>close</TT>(<TT>FILE_HANDLE</TT>)</TD><TD WIDTH=349>Closes the connection between your program and the file opened with <TT>FILE_HANDLE</TT>.</TD></TR><TR><TD WIDTH=241><TT>closedir</TT>(<TT>DIR_HANDLE</TT>)</TD><TD WIDTH=349>Closes the connection between your program and the directory opened with <TT>DIR_HANDLE</TT>.</TD></TR><TR><TD WIDTH=241><TT>eof</TT>(<TT>FILE_HANDLE</TT>)</TD><TD WIDTH=349>Returns true if the next read on <TT>FILE_HANDLE</TT> will result in hitting the end of the file or if the file is not open. If <TT>FILE_HANDLE</TT> is not specified the status of the last file read is returned. All input fuNCtions return the undefined value when the end of file is reached, so you'll almost never need to use <TT>eof()</TT>.</TD></TR><TR><TD WIDTH=241><TT>fcntl</TT>(<TT>FILE_HANDLE</TT>, </TD><TD WIDTH=349>Implements the <TT>fcntl()</TT> fuNCtion which lets </TD></TR><TR><TD WIDTH=241><TT>FUNCTION</TT>, <TT>SCALAR</TT>)</TD><TD WIDTH=349>you perform various file control operations. Its use is beyond the scope of this book.</TD></TR><TR><TD WIDTH=241><TT>fileno</TT>(<TT>FILE_HANDLE</TT>)</TD><TD WIDTH=349>Returns the file descriptor for the specified <TT>FILE_HANDLE</TT>.</TD></TR><TR><TD WIDTH=241><TT>flock</TT>(<TT>FILEHANDLE</TT>, <TT>OPERATION</TT>)</TD><TD WIDTH=349>This fuNCtion will place a lock on a file so that multiple users or programs can't simultaneously use it. The <TT>flock()</TT> fuNCtion is beyond the scope of this book.</TD></TR><TR><TD WIDTH=241><TT>getc</TT>(<TT>FILE_HANDLE</TT>)</TD><TD WIDTH=349>Reads the next character from <TT>FILE_HANDLE</TT>. If <TT>FILE_HANDLE</TT> is not specified, a character will be read from <TT>STDIN</TT>.</TD></TR><TR><TD WIDTH=241><TT>glob</TT>(<TT>EXPRESSION</TT>)</TD><TD WIDTH=349>Returns a list of files that match the specification of <TT>EXPRESSION</TT>, which can contain wildcards. For instaNCe, <TT>glob</TT>(<TT>&quot;*.pl&quot;</TT>) will return a list of all Perl program files in the current directory.</TD></TR><TR><TD WIDTH=241><TT>ioctl</TT>(<TT>FILE_HANDLE</TT>, </TD><TD WIDTH=349>Implements the <TT>ioctl()</TT> fuNCtion which lets </TD></TR><TR><TD WIDTH=241><TT>FUNCTION</TT>, <TT>SCALAR</TT>)</TD><TD WIDTH=349>you perform various file control operations. Its use is beyond the scope of this book. For more in-depth discussion of this fuNCtion see Que's <I>Special Edition Using Perl for Web Programming</I>.</TD></TR><TR><TD WIDTH=241><TT>link</TT>(<TT>OLD_FILE_NAME</TT>, </TD><TD WIDTH=349>This UNIX-based fuNCtion creates a new </TD></TR><TR><TD WIDTH=241><TT>NEW_FILE_NAME</TT>)</TD><TD WIDTH=349>file name that is linked to the old file name. It returns true for success and false for failure. There is no DOS equivalent for this fuNCtion.</TD></TR><TR><TD WIDTH=241><TT>lstat</TT>(<TT>FILE_HANDLE_OR_</TT></TD><TD WIDTH=349>Returns file statistics in a 13-element array. </TD></TR><TR><TD WIDTH=241><TT>FILE_NAME</TT>)</TD><TD WIDTH=349><TT>lstat()</TT> is identical to <TT>stat()</TT> except that it can also return information about symbolic links. See the section,&quot;Example: Getting File Statistics,&quot; for more information.</TD></TR><TR><TD WIDTH=241><TT>mkdir</TT>(<TT>DIR_NAME</TT>, <TT>MODE</TT>)</TD><TD WIDTH=349>Creates a directory named <TT>DIR_NAME</TT>. If you try to create a subdirectory, the parent must already exist. This fuNCtion returns false if the directory can't be created. The special variable <TT>$!</TT> is assigned the error message.</TD></TR><TR><TD WIDTH=241><TT>open</TT>(<TT>FILE_HANDLE</TT>, <TT>EXPRESSION</TT>)</TD><TD WIDTH=349>Creates a link between <TT>FILE_HANDLE</TT> and a file specified by <TT>EXPRESSION</TT>. See the section, &quot;Example: Opening a File,&quot; for more information.</TD></TR><TR><TD WIDTH=241><TT>opendir</TT>(<TT>DIR_HANDLE</TT>, <TT>DIR_NAME</TT>)</TD><TD WIDTH=349>Creates a link between <TT>DIR_HANDLE</TT> and the directory specified by <TT>DIR_NAME</TT>. <TT>opendir()</TT> returns true if successful, false otherwise.</TD></TR><TR><TD WIDTH=241><TT>pipe</TT>(<TT>READ_HANDLE</TT>, </TD><TD WIDTH=349>Opens a pair of connected pipes like the </TD></TR><TR><TD WIDTH=241><TT>WRITE_HANDLE</TT>)</TD><TD WIDTH=349>corresponding system call. Its use is beyond the scope of this book. For more on this fuNCtion see Que's <I>Special Edition Using Perl for Web Programming</I>.</TD></TR><TR><TD WIDTH=241><TT>print FILE_HANDLE</TT> (<TT>LIST</TT>)</TD><TD WIDTH=349>Sends a list of strings to <TT>FILE_HANDLE</TT>. If <TT>FILE_HANDLE</TT> is not specified, then <TT>STDOUT</TT> is used. See the section, &quot;Example: Printing Revisited,&quot; for more information.</TD></TR><TR><TD WIDTH=241><TT>printf FILE_HANDLE</TT> </TD><TD WIDTH=349>Sends a list of strings in a format specified by </TD></TR><TR><TD WIDTH=241>(<TT>FORMAT</TT>, <TT>LIST</TT>)</TD><TD WIDTH=349><TT>FORMAT</TT> to <TT>FILE_HANDLE</TT>. If <TT>FILE_HANDLE</TT> is not specified, then <TT>STDOUT</TT> is used. See the section, &quot;Example: Printing Revisited,&quot; for more information.</TD></TR><TR><TD WIDTH=241><TT>read</TT>(<TT>FILE_HANDLE</TT>, <TT>BUFFER</TT>, </TD><TD WIDTH=349>Reads bytes from <TT>FILE_HANDLE</TT> starting at </TD></TR><TR><TD WIDTH=241><TT>LENGTH</TT>,<TT>LENGTH</TT> <TT>OFFSET</TT>)</TD><TD WIDTH=349><TT>OFFSET</TT> position in the file into the scalar variable called <TT>BUFFER</TT>. It returns the number of bytes read or the undefined value.</TD></TR><TR><TD WIDTH=241><TT>readdir</TT>(<TT>DIR_HANDLE</TT>)</TD><TD WIDTH=349>Returns the next directory entry from <TT>DIR_HANDLE</TT> when used in a scalar context. If used in an array context, all of the file entries in <TT>DIR_HANDLE</TT> will be returned in a list. If there are no more entries to return, the undefined value or a null list will be returned depending on the context.</TD></TR><TR><TD WIDTH=241><TT>readlink</TT>(<TT>EXPRESSION</TT>)</TD><TD WIDTH=349>This UNIX-based fuNCtion returns that value of a symbolic link. If an error occurs, the undefined value is returned and the special variable <TT>$!</TT> is assigned the error message. The <TT>$_ </TT>special variable is used if <TT>EXPRESSION</TT> is not specified.</TD></TR><TR><TD WIDTH=241><TT>rename</TT>(<TT>OLD_FILE_NAME</TT>, </TD><TD WIDTH=349>Changes the name of a file. You can use this </TD></TR><TR><TD WIDTH=241><TT>NEW_FILE_NAME</TT>)</TD><TD WIDTH=349>fuNCtion to change the directory where a file resides, but not the disk drive or volume.</TD></TR><TR><TD WIDTH=241><TT>rewinddir</TT>(<TT>DIR_HANDLE</TT>)</TD><TD WIDTH=349>Resets <TT>DIR_HANDLE</TT> so that the next <TT>readdir()</TT> starts at the beginning of the directory.</TD></TR><TR><TD WIDTH=241><TT>rmdir</TT>(<TT>DIR_NAME</TT>)</TD><TD WIDTH=349>Deletes an empty directory. If the directory can be deleted it returns false and <TT>$!</TT> is assigned the error message. The <TT>$ </TT>special variable is used if <TT>DIR_NAME</TT> is not specified.</TD></TR><TR><TD WIDTH=241><TT>seek</TT>(<TT>FILE_HANDLE</TT>, <TT>POSITION</TT>, </TD><TD WIDTH=349>Moves to <TT>POSITION</TT> in the file connected to </TD></TR><TR><TD WIDTH=241><TT>WHENCE</TT>)</TD><TD WIDTH=349><TT>FILE_HANDLE</TT>. The <TT>WHENCE</TT> parameter determines if <TT>POSITION</TT> is an offset from the beginning of the file (<TT>WHENCE=0</TT>), the current position in the file (<TT>WHENCE=1</TT>), or the end of the file (<TT>WHENCE=2</TT>).</TD></TR><TR><TD WIDTH=241><TT>seekdir</TT>(<TT>DIR_HANDLE</TT>, <TT>POSITION</TT>)</TD><TD WIDTH=349>Sets the current position for <TT>readdir()</TT>. <TT>POSITION</TT> must be a value returned by the <TT>telldir()</TT> fuNCtion.</TD></TR><TR><TD WIDTH=241><TT>select</TT>(<TT>FILE_HANDLE</TT>)</TD><TD WIDTH=349>Sets the default <TT>FILE_HANDLE</TT> for the <TT>write()</TT> and <TT>print()</TT> fuNCtions. It returns the currently selected file handle so that you may restore it if needed. You can see the section, &quot;Example: Printing Revisited,&quot; to see this fuNCtion in action.</TD></TR><TR><TD WIDTH=241><TT>sprintf</TT>(<TT>FORMAT</TT>, <TT>LIST</TT>)</TD><TD WIDTH=349>Returns a string whose format is specified by <TT>FORMAT</TT>.</TD></TR><TR><TD WIDTH=241><TT>stat</TT>(<TT>FILE_HANDLE_OR_</TT></TD><TD WIDTH=349>Returns file statistics in a 13-element array. </TD></TR><TR><TD WIDTH=241><TT>FILE_NAME</TT>)</TD><TD WIDTH=349>See the section, &quot;Example: Getting File Statistics,&quot; for more information.</TD></TR><TR><TD WIDTH=241><TT>symlink</TT>(<TT>OLD_FILE_NAME</TT>, </TD><TD WIDTH=349>This UNIX-based fuNCtion creates a new </TD></TR><TR><TD WIDTH=241><TT>NEW_FILE_NAME</TT>)</TD><TD WIDTH=349>file name symbolically linked to the old file name. It returns false if the <TT>NEW_FILE_NAME</TT> cannot be created.</TD></TR><TR><TD WIDTH=241><TT>sysread</TT>(<TT>FILE_HANDLE</TT>, <TT>BUFFER</TT>, </TD><TD WIDTH=349>Reads <TT>LENGTH</TT> bytes from <TT>FILE_HANDLE</TT> starting </TD></TR><TR><TD WIDTH=241><TT>LENGTH</TT>,<TT> OFFSET</TT>)</TD><TD WIDTH=349>at <TT>OFFSET</TT> position in the file into the scalar variable called <TT>BUFFER</TT>. It returns the number of bytes read or the undefined value.</TD></TR><TR><TD WIDTH=241><TT>syswrite</TT>(<TT>FILE_HANDLE</TT>, <TT>BUFFER</TT>, </TD><TD WIDTH=349>Writes <TT>LENGTH</TT> bytes from <TT>FILE_HANDLE</TT> starting </TD></TR><TR><TD WIDTH=241><TT>LENGTH</TT>, <TT>OFFSET</TT>)</TD><TD WIDTH=349>at <TT>OFFSET</TT> position in the file into the scalar variable called <TT>BUFFER</TT>. It returns the number of bytes written or the undefined value.</TD></TR><TR><TD WIDTH=241><TT>tell</TT>(<TT>FILE_HANDLE</TT>)</TD><TD WIDTH=349>Returns the current file position for <TT>FILE_HANDLE</TT>. If <TT>FILE_HANDLE</TT> is not specified, the file position for the last file read is returned.</TD></TR><TR><TD WIDTH=241><TT>telldir</TT>(<TT>DIR_HANDLE</TT>)</TD><TD WIDTH=349>Returns the current position for <TT>DIR_HANDLE</TT>. The return value may be passed to <TT>seekdir()</TT> to access a particular location in a directory.</TD></TR><TR><TD WIDTH=241><TT>truNCate</TT>(<TT>FILE_HANDLE</TT>, <TT>LENGTH</TT>)</TD><TD WIDTH=349>TruNCates the file opened on <TT>FILE_HANDLE</TT> to be <TT>LENGTH</TT> bytes long.</TD></TR><TR><TD WIDTH=241><TT>unlink</TT>(<TT>FILE_LIST</TT>)</TD><TD WIDTH=349>Deletes a list of files. If <TT>FILE_LIST</TT> is not specified, then <TT>$</TT> will be used. It returns the number of files successfully deleted. Therefore, it returns false or 0 if no files were deleted.</TD></TR><TR><TD WIDTH=241><TT>utime</TT>(<TT>FILE_LIST</TT>)</TD><TD WIDTH=349>This UNIX-based fuNCtion changes the access and modification times on each file in <TT>FILE_LIST</TT>.</TD></TR><TR><TD WIDTH=241><TT>write</TT>(<TT>FILE_HANDLE</TT>)</TD><TD WIDTH=349>Writes a formatted record to <TT>FILE_HANDLE</TT>. See <A HREF="ch11.htm" >Chapter 11</A>, &quot;Creating Reports,&quot; for more information.</TD></TR></TABLE></CENTER><P><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD><B>Note</B></TD></TR><TR><TD><BLOCKQUOTE>The UNIX-based fuNCtions will be discussed further in <A HREF="ch18.htm" >Chapter 18</A>, &quot;Using Internet Protocols.&quot;</BLOCKQUOTE><BLOCKQUOTE>UNIX-based implementations of Perl have several database fuNCtions available to them. For example, <TT>dbmopen()</TT> and <TT>dbmclose()</TT>. These fuNCtions are beyond the scope of this book.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><H3><A NAME="ExampleOpeningFiles">Example: Opening Files</A></H3><P>The <TT>open()</TT> fuNCtion is usedto open a file and create a connection to it called a file handle.The basic <TT>open()</TT> fuNCtioncall looks like this:<BLOCKQUOTE><PRE>open(FILE_HANDLE);</PRE></BLOCKQUOTE><P>The <TT>FILE_HANDLE </TT>parameterin this version of open() is the name for the new file handle.It is also the name of the scalar variable that holds the filename that you would like to open for input. For example:<P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><p><BLOCKQUOTE><I>Assign the file name, </I><TT><I>FIXED.DAT</I></TT><I>,to the </I><TT><I>$INPUT_FILE</I></TT><I>variable. All capital letters are used for the variable name toindicate that it is also the name of the file handle.<BR>Open the file for reading.<BR>Read the entire file into </I><TT><I>@array</I></TT><I>.Each line of the file becomes a single element of the array.<BR>Close the file.<BR>Use a </I><TT><I>foreach</I></TT><I>loop to look at each element of </I><TT><I>@array</I></TT><I>.<BR>Print </I><TT><I>$_</I></TT><I>, theloop variable, which contains one of the elements of </I><TT><I>@array</I></TT><I>.</I></BLOCKQUOTE><HR><BLOCKQUOTE><B>Listing 9.7&nbsp;&nbsp;09LST07.PL-How to Open a File for Input<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE>$INPUT_FILE = &quot;fixed.dat&quot;;open(INPUT_FILE);@array = &lt;INPUT_FILE&gt;;close(INPUT_FILE);foreach (@array) {    print();}</PRE></BLOCKQUOTE><HR><P>This program displays:<BLOCKQUOTE><PRE>1212Jan       Jaspree             Painter3453Kelly     Horton              Jockey</PRE></BLOCKQUOTE><P>It is considered good programming practice to close any connectionsthat are made with the <TT>open()</TT>fuNCtion as soon as possible. While not strictly needed, it doesensure that all temporary buffers and caches are written to thehard disk in case of a power failure or other catastrophic failure.<BR><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD><B>Note</B></TD></TR><TR><TD><BLOCKQUOTE>DOS-and by extension, Windows-limits the number of files that you can have open at any given time. Typically, you can have from 20 to 50 files open. Normally, this is plenty. If you need to open more files, please see your DOS documentation.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><P>The <TT>open()</TT> fuNCtion has manyvariations to let you access files in different ways. Table 9.4shows all of the different methods used to open a file.<BR><P><CENTER><B>Table 9.4&nbsp;&nbsp;The Different Ways to Open a File</B></CENTER><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD WIDTH=175><I>Open Statement</I></TD><TD WIDTH=415><I>Description</I></TD></TR><TR><TD WIDTH=175><TT>open</TT>(<TT>FILE_HANDLE</TT>);</TD><TD WIDTH=415>Opens the file named in <TT>$FILE_HANDLE</TT> and connect to it using <TT>FILE_HANDLE</TT> as the file handle. The file will be opened for input only.</TD></TR><TR><TD WIDTH=175><TT>open</TT>(<TT>FILE_HANDLE</TT>, <TT><I>FILENAME.EXT</I></TT>);</TD><TD WIDTH=415>Opens the file called <TT><I>FILENAME.EXT</I></TT> for input using  <TT>FILE_HANDLE</TT> as the file handle.</TD></TR><TR><TD WIDTH=175><TT>open</TT>(<TT>FILE_HANDLE</TT>, +<TT><I>&lt;FILENAME.EXT</I></TT>);</TD><TD WIDTH=415>Opens <TT><I>FILENAME.EXT</I></TT> for input using <TT>FILE_HANDLE</TT> as the file handle.</TD></TR><TR><TD WIDTH=175><TT>open</TT>(<TT>FILE_HANDLE</TT>, <TT><I>&gt;FILENAME.EXT</I></TT>);</TD><TD WIDTH=415>Opens <TT><I>FILENAME.EXT</I></TT> for output using <TT>FILE_HANDLE</TT> as  the file handle.</TD></TR><TR><TD WIDTH=175><TT>open</TT>(<TT>FILE_HANDLE</TT>, <TT><I>-</I></TT>);</TD><TD WIDTH=415>Opens standard input.</TD></TR><TR><TD WIDTH=175><TT>open</TT>(<TT>FILE_HANDLE</TT>, <TT><I>&gt;-</I></TT>);</TD><TD WIDTH=415>Opens standard output.</TD></TR><TR><TD WIDTH=175><TT>open</TT>(<TT>FILE_HANDLE</TT>, <TT><I>&gt;&gt;FILENAME.EXT</I></TT>);</TD><TD WIDTH=415>Opens <TT><I>FILENAME.EXT</I></TT> for appending using <TT>FILE_HANDLE</TT> as the file handle.</TD></TR><TR><TD WIDTH=175><TT>open</TT>(<TT>FILE_HANDLE</TT>, <TT><I>+&lt;FILENAME.EXT</I></TT>);</TD><TD WIDTH=415>Opens <TT><I>FILENAME.EXT</I></TT> for both input and output using <TT>FILE_HANDLE</TT> as the file handle.</TD></TR><TR><TD WIDTH=175><TT>open</TT>(<TT>FILE_HANDLE</TT>, <TT><I>+&gt;FILENAME.EXT</I></TT>);</TD><TD WIDTH=415>Opens <TT><I>FILENAME.EXT</I></TT> for both input and output using <TT>FILE_HANDLE</TT> as the file handle.</TD></TR><TR><TD WIDTH=175><TT>open</TT>(<TT>FILE_HANDLE</TT>, <TT><I>+&gt;&gt;FILENAME.EXT</I></TT>);</TD><TD WIDTH=415>Opens <TT><I>FILENAME.EXT</I></TT> for both input and output using <TT>FILE_HANDLE</TT> as the file handle.</TD></TR><TR><TD WIDTH=175><TT>open</TT>(<TT>FILE_HANDLE</TT>, <TT><I>| PROGRAM</I></TT>)</TD><TD WIDTH=415>Sends the output printed to <TT>FILE_HANDLE</TT> to another program.</TD></TR><TR><TD WIDTH=175><TT>open</TT>(<TT>FILE_HANDLE</TT>, <TT><I>PROGRAM |</I></TT>)</TD><TD WIDTH=415>Reads the output from another program using  <TT>FILE_HANDLE</TT>.</TD></TR></TABLE></CENTER><P><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD><B>Note</B></TD></TR><TR><TD><BLOCKQUOTE>I am currently researching the differeNCes between +&lt;, +&gt;, and +&gt;&gt;. The research should be available by 12/1/97 as a link from </BLOCKQUOTE><BLOCKQUOTE><TT>http:\\www.mtolive.com\pbe\index.html.</TT></BLOCKQUOTE></TD></TR></TABLE></CENTER><P><P>For information about handling failures while opening files, see<A HREF="ch13.htm" >Chapter 13</A>, &quot;Handling Errors and Signals.&quot; <P>By prefixing the file name with a &gt; character you open thefile for output. This next example opens a file that will holda log of messages.<P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><p><BLOCKQUOTE><I>Call the </I><TT><I>open()</I></TT><I>fuNCtion to open the </I><TT><I>MESSAGE.LOG</I></TT><I>file for writing with </I><TT><I>LOGFILE</I></TT><I>as the file handle. If the open was successful, a true value willbe returned and the statement block will be executed.<BR>Send the first message to the </I><TT><I>MESSAGE.LOG</I></TT><I>file using the </I><TT><I>print()</I></TT><I>fuNCtion. Notice that an alternate method is being used to call</I><TT><I>print()</I></TT><I>.<BR>Send the second message to the </I><TT><I>MESSAGE.LOG</I></TT><I>file.<BR>Close the file.</I></BLOCKQUOTE><BLOCKQUOTE><PRE>if (open(LOGFILE, &quot;&gt;message.log&quot;)) {    print LOGFILE (&quot;This is message number 1.\n&quot;);    print LOGFILE (&quot;This is message number 2.\n&quot;);    close(LOGFILE);}</PRE></BLOCKQUOTE><P>This program displays nothing. Instead, the output from the <TT>print()</TT>

⌨️ 快捷键说明

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