Perl

FileHandle Module in Perl

Multiple ways exist in Perl for creating and accessing the files. A file can be opened for reading, writing, or updating in Perl using a file handler. The file handler can be used in the open() method in a structured programming format or object-oriented programming format. The FileHandle module can be used in Perl to declare the file handler as an object and use this object to open a file for creating or accessing using different properties and methods of the “FileHandle” class. The uses of some common methods of the FileHandle module and the methods of using the FileHandle module to work with the file in Perl are shown in this tutorial.

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.

#!/usr/bin/perl

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:

p1-1

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.

p1-2

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.

#!/usr/bin/perl

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:

p2

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.

#!/usr/bin/perl

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:

p3-1

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.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.