How to Write to a File in Perl

Perl Write to File

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. To write a file in Perl 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 for Using Perl Write to File

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

Perl Write to File - 1

Output #2

Perl Write to File - 2

Output #3 After writing data into the file.

Perl Write to File - 3

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.

abc.txt source file

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";

Copied successfully

Copying content from abc.txt file to pqr.txt file.

abc.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:

Perl Write to File - 7

New file has created the name as xyz.txt and writes data into the file.

xyz.txt

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

This is a guide to Perl Write to File. Here we discuss an introduction to Perl Write to File with appropriate syntax, how to write to File with three different methods in detail. You can also go through our other related articles to learn more –

  1. Perl if statements
  2. Perl Data Types
  3. Split in Perl
  4. Perl Versions

How to Write to a File in Perl

Source: https://www.educba.com/perl-write-to-file/

0 Response to "How to Write to a File in Perl"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel