How to do file manipulation in Perl

In Perl, a FileHandle maps a name to an external file, which can then be used to perform a number of actions on the file such as reading, writing, and altering data on the file. A FileHandle can be used until the end of a program or until the file is closed.

All FileHandles are equipped with read/write access to the files. However, the user can determine the mode in which the FileHandle is opened. There are three standard types of FileHandles:

  • STDIN (Standard Input Stream)
  • STDOUT (Standard Output Stream)
  • STDERR (Standard Error Stream)

There are two functions that can be used to open a file, open() and sysopen(). Both functions perform the same tasks; however, the open() function is a high-level wrapper around the open system call, while the sysopen() is a relatively thin wrapper around it. sysopen() provides more precise control over file redirection and piping, but for most use cases, the open() function is more than enough.

open()

Protoytpe

open(FileHandle, Mode, "filename");

Parameters

  • FileHandle: FileHandle is used to refer to the file and perform actions read/write, or update operations on it until program execution stops or the FileHandle is closed.
  • Mode: The mode in which the file should be opened, e.g., read-only, read-write, etc.
  • filename: The name of the file passed as a string.

Note: The filename and the mode can be concatenated into a single string to form an expression like open(FileHandle, "+<FileName.txt") , where the +< denotes read and write mode.

Return value

open() returns non-zero upon success, and returns undef upon failure.

Print

The general way to write into any file stream is through the print() function. By default, print() will write to STDOUT. However, an optional parameter can be passed to specify the file stream where data is to be written.

Prototype

print FILEHANDLE LIST
print FILEHANDLE
print LIST
print

Parameters

  • FileHandle: The Filehandle specifying the file stream where data will be written.
  • List: The list of data to be written.

Return value

print returns True if successful.

Mode

Description

<

Read Only

>

Create, Write or Truncate

>>

Writes, Appends and Creates

+<

Read and Write

+>

Reads, Writes, Creates, and Truncates

+>>

Reads, Writes, Appends, and Creates

close()

The close() function closes the file descriptor or FileHandle, flushes the I/O buffers and closes any related pipes.

Prototype

close(FileHandle)

Parameters

  • FileHandle: The FileHandle to close.

Return value

close() returns True if all the operations are executed successfully.

<> Operator

The <> operator is the primary method of reading the contents of a file from a FileHandle. It returns the whole contents of the file is used in a scalar context. If used in the array context, it returns each line of the file as an element in the array.

Code

main.perl
test.txt
open($In, "+<" ,"test.txt");
while ($line = <$In>){
print $line;
}
close $In;

In the above code, we use the <FileHandle> operator in the singular context, reading one line at a time and printing it to STDOUT. Let’s see an example of <FileHandle> working in the array context:

main.perl
test.txt
open($In, "+<" ,"test.txt");
@array = <$In>;
print @array;

File reading and writing

The following code snippet gives an example of how to read and write to a file. The first step is to open the file using the open() function in read/write mode. Then, we read the contents of the file and display them on STDOUT. Next, we update the contents of the file and display the updated contents on the standard output.

main.perl
test.txt
open(In, "+<", "test.txt");
print(<In>);
close(In);
print "\nAppending ...\n";
# Appending a string to the file and displaying it.
open(Appnd, ">>", "test.txt");
print Appnd "\nThis is the appended string\n";
close Appnd;
# opening in read mode
open(Rd, "<", "test.txt");
print <Rd>;
close Rd;

sysopen()

sysopen() works similarly to the previously mentioned open() function, but it uses the open system call instead. sysopen() is equivalent to the C system call for open(), as Perl is written in C.

The PERMS argument denotes the file permissions given to the sysopen() call. If it is not specified, then the default value 0666 is used.

Prototype

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

Parameters

  • FileHandle: The name of the FileHandle used to refer to the file.
  • FileName: The name of the file.
  • Mode: The mode in which the file should open, e.g., read-only, read-write, etc.
  • PERMS: Specifies the permissions of the file. The default value is 0666.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved