Understanding Perl File Handling: open, read, write and close files with examples
Opening a file in Perl
Opening a file in Perl is simple and straightforward
To open a file we are using open method as show in following example TESTHANDLE is the file handle which will be used to read the file.
Opening a Perl File Handle reference in Normal Scalar Variable.
TESTHANDLE is the file handle returned by open function. In above example we are using < sign which is for opening a file in read mode. There are few other mode in which you can read or write the file, they are as follows :
|< or r||Read Only Access|
|> or w||For creation, writing, and Truncation|
|>> or a||For Writing Appends, and Creates|
|+< or r+||For Reading and Writing|
|+> or w+||For Reading , Writing, Creating, and Truncation||+>> or a+||For Reading , Writing, Appending, and Creating|
Similar to open function Perl offers a sysopen function to open a file. In sysopen function it uses systems open() function due to that it requires different command as shown in example below
sysopen(DATA, "file.txt", O_RDWR);
this is used to open a file in read write mode.
Below is the table of commands we need to pass in order to manipulate the file.
|O_RDWR||Read and Write|
|O_CREAT||Create the file|
|O_APPEND||Append the file|
|O_TRUNC||Truncate the file|
|O_EXCL||Stops if file already exists|
close function is to close the file handle or disassociate the filehandle from the file. There are two main purposes of close function
1. Flush the buffers of fileHandle
2. To close the system's file descriptor.
Reading contents from File
Reading contents from the file is also a very easy task. Lets see how we can read the contents of file using File Handle.
Reading contents of file using filehandle
open TESTHANDLE,"less thantest.txt";
Here is above example we have taken a line in a file using scalar variable with the help of TESTHANDLE
Similar to above example we can get the list of line using list context as follows
open TESTHANDLE,"less than test.txt";
@lines = less thanTESTHANDLEgreater than;
In above example TESTHANDLE operator returns list of lines which we are collecting in @lines.
Using getc function we can get the a single character from the specified TESTHANDLE , or standard input stream if none is specified.
Now we will see how we can write the contents into the file.
Writing contents of file using filehandle.
open FILEHANDLE,"less thantest.txt";
print FILEHANDLE "This is the line getting printed in file\n";
Copying the file
Lets see the example for copying the contents one file to another. In the example below we are opening original file for reading and creating new file with another file handle in write mode.
# Opening file in read mode
open(FILEHANDLE_ORG, "less thanoriginal.txt");
# Opening a new file in write mode
open(FILEHANDLE_DUP, "greater thanduplicate.txt");
# Copying the data from one file to another using filehandle.
print FILEHANDLE_DUP $_;
close( FILEHANDLE_ORG );
close( FILEHANDLE_DUP );
Deleting a file
Deleting a file is also a very easy task in Perl. Perl has unlink function to delete the file. It is used as follows :
Hope you find this article helpful.