Classic usage sharing of Perl file reading

Time:2022-4-26

Open the file with the open() function

Common methods of opening files are:
 

Copy codeThe code is as follows:

open(FH, “< $filename”)
or die “Couldn’t open $filename for reading: $!”;

 

The open() function usually takes two parameters. The first parameter is the file handle, which is used to point to the open file. The second parameter is a mixture of the file name and mode (the opening mode of the file). If the file is successfully opened, the open() function returns true, otherwise it is false. We use “or” to test this condition.

The pattern in the above code is represented by the less than character (<). If the file does not exist, open () returns false. At this point, you can read the file handle, but not write.

Greater than characters indicate write. If the file does not exist, it will be created. If the file exists, the file is cleared and previous data will be lost. You can write file handles, but not read them.
 

Copy codeThe code is as follows:

#If the file does not exist, create it
open(FH, “> $filename”)
or die “Couldn’t open $filename for writing: $!”;

 

If the file does not exist, the add mode (represented by two greater than symbols) can be used to create a new file. If the file exists, the mode will not clear the original data.

Like < “or” read “mode, you can only write to file handles. (all writes are added to the end of the file). An attempt to read will result in a run error.
 

Copy codeThe code is as follows:

open(FH, “>> $filename”)
or die “Couldn’t open $filename for appending: $!”;

 

With “+ <” mode, you can read and write files. You can move inside the file through the tell() function and locate it through the seek() function. If the file does not exist, it will be created. If the file already exists, the original data will not be cleared.

If you plan to clear the contents of the original file, either call the truncate() function yourself, or use the “+ >” mode.
 

Copy codeThe code is as follows:

open(FH, “+> $filename”)
or die “Couldn’t open $filename for reading and writing: $!”;

 

Note the difference between “+ <” and “+ >”, both of which can be read and written. The former is non-destructive and the latter is destructive.

error

How did the error occur? Errors can occur in many places: for example, the directory does not exist, the file cannot be written, your program loses the file handle, and so on.

You should check the results of the system call (such as open() and sysopen()) to see if the call is successful.

In order to help users check errors, “or die()” is usually used. You should remember these usages. First, write out the information of system call failure (“open”). Secondly, the file name information should be written out to make it easier to locate when correcting errors. Third, write out the way to open the file (“for writing,” “for appending”). Fourth, output the error information of the operating system (included in $!). In this way, if a file cannot be opened, users using your program will generally know why it cannot be opened. Sometimes we combine the first and third:
or die “unable to append to $filename: $!”;

If you write the full name of the file in both open () and the error message, you risk changing open (), making the error message inappropriate or incorrect.
 

Copy codeThe code is as follows:

#A false error message will appear below
open(FH, “</var/run/file.pid”)
or die “Can’t open /var/log/file.pod for writing : $!”;

 

More control with sysopen()
To better control how files are opened, you can use the sysopen() function:
 

Copy codeThe code is as follows:

use Fcntl;
sysopen(FH, $filename, O_RDWR|O_CREAT, 0666)
or die “Can’t open $filename for reading/writing/creating : $!”;

 

The function sysopen() has four parameters. The first parameter is a file handle parameter similar to the open() function. The second parameter is a file name without mode information. The third parameter is a mode parameter, which is composed of constants combined by logical or operation provided by fcntl module. The fourth parameter (optional) is octal attribute value (0666 represents data file and 0777 represents program). Sysopen() returns true if the file can be opened, and false if the opening fails.

Unlike the open () function, sysopen () does not provide a short form of mode description, but combines some constants. Moreover, each mode constant has a unique meaning. They can be combined only through logical or operation. You can set the combination of multiple behaviors.

O_RDONLYRead-only
O_WRONLY Write-only
O_RDWR Reading and writing
O_APPEND Writes go to the end of the file
O_TRUNC Truncate the file if it existed
O_CREAT Create the file if it didn’t exist
O_EXCLError if the file already existed (used with O_CREAT)

When you need to be careful, use the sysopen() function. For example, if you plan to add content to a file, if the file does not exist and a new file is not created, you can write:
 

Copy codeThe code is as follows:

sysopen(LOG, “/var/log/myprog.log”, O_APPEND, 0666)
or die “Can’t open /var/log/myprog.log for appending: $!”;