A filehandle is a variable that is used to read and write to a file. This filehandle gets associated with the file.
In order to write to the file, it is opened in write mode as shown below:
open (FH, ‘>’, “filename.txt”);
If the file is existing then it truncates the old content of file with the new content. Otherwise a new file will be created and content will be added.
print() function is used to write content to a file.
Syntax: print filehandle string
Here, filehandle is associated to the file at the time of opening the file and string holds the content to be written to the file.
Example :
# Opening file Hello.txt in write mode open (fh, ">", "Hello.txt"); # Getting the string to be written # to the file from the user print "Enter the content to be added\n"; $a = <>; # Writing to the file print fh $a; # Closing the file close(fh) or "Couldn't close the file"; |
Before Writing to File:

Executing Code to Write:

Updated File:

Here is how the program works:
Step 1: Opening file Hello.txt in write mode.
Step 2: Getting the text from the standard input keyboard.
Step 3: Writing the string stored in ‘$a’ to the file pointed by filehandle ‘fh’
Step 4: Closing the file.
Copying Content from one file to another:
Before Code Execution:
Source File:

Destination File:

Example :
Example below reads the content from the source file and writes it to destination file.
# Source File $src = 'Source.txt'; # Destination File $des = 'Destination.txt'; # open source file for reading open(FHR, '<', $src); # open destination file for writing open(FHW, '>', $des); print("Copying content from $src to $des\n"); while(<FHR>) { print FHW $_; } # Closing the filehandles close(FHR); close(FHW); print "File content copied successfully!\n"; |
Executing Code:

Updated Destination File:

Here is how the program works:-
Step 1: Opening 2 files Source.txt in read mode and Destination.txt in write mode.
Step 2: Reading the content from the FHR which is filehandle to read content while FHW is a filehandle to write content to file.
Step 3: Copying the content with the use of print function.
Step 4: Close the conn once reading file is done.
There are two ways in which Errors can be handled
- Throw an exception if file cannot be opened (Handling an error)
- Give a warning if file cannot be opened and continue running (Error reporting)
Throw an Exception (Using Die Function)
When filehandle could not be assigned a valid file pointer at that time die gets executed printing the message and kills the current program.
Example :
# Initializing filename $filename = 'Hello.txt'; # $filename = 'ello.txt'; # Prints an error and exits # if file not found open(fh, '<', $filename) or die "Couldn't Open file $filename"; |

In the above code when File exists it simply gets executed with no errors but if file doesn’t exists then it generates an error and code terminates.
Give a warning (Using warn function)
When filehandle could not be assigned a valid file pointer it just prints warning message using warn function and keeps running.
Example :
# Initializing filename $filename = 'GFG.txt'; # Opening a file and reading content if(open(fh, '<', $filename)) { while(<fh>) { print $_; } } # Executes if file not found else{ warn "Couldn't Open a file $filename"; } |
When File exists:

When File doesn’t exists:

Recommended Posts:
- Modes of Writing a Perl Code
- Perl | Basic Syntax of a Perl Program
- Perl Tutorial - Learn Perl With Examples
- Perl | Searching in a File using regex
- Perl | File Locking
- Perl | File Test Operators
- Perl | Opening and Reading a File
- Perl | Appending to a File
- Perl | Reading a CSV File
- Perl | File Upload in CGI
- Perl | File Handling Introduction
- Perl | Accessing a Directory using File Globbing
- Perl | Useful File-handling functions
- Perl | File I/O Functions
- Perl | eof - End of File Function
- CSS | writing-mode Property
- Analytical Writing Section in GRE General
- Writing CSV files in Python
- Why split the <script> tag when writing it within document.write() method in JavaScript ?
- Writing to Files in R Programming
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.

