Opening and Closing Files in Perl


There are following two functions with multiple forms, which can be used to open any new or existing file in Perl.

open FILEHANDLE, EXPR
open FILEHANDLE

sysopen FILEHANDLE, FILENAME, MODE, PERMS
sysopen FILEHANDLE, FILENAME, MODE

Here FILEHANDLE is the file handle returned by the open function and EXPR is the expression having file name and mode of opening the file.

Open Function

Following is the syntax to open file.txt in read-only mode. Here less than < sign indicates that file has to be opened in read-only mode.

open(DATA, "<file.txt");

Here DATA is the file handle, which will be used to read the file. Here is the example, which will open a file and will print its content over the screen.

#!/usr/bin/perl
open(DATA, "<file.txt") or die "Couldn't open file file.txt, $!";
while(<DATA>) {
   print "$_";
}

Following is the syntax to open file.txt in writing mode. Here less than > sign indicates that file has to be opened in the writing mode.

open(DATA, ">file.txt") or die "Couldn't open file file.txt, $!";

This example actually truncates (empties) the file before opening it for writing, which may not be the desired effect. If you want to open a file for reading and writing, you can put a plus sign before the > or < characters.

For example, to open a file for updating without truncating it −

open(DATA, "+<file.txt"); or die "Couldn't open file file.txt, $!";

To truncate the file first −

open DATA, "+>file.txt" or die "Couldn't open file file.txt, $!";

You can open a file in the append mode. In this mode, writing point will be set to the end of the file.

open(DATA,">>file.txt") || die "Couldn't open file file.txt, $!";

A double >> opens the file for appending, placing the file pointer at the end, so that you can immediately start appending information. However, you can't read from it unless you also place a plus sign in front of it −

open(DATA,"+>>file.txt") || die "Couldn't open file file.txt, $!";

Following is the table, which gives the possible values of different modes

Sr.NoEntities & Definition
1< or r
Read Only Access
2< or w
Creates, Writes, and Truncates
3< or a
Writes, Appends, and Creates
4+< or r+
Reads and Writes
5+> or w+
Read Only Access
6+>> or a+
Read Only Access

Sysopen Function

The sysopen function in Perl is similar to the main open function, except that it uses the system open() function, using the parameters supplied to it as the parameters for the system function −

For example, to open a file for updating, emulating the +<filename format from open −

sysopen(DATA, "file.txt", O_RDWR);

Or to truncate the file before updating −

sysopen(DATA, "file.txt", O_RDWR|O_TRUNC );

You can use O_CREAT to create a new file and O_WRONLY- to open file in write only mode and O_RDONLY - to open file in read only mode.

The PERMS argument specifies the file permissions for the file specified, if it has to be created. By default it takes 0x666.

Following is the table, which gives the possible values of MODE.

Sr.NoEntities & Definition
1O_RDWR
Read and Write
2O_RDONLY
Read Only
3O_WRONLY
Write Only
4O_CREAT
Create the file
5O_APPEND
Append the file
6O_TRUNC
Truncate the file
7O_EXCL
Stops if file already exists
8O_NONBLOCK
Non-Blocking usability

Close Function

To close a file handle, and therefore disassociate the file handle from the corresponding file, you use the close function. This flushes the file handle's buffers and closes the system's file descriptor.

close FILEHANDLE
close

If no FILEHANDLE is specified, then it closes the currently selected filehandle. It returns true only if it could successfully flush the buffers and close the file.

close(DATA) || die "Couldn't close file properly";

Updated on: 29-Nov-2019

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements