Updated April 3, 2023
Introduction to File Handling in Perl
File handling in Perl is the most important task, it is the internal structure of the file that was associated with the name of the file. Using file handling in Perl we can open, create, read, write and close the file, for opening file in Perl we have used >, < and >> operator. In another word file handling in Perl is nothing but it is the connection of the file to modify the content of the file and file name is given into a connection to access a file. There are three file handles available in Perl are STDERR, STDOUT, and STDIN.
Various File Operations in Perl
Perl file handles are used in file creating, opening, reading, writing, closing, and copying the file. The file operations available in Perl are as follows:
1. Perl Create File
- We are creating a file name as “perl_create_file” to define an example of create file in perl.
- We have used $fh (This is a file handler scalar variable) variable and it is defined inside the open function in Perl.
- In the below example, we can define scalar variable inside the open function.
- We have used “>” sign for opening the file for writing mode. Also, we have used variable like “$create_file” this variable is used to define a path or location of the file.
- The print function is used to print any text into the file. We have open and close the file using a $fh variable.
- The below example shows the Create File operation in Perl is as follows.
Code:
use warnings;
use strict;
## Create a file name as perl_create_file
my $create_file = 'perl_create_file.txt';
## Opening perl_create_file file for write operation.
open(my $fh, '>', $create_file) or die "File is not opening '$create_file' $!";
## Writing below contents in perl_create_file file
print $fh "file created\n";
## Closing the file.
close $fh;
print "file closed\n";
Output:
2. Perl Open File
We have used below operator to open a file in Perl are as follows. We will discuss one by one are as follows.
- <
- >
- +> and +<
- >
1. <
- “<” sign is basically used to open an existing file in perl. While using this operator file will open in read mode.
- The below example shows the “<” operator is as follows.
Code:
open FILE, "<", "perl_create_file.txt" or die $!
2. >
- “>” sign is basically used to open an existing file or create a new file if it does not exist in perl. While using this operator file will be opened in write mode.
- The below example shows the “>” operator are as follows.
Code:
open FILE, ">", "create_new_file.txt" or die $!
3. +> and +<
- “<” this operator is basically used for empty the file before opening it, to prevent this we have used + sign before the > and < operator.
- The below example shows the operator of +> and +< in perl.
Code:
open FILE, "+<", "perl_create_file.txt" or die $!
open FILE, "+>", "perl_create_file.txt" or die $!
4. >>
- “>>” this sign is basically used to read and append the file content in perl.
- The below example shows the >> operator is as follows.
Code:
open FILE, "+>", "perl_create_file.txt" or die $!
3. Perl Read File
In Perl language, we can read a single line or multiple lines at one time using read file operation.
- Read Single Line from File
The below example shows that read single line from file is as follows.
Code:
use strict;
use warnings;
## Define file name for reading operation.
my $read_file = 'perl_create_file.txt';
## Opening perl_create_file file for read operation.
open(my $fh, '<', $read_file) or die "file is not opening '$read_file' $!";
my $row = <$fh>;
print "$row\n";
print "Completed\n";
Output:
- Read Multiple Line from File
The below example shows that read multiple line from file are as follows.
Code:
use strict;
use warnings;
## Define file name for reading operation.
my $read_file = 'perl_create_file.txt';
## Opening perl_create_file file for read operation.
open(my $fh, '<', $read_file) or die "file is not opening '$read_file' $!";
##Pring multiple lines.
while (my $row = <$fh>) {
chomp $row;
print "$row\n"; }
print "Completed\n";
Output:
4. Perl Write File
While writing a file in Perl line is added last to the file. The below example shows the write a file in Perl is as follows.
Code:
my $write_file = 'perl_create_file.txt';
## Opening perl_create_file file for write operation.
open (FILE, ">> $write_file") || die "problem in file opening $write_file\n";
##Print the first line in the file.
print FILE "Add the first line into the file.\n";
## write a first array of lines in to the file.
print FILE @lines1;
##Print the second line in the file.
print FILE "Add the second line into the file.";
# write a second array of lines in to the file.
print FILE @lines2;
Output:
5. Perl Close File
- Close file operation is used to close file in perl. We have close file in Perl using a close function.
- Closing of file is not compulsory operations in perl, Perl will automatically close the file if we have not defined close function in our code.
- The below example shows the close file operation in Perl is as follows.
Code:
my $close_file = 'perl_close_file.txt';
## Opening perl_create_file file for close operation.
open (FILE, ">> $close_file") || die "problem in file opening $close_file\n";
##Close file.
close perl_close_file.txt;
Output:
6. Perl Copying File
The below example shows the close file operation in Perl is as follows. Copying operation will copy the contents of file from one file to another file.
Code:
open(Datafile1, "<perl_create_file.txt");
open(Datafile2, ">copying_file2.txt");
while(<Datafile1>)
{
print Datafile2 $_;
}
close Datafile1;
close Datafile2;
Output:
Conclusion
File handling is very important in every language, in Perl we can open, create, read, write, and close the file. There is three file handler available in Perl are STDERR, STDOUT, and STDIN. File handling is the internal structure of the file which was associated with the name of the file.
Recommended Articles
We hope that this EDUCBA information on “File Handling in Perl” was beneficial to you. You can view EDUCBA’s recommended articles for more information.