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

📄 ch9.htm

📁 this is a book on pearl , simple example with explanation is given here. it could be beneficial for
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<HTML><HEAD><TITLE>Chapter 9 -- Using Files</TITLE><META></HEAD><BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#0000EE" VLINK="#551A8B" ALINK="#CE2910"><H1><FONT SIZE=6 COLOR=#FF0000>Chapter&nbsp;9</FONT></H1><H1><FONT SIZE=6 COLOR=#FF0000>Using Files</FONT></H1><HR><P><CENTER><B><FONT SIZE=5>CONTENTS</FONT></B></CENTER><UL><LI><A HREF="#SomeFilesAreStandard">Some Files Are Standard</A><UL><LI><A HREF="#ExampleUsingSTDIN">Example: Using STDIN</A><LI><A HREF="#ExampleUsingRedirectiontoChangeSTDINandSTDOUT">Example: Using Redirection to Change STDIN and STDOUT</A><LI><A HREF="#ExampleUsingtheDiamondOperatorltgt">Example: Using the Diamond Operator (&lt;&gt;)</A></UL><LI><A HREF="#FileTestOperators">File Test Operators</A><UL><LI><A HREF="#ExampleUsingFileTests">Example: Using File Tests</A></UL><LI><A HREF="#FileFuNCtionsBR">File FuNCtions<BR></A><UL><LI><A HREF="#ExampleOpeningFiles">Example: Opening Files</A><LI><A HREF="#ExampleBinaryFiles">Example: Binary Files</A><LI><A HREF="#ExampleGettingFileStatistics">Example: Getting File Statistics</A><LI><A HREF="#ExampleUsingtheDirectoryFuNCtions">Example: Using the Directory FuNCtions</A><LI><A HREF="#ExamplePrintingRevisited">Example: Printing Revisited</A></UL><LI><A HREF="#Globbing">Globbing</A><UL><LI><A HREF="#ExampleAssigningaGlobtoanArray">Example: Assigning a Glob to an Array</A></UL><LI><A HREF="#UsingDataStructureswithFiles">Using Data Structures with Files</A><UL><LI><A HREF="#ExampleSplittingaRecordintoFields">Example: Splitting a Record into Fields</A></UL><LI><A HREF="#Summary">Summary</A><LI><A HREF="#ReviewQuestions">Review Questions</A><LI><A HREF="#ReviewExercises">Review Exercises</A></UL><HR><P>If you've read the previous chapters and have executed some ofthe programs, then you already know that a file is a series ofbytes stored on a disk instead of inside the computer's memory.A <I>file</I> is good for long-term storage of information. Informationin the computer's memory is lost when the computer is turned off.Information on a disk, however, is persistent. It will be therewhen the computer is turned back on.<P>Back in <A HREF="ch1.htm" >Chapter 1</A> &quot;Getting Your Feet Wet,&quot; you sawhow to create a file using the edit program that comes with Windows95 and Windows NT. In this chapter, you'll see how to manipulatefiles with Perl.<P>There are four basic operations that you can do with files. Youcan open them, read from them, write to them, and close them.Opening a file creates a connection between your program and thelocation on the disk where the file is stored. Closing a fileshuts down that connection.<P>Every file has a unique <I>fully qualified </I>name so that itcan't be confused with other files. The fully qualified name iNCludesthe name of the disk, the directory, and the file name. Filesin different directories can have the same name because the operatingsystem considers the directory name to be a part of the file name.Here are some fully qualified file names:<BLOCKQUOTE><PRE>c:/windows/win95.txtc:/windows/command/scandisk.inic:/a_long_directory_name/a_long_subdirectory_name/a_long_file_name.doc<BR></PRE></BLOCKQUOTE><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD><B>Caution</B></TD></TR><TR><TD><BLOCKQUOTE>You may be curious to know if spaces can be used inside file names. Yes, they can. But, if you use spaces, you need to surround the file name with quotes when referring to it from a DOS or UNIX command line.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD><B>Note</B></TD></TR><TR><TD><BLOCKQUOTE>It is very important that you check for errors when dealing with files. To simplify the examples in this chapter, little error checking will be used in the example. Instead, error checking information will be discussed in <A HREF="ch13.htm" >Chapter 13</A>, &quot;Handling Errors and Signals.&quot;</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><H2><A NAME="SomeFilesAreStandard"><FONT SIZE=5 COLOR=#FF0000>Some Files Are Standard</FONT></A></H2><P>In an effort to make programs more uniform, there are three connectionsthat always exist when your program starts. These are <TT>STDIN</TT>,<TT>STDOUT</TT>, and <TT>STDERR</TT>.Actually, these names are <I>file handles</I>. File handles arevariables used to manipulate files. Just like you need to grabthe handle of a hot pot before you can pick it up, you need afile handle before you can use a file. Table 9.1 describes thethree file handles.<BR><P><CENTER><B>Table 9.1&nbsp;&nbsp;The Standard File Handles</B></CENTER><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD WIDTH=73><I>Name</I></TD><TD WIDTH=517><I>Description</I></TD></TR><TR><TD WIDTH=73><TT>STDIN</TT></TD><TD WIDTH=517>Reads program input. Typically this is the computer's keyboard.</TD></TR><TR><TD WIDTH=73><TT>STDOUT</TT></TD><TD WIDTH=517>Displays program output. This is usually the computer's monitor.</TD></TR><TR><TD WIDTH=73><TT>STDERR</TT></TD><TD WIDTH=517>Displays program errors. Most of the time, it is equivalent to <TT>STDOUT</TT>, which means the error messages will be displayed on the computer's monitor.</TD></TR></TABLE></CENTER><P><P>You've been using the <TT>STDOUT</TT>file handle without knowing it for every <TT>print()</TT>statement in this book. The <TT>print()</TT>fuNCtion uses <TT>STDOUT</TT> as thedefault if no other file handle is specified. Later in this chapter,in the &quot;Examples: Printing Revisited&quot; section, you willsee how to send output to a file instead of to the monitor.<H3><A NAME="ExampleUsingSTDIN">Example: Using STDIN</A></H3><P>Reading a line of input from the standard input, <TT>STDIN</TT>,is one of the easiest things that you can do in Perl. This followingthree-line program will read a line from the keyboard and thendisplay it. This will continue until you press <TT>Ctrl+Z</TT>on DOS systems or <TT>Ctrl-D</TT>on UNIX systems.<HR><BLOCKQUOTE><B>Listing 9.1&nbsp;&nbsp;09LST01.PL-Read from Standard InputUntil an End-of-File Character Is Found<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE>while (&lt;STDIN&gt;) {    print();}</PRE></BLOCKQUOTE><HR><P>The <TT>&lt;&gt;</TT> characters,when used together, are called the <I>diamond</I> operator. Ittells Perl to read a line of input from the file handle insidethe operator. In this case, <TT>STDIN</TT>.Later, you'll use the diamond operator to read from other filehandles.<P>In this example, the diamond operator assigned the value of theinput string to <TT>$_ </TT>. Then,the <TT>print()</TT> fuNCtion wascalled with no parameters, which tells <TT>print()</TT>to use <TT>$_</TT> as the defaultparameter. Using the <TT>$_ </TT>variable can save a lot of typing, but I'll let you decide whichis more readable. Here is the same program without using <TT>$_</TT>.<BLOCKQUOTE><PRE>while ($inputLine = &lt;STDIN&gt;) {    print($inputLine);}</PRE></BLOCKQUOTE><P>When you pressed <TT>Ctrl+Z</TT> or<TT>Ctrl+D</TT>, you told Perl thatthe input file was finished. This caused the diamond operatorto return the undefined value which Perl equates to false andcaused the <TT>while</TT> loop toend. In DOS (and therefore in all of the flavors of Windows),26-the value of <TT>Ctrl+Z</TT>-isconsidered to be the end-of-file indicator. As DOS reads or writesa file, it monitors the data stream and when a value of 26 iseNCountered the file is closed. UNIX does the same thing whena value of 4-the value of <TT>Ctrl+D</TT>-isread.<BR><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD><B>Tip</B></TD></TR><TR><TD><BLOCKQUOTE>When a file is read using the diamond operator, the newline character that ends the line is kept as part of the input string. Frequently, you'll see the <TT>chop()</TT> fuNCtion used to remove the newline. For instaNCe, <TT>chop($inputLine = &lt;INPUT_FILE&gt;);</TT>. This statement reads a line from the input file, assigns its value to <TT>$inputLine</TT> and then removes that last character from <TT>$inputLine</TT>-which is almost guaranteed to be a newline character. If you fear that the last character is not a newline, use the <TT>chomp()</TT> fuNCtion instead.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><H3><A NAME="ExampleUsingRedirectiontoChangeSTDINandSTDOUT">Example: Using Redirection to Change STDIN and STDOUT</A></H3><P>DOS and UNIX let you change the standard input from being thekeyboard to being a file by changing the command line that youuse to execute Perl programs. Until now, you probably used a commandline similar to:<BLOCKQUOTE><PRE>perl -w 09lst01.pl</PRE></BLOCKQUOTE><P>In the previous example, Perl read the keyboard to get the standardinput. But, if there was a way to tell Perl to use the file <TT>09LST01.PL</TT>as the standard input, you could have the program print itself.Pretty neat, huh? Well, it turns out that you can change the standardinput. It's done this way:<BLOCKQUOTE><PRE>perl -w 09lst01.pl &lt; 09lst01.pl</PRE></BLOCKQUOTE><P>The &lt; character is used to <I>redirect</I> the standard inputto the 09LST01.PL file. You now have a program that duplicatesthe fuNCtionality of the DOS type command. And it only took threelines of Perl code!<P>You can redirect standard output to a file using the <TT>&gt;</TT>character. So, if you wanted a copy of <TT>09LST01.PL</TT>to be sent to <TT>OUTPUT.LOG,</TT>you could use this command line:<BLOCKQUOTE><PRE>perl -w 09lst01.pl &lt;09lst01.pl &gt;output.log</PRE></BLOCKQUOTE><P>Keep this use of the <TT>&lt;</TT>and <TT>&gt;</TT> characters in mind.You'll be using them again shortly when we talk about the <TT>open()</TT>fuNCtion. The <TT>&lt;</TT> characterwill signify that files should be opened for input and the <TT>&gt;</TT>will be used to signify an output file. But first, let's continuetalking about accessing files listed on the command line.<H3><A NAME="ExampleUsingtheDiamondOperatorltgt">Example: Using the Diamond Operator (&lt;&gt;)</A></H3><P>If no file handle is used with the diamond operator, Perl willexamine the <TT>@ARGV</TT> specialvariable. If <TT>@ARGV</TT> has noelements, then the diamond operator will read from <TT>STDIN</TT>-eitherfrom the keyboard or from a redirected file. So, if you wantedto display the contents of more than one file, you could use theprogram shown in Listing 9.2.<HR><BLOCKQUOTE><B>Listing 9.2&nbsp;&nbsp;09LST02.PL-Read from Multiple Filesor from </B><TT><I><B><FONT FACE="Courier">STDIN<BR></FONT></B></I></TT></BLOCKQUOTE><BLOCKQUOTE><PRE>while (&lt;&gt;) {    print();}</PRE></BLOCKQUOTE><HR><P>The command line to run the program might look like this:<BLOCKQUOTE><PRE>perl -w 09lst02.pl 09lst01.pl 09lst02.pl</PRE></BLOCKQUOTE><P>And the output would be:<BLOCKQUOTE><PRE>while (&lt;STDIN&gt;) {    print();}while (&lt;&gt;) {    print();}</PRE></BLOCKQUOTE><P>Perl will create the <TT>@ARGV</TT>array from the command line. Each file name on the command line-afterthe program name-will be added to the <TT>@ARGV</TT>array as an element. When the program runs the diamond operatorstarts reading from the file name in the first element of thearray. When that entire file has been read, the next file is readfrom, and so on, until all of the elements have been used. Whenthe last file has be finished, the <TT>while</TT>loop will end.<P>Using the diamond operator to iterate over a list of file namesis very handy. You can use it in the middle of your program byexplicitly assigning a list of file names to the <TT>@ARGV</TT>array. Listing 9.3 shows what this might look like in a program.<HR><BLOCKQUOTE><B>Listing 9.3&nbsp;&nbsp;09LST03.PL-Read from Multiple FilesUsing the @ARGV Array<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE>@ARGV = (&quot;09lst01.pl&quot;, &quot;09lst02.pl&quot;);while (&lt;&gt;) {

⌨️ 快捷键说明

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