Some Useful Methods of FileHandle
Some commonly used methods of the FileHandle module are as follows:
Method | Purpose |
tell | It is used to count the size of the file in bytes. |
getc | It is used to read each character of the file content. |
seek | It is used to move the file pointer to a particular position. |
eof | It indicates the end of the file. |
close | It is used to close the previously defined file handler. |
Examples of Perl FileHandle
Different uses of the FileHandle module are shown in this part of the tutorial using multiple examples.
Example 1: Create a New File
Create a Perl file with the following script that creates a new file using the FileHandle module. A filename is taken from the user and checked whether it exists or not. If the file does not exist, the file is opened for writing by creating an object of the “FileHandle” class after waiting 1 second.
use strict;
use warnings;
use 5.34.0;
use FileHandle;
#Take the file name from the user to create
print "Enter the filename:";
my $f = <>;
chomp($f);
#Check whether the file already exists or not
if (-e $f)
{
#Print a message if the file exists
say "File already exists.";
}
else
{
#Create file handler object
my $FileHandler = FileHandle->new;
say "Writing into the file...";
#Wait for 1 second
sleep(1);
#Open the file for writing
if ($FileHandler->open("> $f"))
{
#Write a line of text into the file
print $FileHandler "It is a test message.\nIt is the second line.\n";
#Close the file handler
$FileHandler->close;
}
}
Output:
According to the output, the “test.txt” file did not exist before and this file is created with two lines of text. Then, the “cat” command is executed to check whether the file is created successfully with the content or not:
The script is executed again with the same input value and the “File already exists” message is printed here because the “test.txt” file is created before.
Example 2: Read the Content of the File
Create a Perl file with the following script that reads the content of a file using the “FileHandle” class if the file exists. The filename is taken from the user.
use strict;
use warnings;
use 5.34.0;
use FileHandle;
#Take the file name from the user to create
my $f = <>;
my $f = ;
chomp($f);
#Check whether the file already exists or not
if (-e $f)
{
#Open the file for reading
my $FileHandler = FileHandle->new(" < $f");
#Print the file content
print <$FileHandler>;
#Close the file handler
close $FileHandler;
}
Output:
The content of the “test.txt” file is printed in the output because it exists in the current location:
Example 3: Append a Content into a File
Create a Perl file with the following script that appends the content into a non-empty file and print the size of the file in bytes. The filename is taken from the user. If the file exists and contains the data, a line of text is added at the end of the file. Otherwise, a message is printed.
use strict;
use warnings;
use 5.34.0;
use FileHandle;
#Take the file name from the user to create
print "Enter the filename:";
my $f = <>;
chomp($f);
#Check whether the file already exists and nonempty or not
if (-s $f)
{
my $FileHandler = FileHandle->new(" >> $f");
#Print the file size
print "The size of the file is ". $FileHandler->tell." bytes.\n";
say "Appending content into the file...";
#Wait for 1 second
sleep(1);
#Write content at the end of the file
print $FileHandler "It is a new line.\n";
#Close the file handler
$FileHandler->close;
}
else
{
say "File does not exist.";
}
Output:
According to the output, a new line is appended to the “test.txt” file and the size of the file is 45 bytes. Then, the “cat” command is executed to check whether the content is added properly into the file or not:
Conclusion
The FileHandle module is one of the options of Perl to read, write, or append the files. Three different uses of this module are shown in this tutorial using simple examples.