Perl file manipulation


Perl uses a variable called a file handle type to operate on files.

Reading or writing data from a file requires a file handle.

The file handle is the name of an I/O connection.

Perl provides three file handles: STDIN, STDOUT, and STDERR, which represent standard input, standard output, and standard error output respectively.

You can use the following methods to open a file in Perl:

open FILEHANDLE, EXPR
open FILEHANDLE

sysopen FILEHANDLE, FILENAME, MODE, PERMS
sysopen FILEHANDLE, FILENAME, MODE

Parameter description:

  • FILEHANDLE: file handle, used to store a unique file identifier .

  • EXPR: An expression composed of file name and file access type.

  • MODE: File access type.

  • PERMS: Access permission bits.


Open function

In the following code we use the open function to open the file file.txt in read-only mode (<):

open(DATA, "<file.txt");

< indicates read-only mode.

The DATA in the code is the file handle used to read the file. The following example will open the file and output the file content:

#!/usr/bin/perl

open(DATA, "<file.txt") or die "file.txt 文件无法打开, $!";

while(<DATA>){
   print "$_";
}

The following code opens the file for writing (>) file.txt:

open(DATA, ">file.txt") or die "file.txt 文件无法打开, $!";

> indicates the writing method.

If you need to open the file in read-write mode, you can add a + sign before the > or < characters:

open(DATA, "+<file.txt"); or die "file.txt 文件无法打开, $!";

This method will not delete the original content of the file. , the format is as follows:

open DATA, "+>file.txt" or die "file.txt 文件无法打开, $!";

If you want to append data to the file, you only need to open the file in append mode before appending the data:

open(DATA,">>file.txt") || die "file.txt 文件无法打开, $!";

>> means to Append data to the end of the existing file. If you need to read the content of the file to be appended, you can add a + sign:

open(DATA,"+>>file.txt") || die "file.txt 文件无法打开, $!";

The following table lists the different access modes:

##+< or r+Open in read-write mode and point the file pointer to the file header. ##+> or w+##+>> or a+ Open in read-write mode and point the file pointer to the end of the file. If the file does not exist, try to create it.

Sysopen function

sysopen The function is similar to the open function, except that their parameter forms are different.

The following example opens the file in read-write (+<filename) mode:

sysopen(DATA, "file.txt", O_RDWR);

If you need to clear the file before updating the file, the writing method is as follows:

sysopen(DATA, "file.txt", O_RDWR|O_TRUNC );

You can use O_CREAT to create a new file, O_WRONLY is write-only mode, O_RDONLY is read-only mode.

The PERMS parameter is an octal attribute value, indicating the permissions after the file is created. The default is 0x666.

The following table lists the possible mode values:

ModeDescription
< or rOpen in read-only mode, pointing the file pointer to the file header.
> or wOpen in writing mode, point the file pointer to the file header and truncate the file size to zero. If the file does not exist, try to create it.
>> or aOpen in writing mode and point the file pointer to the end of the file. If the file does not exist, try to create it.
Open in read-write mode, point the file pointer to the file header and truncate the file size to zero. If the file does not exist, try to create it.
Mode Description
O_RDWROpen in read-write mode and point the file pointer to the file header.
O_RDONLYOpen in read-only mode and point the file pointer to the file header.
O_WRONLYOpen in writing mode, point the file pointer to the file header and truncate the file size to zero. If the file does not exist, try to create it.
O_CREATCreate file
O_APPENDAppend file
O_TRUNCTruncate the file size to zero
O_EXCLIf the file exists when using O_CREAT, an error message will be returned. It can Test whether the file exists
O_NONBLOCK Non-blocking I/O allows our operation to either succeed or return an error immediately without being blocked.

Close function

After the file is used, the file must be closed to refresh the input and output buffer associated with the file handle. The syntax for closing the file is as follows:

close FILEHANDLE
close

FILEHANDLE is the specified file handle, and returns true if it is closed successfully.

close(DATA) || die "无法关闭文件";

Reading and writing files

There are several different ways to read and write information to files:

<FILEHANDL> Operator

The primary method of reading information from an open file handle is the <FILEHANDLE> operator. In a scalar context, it returns a single row from the file handle. For example:

#!/usr/bin/perl

print "php中文网网址?\n";
$name = <STDIN>;
print "网址:$name\n";

After the above program is executed, the following information will be displayed. After we enter the URL, the print statement will output:

When we use <FILEHANDLE> ; operator, it will return a list of each line in the file handle, for example we can import all lines into an array.

Implement creating the import.txt file with the following content:

$ cat import.txt 
1
2
3

Read import.txt and put each line into the @lines array:

#!/usr/bin/perl

open(DATA,"<import.txt") or die "无法打开数据";
@lines = <DATA>;
print @lines;    # 输出数组内容
close(DATA);

Execute the above program, The output result is:

1
2
3

