Perl | Appending to a File Last Updated : 05 Mar, 2019 Comments Improve Suggest changes 1 Likes Like Report 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. Example: perl # 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"; while(<FH>) { 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"; while(<FH>) { 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 Create Quiz Comment R rupanisweety Follow 1 Improve R rupanisweety Follow 1 Improve Article Tags : Perl Perl-files Explore BasicsPerl Programming Language2 min readIntroduction to Perl7 min readPerl Installation and Environment Setup in Windows, Linux, and MacOS3 min readPerl | Basic Syntax of a Perl Program10 min readHello World Program in Perl3 min readFundamentalsPerl | Data Types3 min readPerl | Boolean Values3 min readPerl | Operators | Set - 112 min readPerl | Operators | Set - 27 min readPerl | Variables4 min readPerl | Modules3 min readPackages in Perl4 min readControl FlowPerl | Decision Making (if, if-else, Nestedâif, if-elsif ladder, unless, unless-else, unless-elsif)6 min readPerl | Loops (for, foreach, while, do...while, until, Nested loops)7 min readPerl | given-when Statement4 min readPerl | goto statement3 min readArrays & ListsPerl | Arrays6 min readPerl | Array Slices3 min readPerl | Arrays (push, pop, shift, unshift)3 min readPerl List and its Types4 min readHashPerl Hash4 min readPerl | Hash Operations8 min readPerl | Multidimensional Hashes6 min readScalarsPerl | Scalars2 min readPerl | Comparing Scalars6 min readPerl | scalar keyword2 min readStringsPerl | Quoted, Interpolated and Escaped Strings4 min readPerl | String Operators4 min readPerl | String functions (length, lc, uc, index, rindex)4 min readOOP ConceptsObject Oriented Programming (OOPs) in Perl7 min readPerl | Classes in OOP6 min readPerl | Objects in OOPs6 min readPerl | Methods in OOPs5 min readPerl | Constructors and Destructors4 min readPerl | Method Overriding in OOPs6 min readPerl | Inheritance in OOPs7 min readPerl | Polymorphism in OOPs4 min readPerl | Encapsulation in OOPs6 min readRegular ExpressionsPerl | Regular Expressions2 min readPerl | Operators in Regular Expression4 min readPerl | Regex Character Classes3 min readPerl | Quantifiers in Regular Expression4 min readFile HandlingPerl | File Handling Introduction7 min readPerl | Opening and Reading a File4 min readPerl | Writing to a File3 min readPerl | Useful File-handling functions2 min read Like