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

📄 ch9.htm

📁 prrl 5 programs codes in the book
💻 HTM
📖 第 1 页 / 共 5 页
字号:
This program displays:<BR>

<BLOCKQUOTE>

<PRE>

30 31 0d 0a

30 32 0d 0a

30 33 0d 0a

</PRE>

</BLOCKQUOTE>

<P>

When the file is read in binary mode, you can see that there are

really two characters at the end of every line-the linefeed and

newline characters.

<P>

Our next example will look at the end-of-file character in both

text and binary modes. We'll use a data file called <TT>EOF.DAT</TT>

with the following contents:

<BLOCKQUOTE>

<PRE>

01

02

&lt;end of file character&gt;03

</PRE>

</BLOCKQUOTE>

<P>

SiNCe the end-of-file character is a non-printing character, it

can't be shown directly. In the spot <TT>&lt;end

of file character&gt;</TT> above is really the value 26.

<P>

Here is the program that you saw previously read the BINARY.DAT

file, only this time, it will read EOF.DAT.

<P>

<IMG SRC="pseudo.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/pseudo.gif" BORDER=1 ALIGN=RIGHT><p>

<BLOCKQUOTE>

<I>Initialize a buffer variable.<BR>

Open the </I><TT><I>BINARY.DAT</I></TT><I>

file for reading.<BR>

Read the first 20 characters of the file using the </I><TT><I>read()</I></TT><I>

fuNCtion.<BR>

Close the file.<BR>

Create an array of out of the characters in the </I><TT><I>$buffer</I></TT><I>

variable and iterate over that array using a </I><TT><I>foreach</I></TT><I>

loop.<BR>

Print the value of the current array element in hexadecimal format.

<BR>

Print a newline character. The current array element is a newline

character.</I>

</BLOCKQUOTE>

<HR>

<BLOCKQUOTE>

<B>Listing 9.10&nbsp;&nbsp;09LST10.PL-Reading a File to Show the

Text Mode End-of-File Character<BR>

</B>

</BLOCKQUOTE>

<BLOCKQUOTE>

<PRE>

$buffer = &quot;&quot;;



open(FILE, &quot;&lt;eof.dat&quot;);

read(FILE, $buffer, 20, 0);

close(FILE);



