Interview :: Perl
How will you open a file in read-only mode in Perl?
To open a file in read-only mode, '
How will you open a file in a write-only mode in Perl?
To open a file in write-only mode, '>' sign is used. The file you open will be emptied or truncated if it already exists if not, a new file will be created.
How to prevent file truncation in Perl?
Opening a file in write-only mode truncates data of the file. To prevent it, use sign '+>'. It will prevent your data, and you can append new data in the last of the file.
The '>>' sign opens a file with appending purpose. It places the pointer at the end of the file where you can add new data.
How to read a single line from a file in Perl?
Taking $row = as a variable will print a single line from the file
How to read multi lines from a file in Perl?
Taking $row = as a variable in a while loop will print all lines from the file.
How to close a file in Perl?
Closing a file in Perl is not mandatory. However, using close() function will disassociate the file handle from the corresponding file.
How to copy a file in Perl?
To copy the content of one file into another file, read all lines of the first file in a while loop and copy it in another file.
It is a symbolic link which links one file name to a new file name.
For example, in file1 -> file2, if we read file1, we will end up reading file2.
The tell function finds your position within a file. It is the first thing you need to do during file handling.