Open a file using the Open () function
Common methods to open a file are:
Copy codeThe Code is as follows: 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.
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 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.
Copy codeThe Code is as follows: 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.
Copy codeThe Code is as follows: 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.
Copy codeThe Code is as follows: # The following is a false error message.
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:
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 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:
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: $! ";