foreach (split(//, $buffer)) {

    printf(&quot;%02x &quot;, ord($_));

    print &quot;\n&quot; if $_ eq &quot;\n&quot;;

}

</PRE>

</BLOCKQUOTE>

<HR>

<P>

This program displays:

<BLOCKQUOTE>

<PRE>

30 31 0d 0a

30 32 0d 0a

</PRE>

</BLOCKQUOTE>

<P>

The end-of-file character prevents the read() fuNCtion from reading

the third line. If the file is placed into binary mode, the whole

file can be read.

<P>

<IMG SRC="pseudo.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/pseudo.gif" BORDER=1 ALIGN=RIGHT><p>

<BLOCKQUOTE>

<I>Initialize a buffer variable.<BR>

Open the </I><TT><I>BINARY.DAT</I></TT><I>

file for reading.<BR>

Change the mode to binary.<BR>

Read the first 20 characters of the file using the </I><TT><I>read()</I></TT><I>

fuNCtion.<BR>

Close the file.<BR>

Create an array of out of the characters in the </I><TT><I>$buffer</I></TT><I>

variable and iterate over that array using a </I><TT><I>foreach</I></TT><I>

loop.<BR>

Print the value of the current array element in hexadecimal format.

<BR>

Print a newline character. The current array element is a newline

character.</I>

</BLOCKQUOTE>

<HR>

<BLOCKQUOTE>

<B>Listing 9.11&nbsp;&nbsp;09LST11.PL-Reading a File to Show that

Binary Mode Does Not Recognize the End-of-File Character<BR>

</B>

</BLOCKQUOTE>

<BLOCKQUOTE>

<PRE>

$buffer = &quot;&quot;;



open(FILE, &quot;&lt;eof.dat&quot;);

binmode(FILE);

read(FILE, $buffer, 20, 0);

close(FILE);



foreach (split(//, $buffer)) {

    printf(&quot;%02x &quot;, ord($_));

    print &quot;\n&quot; if $_ eq &quot;\n&quot;;

}

</PRE>

</BLOCKQUOTE>

<HR>

<P>

This program displays:

<BLOCKQUOTE>

<PRE>

30 31 0d 0a

30 32 0d 0a

1a 30 33 0d 0a

</PRE>

</BLOCKQUOTE>

<P>

With binary mode on, bytes with a value of 26 have no special

meaning and the third line can be read. You see that the value

26-33 in hexadecimal-was printed along with the rest of the characters.

<P>

<TT>Example: Reading into a Hash</TT>

<P>

You've already seen that you can read a file directly into a regular

array using this syntax:

<BLOCKQUOTE>

<PRE>

@array = &lt;FILE_HANDLE&gt;;

</PRE>

</BLOCKQUOTE>

<P>

Unfortunately, there is no similar way to read an entire file

into a hash. But, it's still pretty easy to do. The following

example will use the line number as the hash key for each line

of a file.

<P>

<IMG SRC="pseudo.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/pseudo.gif" BORDER=1 ALIGN=RIGHT><p>

<BLOCKQUOTE>

<I>Open the </I><TT><I>FIXED.DAT</I></TT><I>

file for reading.<BR>

For each line of </I><TT><I>FIXED.DAT </I></TT><I>create

a hash element using the record number special variable (</I><TT><I>$.</I></TT><I>)

as the key and the line of input (</I><TT><I>$_</I></TT><I>)

as the value.<BR>

Close the file.<BR>

Iterate over the keys of the hash.<BR>

Print each key, value pair.</I>

</BLOCKQUOTE>

<HR>

<BLOCKQUOTE>

<B>Listing 9.12&nbsp;&nbsp;09LST12.PL-Reading a Fixed Length Record

with Fixed Length Fields into a Hash<BR>

</B>

</BLOCKQUOTE>

<BLOCKQUOTE>

<PRE>

open(FILE, &quot;&lt;fixed.dat&quot;);

while (&lt;FILE&gt;) {

    $hash{$.} = $_;

}

close(FILE);



foreach (keys %hash) {

    print(&quot;$_: $hash{$_}&quot;);

}

</PRE>

</BLOCKQUOTE>

<HR>

<P>

This program displays:<BR>

<BLOCKQUOTE>

<PRE>

1: 1212Jan       Jaspree             Painter

2: 3453Kelly     Horton              Jockey

</PRE>

</BLOCKQUOTE>

<H3><A NAME="ExampleGettingFileStatistics">

Example: Getting File Statistics</A></H3>

<P>

The file test operators can tell you a lot about a file, but sometimes

you need more. In those cases, you use the <TT>stat()</TT>

or <TT>lstat()</TT> fuNCtion. The

<TT>stat()</TT> returns file information

in a 13-element array. You can pass either a file handle or a

file name as the parameter. If the file can't be found or another

error occurs, the null list is returned. Listing 9.13 shows how

to use the <TT>stat()</TT> fuNCtion

to find out information about the <TT>EOF.DAT</TT>

file used earlier in the chapter.

<P>

<IMG SRC="pseudo.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/pseudo.gif" BORDER=1 ALIGN=RIGHT><p>

<BLOCKQUOTE>

<I>Assign the return list from the </I><TT><I>stat()</I></TT><I>

fuNCtion to 13 scalar variables.<BR>

Print the scalar values.</I>

</BLOCKQUOTE>

<HR>

<BLOCKQUOTE>

<B>Listing 9.13&nbsp;&nbsp;09LST13.PL-Using the </B><TT><I><B><FONT FACE="Courier">stat()</FONT></B></I></TT><B>

FuNCtion<BR>

</B>

</BLOCKQUOTE>

<BLOCKQUOTE>

<PRE>

($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size,

    $atime, $mtime, $ctime, $blksize, $blocks) = stat(&quot;eof.dat&quot;);



print(&quot;dev     = $dev\n&quot;);

print(&quot;ino     = $ino\n&quot;);

print(&quot;mode    = $mode\n&quot;);

print(&quot;nlink   = $nlink\n&quot;);

print(&quot;uid     = $uid\n&quot;);

print(&quot;gid     = $gid\n&quot;);

print(&quot;rdev    = $rdev\n&quot;);

print(&quot;size    = $size\n&quot;);

print(&quot;atime   = $atime\n&quot;);

print(&quot;mtime   = $mtime\n&quot;);

print(&quot;ctime   = $ctime\n&quot;);

print(&quot;blksize = $blksize\n&quot;);

print(&quot;blocks  = $blocks\n&quot;);

</PRE>

</BLOCKQUOTE>

<HR>

<P>

In the DOS environment, this program displays:

<BLOCKQUOTE>

<PRE>

dev     = 2

ino     = 0

mode    = 33206

nlink   = 1

uid     = 0

gid     = 0

rdev    = 2

size    = 13

atime   = 833137200

mtime   = 833195316

ctime   = 833194411

blksize =

blocks  =

</PRE>

</BLOCKQUOTE>

<P>

Some of this information is specific to the UNIX environment and

is beyond the scope of this book. For more information on this

topic, see Que's 1994 edition of <I>Using Unix</I>. One interesting

piece of information is the <TT>$mtime</TT>

value-the date and time of the last modification made to the file.

You can interpret this value by using the following line of code:

<BLOCKQUOTE>

<PRE>

($sec, $min, $hr, $day, $month, $year, $day_Of_Week, 

    $julianDate, $dst) = localtime($mtime);

</PRE>

</BLOCKQUOTE>

<P>

If you are only interested in the modification date, you can use

the array slice notation to just grab that value from the 13-element

array returned by <TT>stat()</TT>.

For example:

<BLOCKQUOTE>

<PRE>

$mtime = (stat(&quot;eof.dat&quot;))[9];

</PRE>

</BLOCKQUOTE>

<P>

Notice that the <TT>stat()</TT> fuNCtion

is surrounded by parentheses so that the return value is evaluated

in an array context. Then the tenth element is assigned to <TT>$mtime</TT>.

You can use this technique whenever a fuNCtion returns a list.

<H3><A NAME="ExampleUsingtheDirectoryFuNCtions">

Example: Using the Directory FuNCtions</A></H3>

<P>

Perl has several fuNCtions that let you work with directories.

You can make a directory with the <TT>mkdir()</TT>

fuNCtion. You can delete a directory with the <TT>rmdir()</TT>

fuNCtion. Switching from the current directory to another is done

using the <TT>chdir()</TT> fuNCtion.

<P>

Finding out which files are in a directory is done with the <TT>opendir()</TT>,

<TT>readdir()</TT>, and <TT>closedir()</TT>

fuNCtions. The next example will show you how to create a list

of all Perl programs in the current directory-well, at least those

files that end with the pl extension.

<P>

<IMG SRC="pseudo.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/pseudo.gif" BORDER=1 ALIGN=RIGHT><p>

<BLOCKQUOTE>

<I>Open the current directory using </I><TT><I>DIR</I></TT><I>

as the directory handle.<BR>

Read a list of file names using the </I><TT><I>readdir()</I></TT><I>

fuNCtion; extract only those that end in </I><TT><I>pl</I></TT><I>;

and the sorted list. The sorted list is assigned to the </I><TT><I>@files</I></TT><I>

array variable.<BR>

Close the directory.<BR>

Print the file names from the </I><TT><I>@files</I></TT><I>

array unless the file is a directory.</I>

</BLOCKQUOTE>

<HR>

<BLOCKQUOTE>

<B>Listing 9.14&nbsp;&nbsp;09LST14.PL-Print All Files in the Current

Directory Whose Name Ends in PL<BR>

</B>

</BLOCKQUOTE>

<BLOCKQUOTE

⌨️ 快捷键说明

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