Syntax:
The syntax of the open() function with the required arguments is given as follows:
- The first argument contains the file handler variable that is used to access the file.
- The second argument contains the mode of opening the file which is described later.
- The third argument contains the filename that is opened.
Files Modes
Different types of file modes to open a file in Perl are shown in the following:
Mode | Purpose |
---|---|
‘r’ or ‘<’ | It is used to open a file for reading. |
‘w’ or ‘>’ | It is used to open a file for reading, writing, and making it empty. |
‘a’ or ‘>>’ | It is used to open a file for creating, writing, and appending. |
‘r+’ or ‘+<’ | It is used to open a file for reading and writing. |
‘w+’ or ‘+>’ | It is used to open a file for creating, reading, writing, and making it empty. |
‘a+’ or ‘+>>’ | It is used to open a file for creating, reading, writing, and appending. |
Different Uses of the Open() Function
The methods of opening a file for different purposes are shown in this part of the tutorial.
Example 1: Create a New File
Create a Perl file with the following script that creates a new file for writing. The name of the file is taken as the input and a line of text is written in the file.
use strict;
use warnings;
print "Enter a filename: ";
#Take the filename from the user
my $filename = <>;
#Remove newline from the input
chomp($filename);
#Open the file for writing
open(my $f_handler, '>', $filename);
#Write the content into the file
print $f_handler "This file is created for testing purpose.\n";
#Close the file handler
close $f_handler;
Output:
The following output appears after executing the script for the “test.txt” input value. Next, the “cat test.txt” command is executed to check whether the file is created with the content or not:
Example 2: Open a File for Reading
Create a text file named “Books.txt” with the following content that is used for reading in the example that is shown here.
Books.txt
Learning Perl
Intermediate Perl
Create a Perl file with the following script that opens the “Books.txt” file for reading using the open() function and the content of the file is retrieved and printed using a “while” loop.
use strict;
use warnings;
# Opening the file
open(f_handler, "< Books.txt") or die "Unable to open the file.";
print "The content of the file: \n";
#Read the file line by line
while(my $line = <f_handler>)
{
#Print each line of the file
print $line;
}
#Close the file handler
close f_handler;
Output:
The following output appears after executing the script:
Example 3: Open an Existing File for Appending
Create a Perl file with the following script that opens the “Books.txt” file for appending using the open() function and two lines of content are added at the end of the file using the “print” function.
Output:
The “cat Books.txt” command is executed first to check the existing content of the file. Next, the script is executed to append two lines in the file. Again, the “cat Books.txt” command is executed to check whether the file is modified or not:
Example 4: Copying the Content of a File to Another File
Create a text file named “Languages.txt” with the following content that is used in this example.
Languages.txt
CSS
JavaScript
PHP
Perl
Python
Create a Perl file with the following script that copies the content of the “Languages.txt” file into another text file named “Languages2.txt” file. Here, two file handlers are used for reading from a file and writing into another file.
use strict;
use warnings;
#Open a file for reading
open(f_handler1, "< Languages.txt");
#Open a file for writing
open(f_handler2, "> Languages2.txt");
# Copy the Languages.txt into Languages2.txt
while(my $line = <f_handler1>)
{
#Add the line from one file to another file
print f_handler2 $line;
}
#Close both file handlers
close f_handler1;
close f_handler2;
Output:
The content of both the “Languages.txt” file and “Languages2.txt” file are checked before executing the script of this example. Next, the content of the “Languages2.txt” file is checked after executing the script.
Conclusion
The uses of the open() function for creating, reading, writing, and appending the files are shown in this tutorial by multiple Perl examples.