getc function

xgetc function returns a single character from the specified FILEHANDLE, if not specified returns STDIN:

getc FILEHANDLE
getc

If an error occurs, or in the file If the handle is at the end of the file, undef is returned.


read function

The read function is used to read information from the file handle of the buffer.

This function is used to read binary data from a file.

read FILEHANDLE, SCALAR, LENGTH, OFFSET
read FILEHANDLE, SCALAR, LENGTH

Parameter description:

  • FILEHANDLE: File handle, used to store a unique file identifier.

  • SCALAR: Start reading position.

  • LENGTH: The length of the read content.

  • OFFSET: Offset.

Returns the number of bytes read if the read is successful, 0 if at the end of the file, and undef if an error occurs.

print function

For all functions that read information from file handles, the main writing function in the backend is print:

print FILEHANDLE LIST
print LIST
print

Use file handles and print functions The results of program execution can be sent to the output device (STDOUT: standard output), for example:

print "Hello World!\n";

File copy

In the following example we will open an existing file file1.txt and read Take each line and write it into the file file2.txt:

#!/usr/bin/perl

# 只读方式打开文件
open(DATA1, "<file1.txt");

# 打开新文件并写入
open(DATA2, ">file2.txt");

# 拷贝数据
while(<DATA1>)
{
   print DATA2 $_;
}
close( DATA1 );
close( DATA2 );

File renaming

In the following example, we rename the existing file file1.txt to file2.txt, specify The directory is under /usr/php/test/:

#!/usr/bin/perl

rename ("/usr/php/test/file1.txt", "/usr/php/test/file2.txt" );

Functionrenames only accepts two parameters and only renames existing files.

Delete files

The following example demonstrates how to use the unlink function to delete files:

#!/usr/bin/perl

unlink ("/usr/php/test/file1.txt");

Specify the file location

You can use the tell function to get the location of the file, and specify the location within the file by using the seek function:

tell function

tell function is used to get the file location:

tell FILEHANDLE
tell

If FILEHANDLE is specified, this function returns the position of the file pointer, in bytes. If not specified, returns the default selected file handle.

seek function

The seek() function reads or writes files by moving the file read-write pointer through the file handle, and reads and writes in bytes. :

seek FILEHANDLE, POSITION, WHENCE

Parameter description:

  • FILEHANDLE: File handle, used to store a unique file identifier.

  • POSITION: Indicates the number of bytes to be moved by the file handle (read and write position pointer).

  • WHENCE: Indicates the starting position when the file handle (read-write position pointer) starts to move. The possible values ​​are 0, 1, and 2; respectively representing the beginning of the file, the current position, and End of file.

The following example reads 256 bytes from the beginning of the file:

seek DATA, 256, 0;

File information

Perl's file operation can also be done first Test whether the file exists, whether it can be read and written, etc.

We can first create the file1.txt file, as follows:

$ cat file1.txt 
www.php.cn
#/usr/bin/perl

my $file = "/usr/test/php/file1.txt";
my (@description, $size);
if (-e $file)
{
	push @description, '是一个二进制文件' if (-B _);
	push @description, '是一个socket(套接字)' if (-S _);
	push @description, '是一个文本文件' if (-T _);
	push @description, '是一个特殊块文件' if (-b _);
	push @description, '是一个特殊字符文件' if (-c _);
	push @description, '是一个目录' if (-d _);
	push @description, '文件存在' if (-x _);
	push @description, (($size = -s _)) ? "$size 字节" : '空';
	print "$file 信息:", join(', ',@description),"\n";
}

Execute the above program, the output result is:

file1.txt 信息:是一个文本文件, 15 字节

The file test operator is shown in the following table :

OperatorDescription
-AThe time when the file was last accessed (unit: Day)
-B is the (inode) index of the binary file
-C Node modification time (unit: days)
-MThe time when the file was last modified (unit: days)
-OFile owned by real UID
-RFile or directory can be read by real UID/GID
-S is a socket
-Tis a text file
-WFiles or directories can be written to with real UID/GID
-X Files or directories can be executed with real UID/GID
-b as block-special (special block) files (such as mounted disks)
-c is character-special (special character) files (such as I/O devices)
-d is Directory
-eThe file or directory name exists
-f is an ordinary file
-gThe file or directory has the setgid attribute
-kThe file or directory has the sticky bit set
-l is a symbolic link
-oThe file is owned by a valid UID
-pThe file is a named pipe (FIFO)
-rThe file can be a valid UID /GID read
-sThe file or directory exists and is not 0 (returns the number of bytes)
-tThe file handle is TTY (return result of system function isatty(); this test cannot be used for file names)
-u File or directory has setuid attribute
-wFile can be written with a valid UID/GID
-xThe file can be executed by a valid UID/GID
-zThe file exists and the size is 0 (the directory is always false), that is, whether it is Empty file,