Open the file with the open () function
The common ways to open files are:
Copy Code code as follows:
Open (FH, "< $filename")
Or die "couldn ' t open $filename for reading: $!";
The open () function usually comes with two arguments, the first is a file handle, it is used to point to an open file, the second parameter is a mixture of the file name and the mode (open mode of the file), or False if the file is opened successfully, the open () function returns True. We use "or" to test the condition.
The pattern in the above code is represented by a small Yu Gifu (<). If the file does not exist, open () returns false. At this point, you can read the file handle, but not write.
is greater than the character representation write. If the file does not exist, it will be created. If the file exists and the file is purged, the previous data will be lost. You can write a file handle, but you can't read it.
Copy Code code as follows:
# Create a file if it doesn't exist
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, and if the file exists, the pattern does not erase the original data.
As with "<" or "read" mode, you can write to a file handle only. (So the write content is added to the end of the file). An attempt to read will result in a run-time error.
Copy Code code as follows:
Open (FH, ">> $filename")
Or die "couldn ' t open $filename for appending: $!";
Through "+<" mode, you can both read and write files. You can move within a file through the tell () function and navigate 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 purged.
If you intend to clear the original file contents, or call the truncate () function yourself, or use the "+>" mode.
Copy Code code as follows:
Open (FH, "+> $filename")
Or die "couldn ' t open $filename for reading and writing: $!";
Notice the difference between "+<" and "+>", both of which can be read and writable. The former is non-destructive writing, the latter is destructive writing.
Error
How did the error come about? Errors can occur in many places: directories do not exist, files are not writable, your program loses file handles, and so on.
You should check the results of system calls (such as open () and Sysopen ()) to see if the call succeeds.
To help users with error checking, you should always use the "or Die ()" to remember these usages. First, you should write out the information for the system call failure ("open"). Second, you should write information about the filename so that you can easily locate it when correcting the error. Third, write the way to open the file ("for writing," "for appending"). The output operating system error information (included in the $!). This way, once the file doesn't open, the user who uses your program will generally know why it can't be turned on. Sometimes we combine the first with the third:
Or die "Unable to append to $filename: $!";
If you write the full name of the file in both open () and error messages, you risk changing the open () so that the error message is outdated or incorrect.
Copy Code code 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: $!";
Use Sysopen () for more control
For better control of how files are opened, you can use the Sysopen () function:
Copy Code code as follows:
Use Fcntl;
Sysopen (FH, $filename, o_rdwr| O_creat, 0666)
Or die "Can ' t open $filename for reading/writing/creating: $!";
function Sysopen () with four parameters, the first is a file handle parameter similar to the open () function, the second parameter is a filename without schema information, and the third parameter is a pattern parameter consisting of a constant composed of the logical or operations provided by the Fcntl module, the fourth argument (optional), is an octal attribute value (0666 represents a data file, 0777 represents a program). If the file can be opened, Sysopen () returns True and False if the open fails.
Unlike the open () function, Sysopen () does not provide a shorthand for the pattern description, but rather combines some constants, and each pattern constant has a unique meaning, which can be combined only by logical or operations, and allows you to set up a 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 want to add content to a file, if the file does not exist, do not create a new file, you can write this:
Copy Code code as follows:
Sysopen (LOG, "/var/log/myprog.log", O_append, 0666)
Or die "Can ' t open/var/log/myprog.log for appending: $!";