Updated April 6, 2023
Introduction to Perl Write to File
Perl write to file is used to write content into a file, we can copy content from one text file and write into another text file in Perl. We have used a file handler variable to write to a file, this file handler is associated with the file. If we want to write any record into the file there is necessary that the file is opened in write mode. If the file already contains data then it will deletes old data and write the new content into the file otherwise new file created and content is added.
How to Write to File in Perl?
- There are various methods was available in Perl to write content into the file. File writing operations is very important and useful in Perl to write the content into the file.
- This write file is used to write the content into the file, in Perl we have assigning file handler to perform the various file operations on the file.
- If we want to write a file in Perl, it is necessary to open the file in write mode in Perl. Below is the syntax shows that open file in write mode are as follows.
Syntax To Open File in write mode
Open (FH, '>', "filename.txt");
- In the above syntax, the open keyword is used to open the file in write mode. Firstly it is important to open a file.
- Filename state that open specified file for write mode to write any content into the file.
- Print() function is very important in Perl to write content into the file. We have written content into the file using a print function.
Below is the syntax of the print function is as follows.
Syntax of Print function
Print filehandle_string;
- filehandle_string is used in print function while writing content into the file.
- We have used a file handler variable to write to a file, this file handler is associated with the file.
- If we want to write any record into the file there is necessary that the file is opened in write mode.
Methods
Below is the method available to write the data into the file are as follows.
- Using file handler associated with the file at the time of opening file string will hold the content to write into the file.
- Copying content from one file to another.
- Write content into the newly created file.
We have written content into the file using a file handler. A file handler is very important to write content into the file. In Perl, we have copying data from one file and also we have written the data into the other file. We have also created a new file and write the content into the newly created file in Perl. It is also possible to write content into the already created file.
Below is the method which we have used in a Perl to write content into the file.
Method #1 – Using file handler associated
Using file handler associated with the file at the time of opening file string which holds the content to write into the file.
Below example shows that write content into the already created file. We have written content into the abc.txt file. This file contains the data. We have overwritten the data of the abc.txt file in the below example.
Code:
# Opening file abc.txt write mode in Perl.
open (fh, ">", "abc.txt");
# Getting the string from user and write content into the file
print "Enter the content \n";
$write = <>;
# Writing content into the file.
print fh $write;
# Closing the file after writing the content into the file.
close(fh) or "close file";
Output #1
Output #2
Output #3 After writing data into the file.
The below steps shows that step by step execution of the program.
- Opening abc.txt file in write mode.
- Getting input from the user.
- Stored input in write variables which was pointed by $FH.
- Closing the abc.txt file.
Method #2 – Copying content from one file to another file.
We have used the abc.txt source file to copy data into the pqr.txt file. Below is the data present in the abc.txt file are as follows.
Code:
# Source File name as abc.txt
$source_file = 'abc.txt';
# Destination File name as pqr.txt
$destination_file = 'pqr.txt';
# open abc.txt file for reading
open(FHR, '<', $source_file);
# open pqr.txt file for writing
open(FHW, '>', $destination_file);
print("Copying $source_file to $destination_file\n");
while(<FHR>)
{
print FHW $_;
}
close(FHR);
close(FHW);
print “Copied successfully.\n”;
Copying content from abc.txt file to pqr.txt file.
The below steps shows that step by step execution of the program.
- Opening abc.txt file in reading mode and pqr.txt file for write mode.
- Reading the content from abc.txt file and write into the pqr.txt file.
- Copying the content of the file using the print function.
- Closing the abc.txt file when reading is done.
Method #3 – Create a new file for writing.
In the below example, we have created a file name as xyz.txt and write content into the file. We have taken input from the user while writing data into the file.
Code:
# Open and create file name as xyz.txt.
open (fh, ">", "xyz.txt");
# Getting the string from user and write content into the new file
print "Enter the content \n";
$new_write = <>;
# Writing content into the file.
print fh $new_write;
# Closing the file after writing the content into the file.
close(fh) or "close file";
Output:
New file has created the name as xyz.txt and writes data into the file.
The below steps shows that step by step execution of the program.
- Opening and creating xyz.txt file in write mode.
- Getting input from the user.
- Stored input in write variables which was pointed by $FH.
- Closing the xyz.txt file.
Conclusion
Perl write to file is used to write content into a file, we can copy content from one file and write into another text file. We have used a file handler variable to write to a file, this file handler is associated with the file.
Recommended Articles
We hope that this EDUCBA information on “Perl Write to File” was beneficial to you. You can view EDUCBA’s recommended articles for more information.