Perl 简明教程
Perl - File I/O
文件处理的基本原理很简单:将 filehandle 与一个外部链接(通常是一个文件)关联起来,然后使用 Perl 中的各种操作符和函数在与文件句柄关联的数据流中读取和更新存储的数据。
The basics of handling files are simple: you associate a filehandle with an external entity (usually a file) and then use a variety of operators and functions within Perl to read and update the data stored within the data stream associated with the filehandle.
文件句柄是关联物理文件和名称的已命名的 Perl 内部结构。所有文件句柄都具有读/写访问权限,因此你可以读取和更新与文件句柄关联的任何文件或设备。但是,当你关联一个文件句柄时,你可以指定文件句柄的打开模式。
A filehandle is a named internal Perl structure that associates a physical file with a name. All filehandles are capable of read/write access, so you can read from and update any file or device associated with a filehandle. However, when you associate a filehandle, you can specify the mode in which the filehandle is opened.
三个基本文件句柄是 - STDIN 、 STDOUT 和 STDERR, ,它们分别代表标准输入、标准输出和标准错误设备。
Three basic file handles are - STDIN, STDOUT, and STDERR, which represent standard input, standard output and standard error devices respectively.
Opening and Closing Files
有两个带有多种形式的基本函数,可用于在 Perl 中打开任何新文件或现有文件。
There are following two functions with multiple forms, which can be used to open any new or existing file in Perl.
open FILEHANDLE, EXPR
open FILEHANDLE
sysopen FILEHANDLE, FILENAME, MODE, PERMS
sysopen FILEHANDLE, FILENAME, MODE
此处的 FILEHANDLE 是由 open 函数返回的文件句柄,EXPR 是具有文件名和文件打开模式的表达式。
Here FILEHANDLE is the file handle returned by the open function and EXPR is the expression having file name and mode of opening the file.
Open Function
以下是在只读模式下打开 file.txt 的语法。此处的小于 < 符号表示该文件必须在只读模式下打开。
Following is the syntax to open file.txt in read-only mode. Here less than < sign indicates that file has to be opend in read-only mode.
open(DATA, "<file.txt");
这里 DATA 是文件句柄,其将用于读文件。这里有一个示例,它将打开一个文件并在屏幕上打印其内容。
Here DATA is the file handle, which will be used to read the file. Here is the example, which will open a file and will print its content over the screen.
#!/usr/bin/perl
open(DATA, "<file.txt") or die "Couldn't open file file.txt, $!";
while(<DATA>) {
print "$_";
}
以下是在写入模式下打开 file.txt 的语法。此处的小于 > 符号表示该文件必须在写入模式下打开。
Following is the syntax to open file.txt in writing mode. Here less than > sign indicates that file has to be opend in the writing mode.
open(DATA, ">file.txt") or die "Couldn't open file file.txt, $!";
此示例实际上在打开文件进行写入之前截短(清空)该文件,但这可能不是预期的效果。如果想要以读写方式打开文件,你可以在 > 或 < 字符之前加上一个加号。
This example actually truncates (empties) the file before opening it for writing, which may not be the desired effect. If you want to open a file for reading and writing, you can put a plus sign before the > or < characters.
例如,要在不截断文件的情况下以更新模式打开文件 −
For example, to open a file for updating without truncating it −
open(DATA, "+<file.txt"); or die "Couldn't open file file.txt, $!";
要先截断文件 −
To truncate the file first −
open DATA, "+>file.txt" or die "Couldn't open file file.txt, $!";
你可以以追加模式打开文件。在此模式下,写入点将设置到文件的末尾。
You can open a file in the append mode. In this mode, writing point will be set to the end of the file.
open(DATA,">>file.txt") || die "Couldn't open file file.txt, $!";
双 >> 以追加方式打开文件,将文件指针放在末尾,这样你就可以立即开始追加信息。但是,除非你在它的前面也加上一个加号,否则你无法从中读取 −
A double >> opens the file for appending, placing the file pointer at the end, so that you can immediately start appending information. However, you can’t read from it unless you also place a plus sign in front of it −
open(DATA,"+>>file.txt") || die "Couldn't open file file.txt, $!";
下表给出了不同模式的可能值
Following is the table, which gives the possible values of different modes
Sr.No. |
Entities & Definition |
1 |
< or r Read Only Access |
2 |
> or w Creates, Writes, and Truncates |
3 |
>> or a Writes, Appends, and Creates |
4 |
< or r Reads and Writes |
5 |
> or w Reads, Writes, Creates, and Truncates |
6 |
>> or a Reads, Writes, Appends, and Creates |
Sysopen Function
sysopen 函数类似于主要 open 函数,不同之处在于它使用系统 open() 函数,使用提供给它的参数作为系统函数的参数 −
The sysopen function is similar to the main open function, except that it uses the system open() function, using the parameters supplied to it as the parameters for the system function −
例如,要打开一个文件进行更新,模拟 open 中的 +<filename 格式 −
For example, to open a file for updating, emulating the +<filename format from open −
sysopen(DATA, "file.txt", O_RDWR);
或在更新前截断文件 −
Or to truncate the file before updating −
sysopen(DATA, "file.txt", O_RDWR|O_TRUNC );
你可以使用 O_CREAT 来创建一个新文件,使用 O_WRONLY 以只写模式打开文件,使用 O_RDONLY 以只读模式打开文件。
You can use O_CREAT to create a new file and O_WRONLY- to open file in write only mode and O_RDONLY - to open file in read only mode.
PERMS 参数指定指定的文件的文件权限(如果必须创建)。默认情况下,它使用 0x666 。
The PERMS argument specifies the file permissions for the file specified, if it has to be created. By default it takes 0x666.
以下是给出了 MODE 可能值的表格。
Following is the table, which gives the possible values of MODE.
Sr.No. |
Entities & Definition |
1 |
O_RDWR Read and Write |
2 |
O_RDONLY Read Only |
3 |
O_WRONLY Write Only |
4 |
O_CREAT Create the file |
5 |
O_APPEND Append the file |
6 |
O_TRUNC Truncate the file |
7 |
O_EXCL Stops if file already exists |
8 |
O_NONBLOCK Non-Blocking usability |
Close Function
要关闭文件句柄,因此取消文件句柄与相应文件之间的关联,可以使用 close 函数。这将刷新文件句柄的缓冲区并关闭系统的文件描述符。
To close a filehandle, and therefore disassociate the filehandle from the corresponding file, you use the close function. This flushes the filehandle’s buffers and closes the system’s file descriptor.
close FILEHANDLE
close
如果未指定 FILEHANDLE,则关闭当前选中的 filehandle。仅当成功刷新缓冲区并关闭文件时才返回 true。
If no FILEHANDLE is specified, then it closes the currently selected filehandle. It returns true only if it could successfully flush the buffers and close the file.
close(DATA) || die "Couldn't close file properly";
Reading and Writing Files
一旦拥有打开的 filehandle,您就需要能够读取和写入信息。有许多不同的方法可以将数据读写到文件中。
Once you have an open filehandle, you need to be able to read and write information. There are a number of different ways of reading and writing data into the file.
The <FILEHANDL> Operator
从打开的 filehandle 中读取信息的的主要方法是 <FILEHANDLE> 运算符。在标量上下文中,它从 filehandle 返回单行。例如:
The main method of reading the information from an open filehandle is the <FILEHANDLE> operator. In a scalar context, it returns a single line from the filehandle. For example −
#!/usr/bin/perl
print "What is your name?\n";
$name = <STDIN>;
print "Hello $name\n";
在列表上下文中使用 <FILEHANDLE> 运算符时,它返回来自指定 filehandle 的行列表。例如,将文件中的所有行导入数组:
When you use the <FILEHANDLE> operator in a list context, it returns a list of lines from the specified filehandle. For example, to import all the lines from a file into an array −
#!/usr/bin/perl
open(DATA,"<import.txt") or die "Can't open data";
@lines = <DATA>;
close(DATA);
getc Function
getc 函数从指定的 FILEHANDLE 返回单个字符,如果未指定,则返回 STDIN:
The getc function returns a single character from the specified FILEHANDLE, or STDIN if none is specified −
getc FILEHANDLE
getc
如果有错误或文件句柄处于文件末尾,则返回 undef。
If there was an error, or the filehandle is at end of file, then undef is returned instead.
read Function
read 函数从缓冲的 filehandle 中读取信息块:此函数用于从文件中读取二进制数据。
The read function reads a block of information from the buffered filehandle: This function is used to read binary data from the file.
read FILEHANDLE, SCALAR, LENGTH, OFFSET
read FILEHANDLE, SCALAR, LENGTH
读取的数据长度由 LENGTH 定义,如果未指定 OFFSET,则数据放置在 SCALAR 的开头。否则,数据在 SCALAR 中的 OFFSET 字节后放置。该函数在成功时返回读取的字节数,在到达文件末尾时返回零,在发生错误时返回 undef。
The length of the data read is defined by LENGTH, and the data is placed at the start of SCALAR if no OFFSET is specified. Otherwise data is placed after OFFSET bytes in SCALAR. The function returns the number of bytes read on success, zero at end of file, or undef if there was an error.
print Function
对于从 filehandle 读取信息的所有不同方法,将信息写回的主要函数是 print 函数。
For all the different methods used for reading information from filehandles, the main function for writing information back is the print function.
print FILEHANDLE LIST
print LIST
print
print 函数将 LIST 的评估值打印到 FILEHANDLE,或打印到当前输出 filehandle(默认情况下为 STDOUT)。例如:
The print function prints the evaluated value of LIST to FILEHANDLE, or to the current output filehandle (STDOUT by default). For example −
print "Hello World!\n";
Copying Files
下面是该示例,它打开了一个现有的文件 file1.txt,逐行读取该文件并生成另一个副本文件 file2.txt。
Here is the example, which opens an existing file file1.txt and read it line by line and generate another copy file file2.txt.
#!/usr/bin/perl
# Open file to read
open(DATA1, "<file1.txt");
# Open new file to write
open(DATA2, ">file2.txt");
# Copy data from one file to another.
while(<DATA1>) {
print DATA2 $_;
}
close( DATA1 );
close( DATA2 );
Renaming a file
下面是一个示例,它展示了如何将文件 file1.txt 重命名为 file2.txt。假设文件位于 /usr/test 目录中。
Here is an example, which shows how we can rename a file file1.txt to file2.txt. Assuming file is available in /usr/test directory.
#!/usr/bin/perl
rename ("/usr/test/file1.txt", "/usr/test/file2.txt" );
此函数 renames 采用两个参数,它只是重命名现有文件。
This function renames takes two arguments and it just renames the existing file.
Deleting an Existing File
下面是一个示例,它展示了如何使用 unlink 函数删除文件 file1.txt。
Here is an example, which shows how to delete a file file1.txt using the unlink function.
#!/usr/bin/perl
unlink ("/usr/test/file1.txt");
Positioning inside a File
您可以使用 tell 函数来了解文件的当前位置,并使用 seek 函数来指向文件中的特定位置。
You can use to tell function to know the current position of a file and seek function to point a particular position inside the file.
tell Function
第一个要求是找到您在文件中的位置,您可以使用 tell 函数来实现:
The first requirement is to find your position within a file, which you do using the tell function −
tell FILEHANDLE
tell
如果指定了 FILEHANDLE,则此函数返回 FILEHANDLE 中文件指针的位置(以字节为单位),如果没有指定,则返回当前默认选定的 filehandle。
This returns the position of the file pointer, in bytes, within FILEHANDLE if specified, or the current default selected filehandle if none is specified.
seek Function
seek 函数将文件指针定位到文件中的指定字节数:
The seek function positions the file pointer to the specified number of bytes within a file −
seek FILEHANDLE, POSITION, WHENCE
该函数使用 fseek 系统函数,您可以相对于三个不同点进行定位:开始、结束和当前位置。您可以通过为 WHENCE 指定一个值来实现。
The function uses the fseek system function, and you have the same ability to position relative to three different points: the start, the end, and the current position. You do this by specifying a value for WHENCE.
零将定位设置为相对于文件开始的位置。例如,该行将文件指针设置为文件中第 256 个字节。
Zero sets the positioning relative to the start of the file. For example, the line sets the file pointer to the 256th byte in the file.
seek DATA, 256, 0;
File Information
你可以使用一组名为 -X 测试的测试操作符,在 Perl 中非常快地测试某些功能。例如,要对文件上的各种权限进行快速测试,你可以使用类似这样的脚本:
You can test certain features very quickly within Perl using a series of test operators known collectively as -X tests. For example, to perform a quick test of the various permissions on a file, you might use a script like this −
#/usr/bin/perl
my $file = "/usr/test/file1.txt";
my (@description, $size);
if (-e $file) {
push @description, 'binary' if (-B _);
push @description, 'a socket' if (-S _);
push @description, 'a text file' if (-T _);
push @description, 'a block special file' if (-b _);
push @description, 'a character special file' if (-c _);
push @description, 'a directory' if (-d _);
push @description, 'executable' if (-x _);
push @description, (($size = -s _)) ? "$size bytes" : 'empty';
print "$file is ", join(', ',@description),"\n";
}
这里是一些功能的列表,你可以针对文件或目录检查这些功能:
Here is the list of features, which you can check for a file or directory −
Sr.No. |
Operator & Definition |
1 |
-A Script start time minus file last access time, in days. |
2 |
-B Is it a binary file? |
3 |
-C Script start time minus file last inode change time, in days. |
3 |
-M Script start time minus file modification time, in days. |
4 |
-O Is the file owned by the real user ID? |
5 |
-R Is the file readable by the real user ID or real group? |
6 |
-S Is the file a socket? |
7 |
-T Is it a text file? |
8 |
-W Is the file writable by the real user ID or real group? |
9 |
-X Is the file executable by the real user ID or real group? |
10 |
-b Is it a block special file? |
11 |
-c Is it a character special file? |
12 |
-d Is the file a directory? |
13 |
-e Does the file exist? |
14 |
-f Is it a plain file? |
15 |
-g Does the file have the setgid bit set? |
16 |
-k Does the file have the sticky bit set? |
17 |
-l Is the file a symbolic link? |
18 |
-o Is the file owned by the effective user ID? |
19 |
-p Is the file a named pipe? |
20 |
-r Is the file readable by the effective user or group ID? |
21 |
-s Returns the size of the file, zero size = empty file. |
22 |
-t Is the filehandle opened by a TTY (terminal)? |
23 |
-u Does the file have the setuid bit set? |
24 |
-w Is the file writable by the effective user or group ID? |
25 |
-x Is the file executable by the effective user or group ID? |
26 |
-z Is the file size zero? |