Updated April 12, 2023
Introduction to Perl open
A variable that is associated with a file is called a file handle through which the contents of the file can be read or data can be written to the file depending on how we open the file. To open a file using a specified file handle, we make use of a function called open() function in Perl. It takes three arguments namely filehandle which is associated with the file to be opened, mode which specifies if the file is opened for reading the data or writing the data or appending the data, filepath which specifies the path to the location of the file that is to be opened.
The syntax to declare open() function in Perl is as follows:
Open(FileHandle, mode, filepath);
where FileHandle is the variable associated with the file that is to be opened,
mode specifies if the file is opened for reading, writing or appending and
filepath specifies the path to the location of the file that is to be opened.
Working of open() function in Perl
Working of open() function in Perl is as follows:
- A variable that is associated with a file is called file handle through which the contents of the file can be read or data can be written to the file depending on how we open the file.
- To open a file using a specified file handle, we make use of a function called open() function in Perl.
- The open() function in Perl takes three arguments namely file handle, mode, and filepath.
- FileHandle is the variable associated with the file that is to be opened.
- mode specifies if the file is opened for reading, writing, or appending.
- The operand < specifies the file is opened in read mode and the file can only be read and no changes can be made to the contents of the file.
- The operand > specifies the file is opened in write mode if it exists, in case the file does not exist, a new file is created and when we open a file in write mode, the contents of the file are deleted and writing starts from the beginning of the file.
- The operand >> specifies the file is opened in append mode through which data can be appended to the existing data in the file, however existing data cannot be changed.
Examples
Let us discuss examples of Perl open.
Example #1
Perl program to demonstrate the working of open function to open a file in write mode to write the data to the file and then read the contents of the file by opening the file in read mode to display the contents of the file:
Code:
use warnings;
use strict;
#The contents to be written to the file are stored in a string called datawrite
my $datawrite = <<END;
We are learning Perl
END
#a temporary file is created to write the data into the file
my $pathtothefile = "https://cdn.educba.com/tmp/file.txt";
#open function is used to open the file through file handler to write the data to the file
open(tow, '>', $pathtothefile) or die $!;
print tow $datawrite;
close(tow);
print "Data has been written to the file successfully\n";
#open function is used to open the file through file handler to read the data from the file
open(tor, '<', $pathtothefile) or die $!;
#displaying the contents of the file
print "The contents of the file are:\n";
while(<tor>){
print $_;
}
close(tor);
The output of the above program is as shown in the snapshot below:
In the above program, the data to be written to the file is stored in a string called datawrite. Then we are making use of open function to open the file in write mode to write the date stored in the string to the file. Then we are again making use of open function to open the file in read mode to read the data written to file and display the data as the output on the screen.
Example #2
Perl program to demonstrate the working of open function to open a file in write mode to write the data to the file and then read the contents of the file by opening the file in read mode to display the contents of the file and then open the file in append mode to append the data to the file and then display the appended contents as the output on the screen:
Code:
use warnings;
use strict;
#The contents to be written to the file are stored in a string called datawrite
my $datawrite = <<END;
We are learning Perl
END
#a temporary file is created to write the data into the file
my $pathtothefile = "https://cdn.educba.com/tmp/file.txt";
#open function is used to open the file through file handler to write the data to the file
open(tow, '>', $pathtothefile) or die $!;
print tow $datawrite;
close(tow);
print "Data has been written to the file successfully\n";
#open function is used to open the file through file handler to read the data from the file
open(tor, '<', $pathtothefile) or die $!;
#displaying the contents of the file
print "The contents of the file are:\n";
while(<tor>){
print $_;
}
close(tor);
#The contents to be appended to the file are stored in a string called datawrite1
my $datawrite1 = <<END;
Learning is fun
END
#open function is used to open the file through file handler to append the data to the file
open(tow, '>>', $pathtothefile) or die $!;
print tow $datawrite1;
close(tow);
print "Data has been appended to the file successfully\n";
#open function is used to open the file through file handler to read the data from the appended file
open(tor, '<', $pathtothefile) or die $!;
#displaying the contents of the file
print "The contents of the file after appending are:\n";
while(<tor>){
print $_;
}
close(tor);
The output of the above program is as shown in the snapshot below:
In the above program, the data to be written to the file is stored in a string called datawrite. Then we are making use of open function to open the file in write mode to write the date stored in the string to the file. Then we are again making use of open function to open the file in read mode to read the data written to file and display the data as the output on the screen. Then again we are making use of open function in append mode to append the data to the file. Then the file is again opened using open function in read mode to read the appended contents of the file and display it on the screen.
Conclusion
In this article, we have learnt the concept of open() function in Perl through definition, syntax, and working of open() function through programming examples and their outputs.
Recommended Articles
This is a guide to Perl open. Here we discuss the introduction and Working of open() function in Perl with Examples for better understanding. You may also have a look at the following articles to learn more –