Perl | Appending to a File

When a file is opened in write mode using “>”, the content of the existing file is deleted and content added using the print statement is written to the file. In this mode, the writing point will be set to the end of the file. So old content of file remains intact and anything that is written to file using print statement is added to the end of the file. However, read operation cannot be performed unless the file is opened in +>> mode indicating append and read.






# Opening a file in read mode
# to display existing content 
open(FH, "Hello.txt") or 
     die "Sorry!! couldn't open";
# Reading and printing the existing
# content of the file
print"\nExisiting Content of the File:\n";
    print $_;
# Opening file in append mode
# using >>
open(FH, ">>", "Hello.txt") or 
die "File couldn't be opened";
# Getting the text to be appended 
# from the user
print "\n\nEnter text to append\n";
$a = <>;
# Appending the content to file
print FH $a;
# Printing the success message
print "\nAppending to File is Successful!!!\n";
# Reading the file after appending
print "\nAfter appending, Updated File is\n";
# Opening file in read mode to 
# display updated content
open(FH, "Hello.txt") or 
     die "Sorry!! couldn't open";
    print $_;
close FH or "couldn't close";


Original File:

Appending to File:

Updated File:

Here is how the program works:-
Step 1: Opening a file in read mode to see the existing content of the file.
Step 2: Printing the existing content of the file.
Step 3: Opening the File in Append mode to add content to the file.
Step 4: Getting text from the user to be appended to a file
Step 5: Appending text to file
Step 6: Reading the file again to see the updated content.
Step 7: Closing a file

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at to report any issue with the above content.