Open a file using the open () function
Common methods to open a file are:
Open (FH, "<$ FILENAME ")
Or die "couldn't open $ filename for reading: $! ";
The open () function usually has two parameters. The first is the file handle, which is used to point to the opened file. The second parameter is a mixture of the file name and mode (File opening mode, if the file is successfully opened, the open () function returns true; otherwise, the value is false. We use "or" to test this condition.
The mode in the above Code is represented by a character smaller than (<. If the file does not exist, open () returns false. In this case, you can read the file handle but cannot write it.
A value greater than a character indicates writing. If the file does not exist, it will be created. If the file exists and the file is cleared, the previous data will be lost. You can write a file handle, but cannot read it.
# 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 can be used to create a new file. If the file exists, the mode does not clear the original data.
Like the "<" or "read" mode, you can only write the file handle. (Therefore, all the written content is added to the end of the file ). An error occurs when you attempt to perform a read operation.
Open (FH, "> $ FILENAME ")
Or die "couldn't open $ filename for appending: $! ";
In the "+ <" mode, you can read and write files. You can use the tell () function to move inside the file 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 want to clear the original file content, or call the truncate () function, or use the "+>" mode.
Open (FH, "+> $ FILENAME ")
Or die "couldn't open $ filename for reading and writing: $! ";
Note the differences between "+ <" and "+>". Both can be readable and writable. The former is non-destructive writing, and the latter is destructive writing.
Error
How does an error occur? Errors may occur in many places: for example, the directory does not exist, the file cannot be written, and your program loses the file handle.
You should check the system call results (such as open () and sysopen () to see if the call is successful.
"Or die ()" is usually used to help users identify errors. Remember these usage. First, the system call failure ("open") Information should be written. Second, the file name information should be written so that it is easier to locate errors when correcting them. Third, write out the method for opening the file ("for writing," "for appending "). Fourth, output the error information of the Operating System (included in $! ). In this way, once the file cannot be opened, the user using your program will generally know why it cannot be opened. Sometimes we merge the first and third objects:
Or die "unable to append to $ filename: $! ";
If you write the full name of the file in both open () and error messages, you may be at risk of changing open (), making the error information inappropriate or incorrect.
# The following error message is false.
Open (FH, "</var/run/file. PID ")
Or die "can't open/var/log/file. pod for writing: $! ";
Use sysopen () for more control
To better control the file opening method, you can use the sysopen () function:
Use fcntl;
Sysopen (FH, $ filename, o_rdwr | o_creat, 0666)
Or die "can't open $ filename for reading/writing/creating: $! ";
The sysopen () function has four parameters. The first parameter is a file handle parameter similar to the open () function, the second parameter is the file name without the mode information, and the third parameter is the mode parameter, A constant composed of logical or operations provided by the fcntl module. The fourth parameter (optional) is the octal value (0666 indicates the data file, and 0777 indicates the program ). If the file can be opened, sysopen () returns true. If the file fails to be opened, false is returned.
Different from the open () function, sysopen () does not provide a short description of the pattern. Instead, it combines some constants, and each pattern constant has a unique meaning, only logical or operations can combine them. You can set a combination of multiple actions.
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 and you do not create a new file, you can write it like this:
Sysopen (log, "/var/log/myprog. log", o_append, 0666)
Or die "can't open/var/log/myprog. log for appending: $! ";