A file in perl can be opened by using the below code snippet:
The above code snippet opens the file in read mode. To open the file in read and append mode, following code snippet can be used:
# Open the file in read and write mode
# Write the logic to read the file here
# Move file position to end of file
seek(FH, 0, 2);
print FH "Appending to file";
Now, the question occurs why below code snippet doesn’t work:
Issue with the above code snippet is that it allows to append to the file, but does not allow to read the contents. This is because the file is opened for append, which automatically moves the file position to the end. So there is nothing to be read.
Hope you find the information presented here useful. Please leave your queries or feedback in the comments section below.