Close
Close

File I/O in Perl


When a program runs, the data is in memory but when it ends or the computer shuts down, it gets lost. To keep data permanently, we need to write it in a file. Like a book, we also need to first open the file and then read or write and at last to close it. Now let's see how to read or write a file.

Opening a file


Now, let's see how to open a file:

open (my $fh, "<", "test.txt") or die "Can't open < test.txt: $!";
Output

open function is used to open a file.
$fh is the file handle. It allows us to refer to the file in future.
< is used to open a file in read mode. This means that we will not be able to write anything on a file opened in read mode.
test.txt is the name of the file which we want to open. It must exist in your working directory.
or die "Can't open < test.txt: $!" - It will give an error message if the computer is not able to open the file. $_ stores the error message. Try to open a file which doesn't exist in your directory and you will get the error message.

We use readline function to read from the filehandle. It reads each line until the end-of-file is reached. Let's see it working:

open (my $fh, "<", "test.txt") or die "Can't open < test.txt: $!";
print readline($fh);
Output
This is a test file.
New line in this file.

Closing a file


We use close FILEHANDLE to close a file.

open (my $fh, "<", "test.txt") or die "Can't open < test.txt: $!";
print readline($fh);
close($fh) or  "Couldn't close the file: $!";
Output
This is a test file.
New line in this file.

Writing on a file


To write on the file, we open it in write (>) or append mode (>>). This will open a file if the file exists in the directory and if the file doesn't exist, then it will create a new one. Writing on a file opened in write mode will write from the starting of the file by replacing its original content, whereas writing on file opened in append mode will start writing from the end of the previously written content.

Adding + before > or < means that we want both read and write access to the file.

+< allows us to both read and write both on the file while +> will read, write, create and truncate the file. We can also use + before >> i.e., +>>.

We just use print function with filehandle to write to a file.

open (my $fh, ">", "new.txt") or die "Can't open > test.txt: $!";

#writing
print $fh "I am new file\n";

close($fh) or  "Couldn't close the file: $!";

#reopening new.txt in read mode
open (my $fh, "<", "new.txt") or die "Can't open < test.txt: $!";
print readline($fh);
close($fh) or  "Couldn't close the file: $!";
Output
I am new file

We used print $fh "I am new file\n"; to write "I am new file" in the file 'new.txt' with its filehandle "$fh".

Let's see an example of copying a file to another.

open (my $fh_source, "<", "test.txt") or die "Can't open < test.txt: $!";
open (my $fh_dest, ">", "dest.txt") or die "Can't open > test.txt: $!";

#copying
print $fh_dest readline($fh_source);

close($fh_source) or  "Couldn't close the file: $!";
close($fh_dest) or  "Couldn't close the file: $!";

#reopening dest.txt in read mode
open (my $fh_dest, "<", "dest.txt") or die "Can't open < test.txt: $!";
print readline($fh_dest);
close($fh_dest) or  "Couldn't close the file: $!";
Output
This is a test file.
New line in this file.

Renaming a file


We use rename function to rename a file.

rename("dest.txt","best.txt");

Deleting a file


We use unlink function to delete a file.

unlink("best.txt");

Ask Yours
Post Yours
Doubt? Ask question