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

📄 ch9.htm

📁 prrl 5 programs codes in the book
💻 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 of

the programs, then you already know that a file is a series of

bytes stored on a disk instead of inside the computer's memory.

A <I>file</I> is good for long-term storage of information. Information

in the computer's memory is lost when the computer is turned off.

Information on a disk, however, is persistent. It will be there

when the computer is turned back on.

<P>

Back in <A HREF="ch1.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/ch1.htm" >Chapter 1</A> &quot;Getting Your Feet Wet,&quot; you saw

how to create a file using the edit program that comes with Windows

95 and Windows NT. In this chapter, you'll see how to manipulate

files with Perl.

<P>

There are four basic operations that you can do with files. You

can open them, read from them, write to them, and close them.

Opening a file creates a connection between your program and the

location on the disk where the file is stored. Closing a file

shuts down that connection.

<P>

Every file has a unique <I>fully qualified </I>name so that it

can't be confused with other files. The fully qualified name iNCludes

the name of the disk, the directory, and the file name. Files

in different directories can have the same name because the operating

system considers the directory name to be a part of the file name.

Here are some fully qualified file names:

<BLOCKQUOTE>

<PRE>

c:/windows/win95.txt

c:/windows/command/scandisk.ini

c:/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" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/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 connections

that 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 are

variables used to manipulate files. Just like you need to grab

the handle of a hot pot before you can pick it up, you need a

file handle before you can use a file. Table 9.1 describes the

three 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 the

default if no other file handle is specified. Later in this chapter,

in the &quot;Examples: Printing Revisited&quot; section, you will

see 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 following

three-line program will read a line from the keyboard and then

display 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 Input

Until 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. It

tells Perl to read a line of input from the file handle inside

the operator. In this case, <TT>STDIN</TT>.

Later, you'll use the diamond operator to read from other file

handles.

<P>

In this example, the diamond operator assigned the value of the

input string to <TT>$_ </TT>. Then,

the <TT>print()</TT> fuNCtion was

called with no parameters, which tells <TT>print()</TT>

to use <TT>$_</TT> as the default

parameter. Using the <TT>$_ </TT>

variable can save a lot of typing, but I'll let you decide which

is 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 that

the input file was finished. This caused the diamond operator

to return the undefined value which Perl equates to false and

caused the <TT>while</TT> loop to

end. In DOS (and therefore in all of the flavors of Windows),

26-the value of <TT>Ctrl+Z</TT>-is

considered to be the end-of-file indicator. As DOS reads or writes

a file, it monitors the data stream and when a value of 26 is

eNCountered the file is closed. UNIX does the same thing when

a value of 4-the value of <TT>Ctrl+D</TT>-is

read.<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 the

keyboard to being a file by changing the command line that you

use to execute Perl programs. Until now, you probably used a command

line similar to:

<BLOCKQUOTE>

<PRE>

perl -w 09lst01.pl

</PRE>

</BLOCKQUOTE>

<P>

In the previous example, Perl read the keyboard to get the standard

input. 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 standard

input. 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 input

to the 09LST01.PL file. You now have a program that duplicates

the fuNCtionality of the DOS type command. And it only took three

lines 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> character

will 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 continue

talking 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 will

examine the <TT>@ARGV</TT> special

variable. If <TT>@ARGV</TT> has no

elements, then the diamond operator will read from <TT>STDIN</TT>-either

from the keyboard or from a redirected file. So, if you wanted

to display the contents of more than one file, you could use the

program shown in Listing 9.2.

<HR>

<BLOCKQUOTE>

<B>Listing 9.2&nbsp;&nbsp;09LST02.PL-Read from Multiple Files

or 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;) {

⌨️ 快捷键说明

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