Perl | File I/O Functions
Last Updated :
11 Nov, 2019
File handling in Perl is used to read data from an external file or to write data into an external file. This is very useful as it provides a platform to permanently store and retrieve data from files.
File Handle
A FileHandle associates a name to an external file, that can be used until the end of the program or until the FileHandle is closed. In short, a FileHandle is like a connection that can be used to modify the contents of an external file and a name is given to the connection (the FileHandle) for faster access and ease.
Input and output functions (I/O functions) are an integral part of file handling and below are the main I/O functions explained with examples.
readline()
This function reads one line from the FILEHANDLE which must be referred to by the given expression. For every function call, the next line of the file is read. This can be repeated till the end of file is reached. In order to use a filehandle directly, it must be passed as a typeglob.
Syntax:
readline (EXPR)
Example:
my ( $data ) = "" ;
open (F, "Hello.txt" ) or
die ( "Error encountered while reading file" );
$data = <F>;
print ( "$data" );
$data = readline ( *F );
print ( "$data" );
close (F);
|
Output:

binmode()
This function is used to set the format of reading and writing of FILEHANDLE to binary. When a file is written as a binary it doesn’t have an end of file character. Since the data is read and written in binary, the execution is faster and more efficient as the data doesn’t have to be converted by the machine and is directly understandable.
Syntax:
binmode(FILEHANDLE)
Example:
my ( $read_data );
open (DATA, "<Hello.txt" ) or die "Error in reading the file" ;
binmode (DATA);
close (DATA);
|
read()
This function is used to read a desired number of characters from a file during execution, i.e, it can be used to read a block of buffered information. It is also used to read binary data from files.
Syntax:
read(FILEHANDLE, SCALAR, LENGTH)
Example:
my ( $read_data );
open (DATA, "<Hello.txt" ) or die "Error in reading the file" ;
read (DATA, $read_data , 4);
print ( $read_data );
close (DATA);
|
Output:

print()
print() is one of the most important I/O functions in perl. It is used to output data into a file from the console.
Syntax:
print FILEHANDLE LIST
Example:
my ( $read_data );
open (DATA, "<Hello.txt" ) or
die "Error in reading the file" ;
@dat = ( "these" , "are" , "the" , "file" , "contents" );
print DATA @dat ;
close (DATA);
|
File before print()
:

Output:
This will write “These are the file contents” into the file data.txt

seek()
This function is used to change the position of the file pointer to the desired position. The position here can also be changed respectively to another specified position (WHENCE).
Syntax:
seek(FILEHANDLE, POSITION, WHENCE)
Here WHENCE is used to specify a position respective to which the pointer is moved.
a) WHENCE = 0, signifies that the pointer must start at the beginning of the file.
b) WHENCE = 1, signifies that the pointer must start at the current position of the file.
c) WHENCE = 2, signifies that the pointer must start at the end of the file.
Example:
my ( $read_data );
open (DATA, "<Hello.txt" ) or
die "Error in reading the file" ;
seek (DATA, 10, 1);
close (DATA);
|
Output:
The position of the pointer will be set to the specified position.
tell()
This function is used to get the position of the pointer in the file from the program.
Syntax:
tell(FILEHANDLE)
Example:
my ( $read_data );
open (DATA, "<Hello.txt" ) or
die "Error in reading the file" ;
print ( tell (DATA));
$char = getc (DATA);
print ( tell (DATA));
close (DATA);
|
Output:

This is so because initially the pointer is at 0 position but after reading a character it moves to position 1.
close()
When we open a file, we associate a FILEHANDLE with an external file. Thus, when we finish using the file, we must disassociate the FILEHANDLE from the file and for this, the close() function is used. This flushes the FILEHANDLE’s buffers.
Syntax:
close(FILEHANDLE)
Example:
my ( $read_data );
open (DATA, "<data.txt" ) or
die "Error in reading the file" ;
close (DATA);
|
Similar Reads
Perl | eof - End of File Function
The eof() function is used to check if the End Of File (EOF) is reached. It returns 1 if EOF is reached or if the FileHandle is not open and undef in all other cases. Syntax: eof(FileHandle) Parameter: FileHandle: used to open the file Returns: 1 if EOF is reached Cases: eof(FileHandle) : Passing Fi
2 min read
Perl | Useful File-handling functions
Perl was originally developed for the text processing like extracting the required information from a specified text file and for converting the text file into a different form. These operations can be performed by the use of various inbuilt file functions. Example: #!/usr/bin/perl # Opening a File
2 min read
Perl | getc Function
getc() function in Perl is used to read the next character from the file whose File Handle is passed to it as argument. If no FileHandle is passed then it takes one character as input from the user. Syntax: getc(FileHandle) Parameter: FileHandle: of the file to be read Returns: the character read fr
1 min read
Perl | tell() Function
tell() function in Perl is used to get the position of the read pointer in a File with the use of its FileHandle. If no FileHandle is passed then it returns the position within the most recent accessed file. Syntax: tell(FileHandle) Parameter: FileHandle: Filehandle of the file to be accessed. Retur
1 min read
Python tell() function
Python too supports file handling and provides inbuilt functions for creating, writing and reading files. There are two types of files that can be handled in python, normal text files and binary files (written in binary language,0s and 1s). Text files: In this type of file, Each line of text is term
3 min read
Perl | File Handling Introduction
In Perl, file handling is the process of creating, reading, writing, updating, and deleting files. Perl provides a variety of built-in functions and modules that make it easy to work with files. Here's an introduction to file handling in Perl: File modes:When opening a file in Perl, you need to spec
7 min read
OS File Operations
File operations within an operating system (OS) encompass a set of essential tasks and actions directed at files and directories residing within a computer's file system. These operations are fundamental for the effective management and manipulation of data stored on various storage devices. In this
4 min read
File System Mounting in OS
In this article, we are going to discuss the most important concept in Operating Systems which allows the users to organize and access files from different storage devices. If we compare it to a real-life scenario, it is closely related to âConnecting puzzles to get a complete picture of dataâ. What
3 min read
Python seek() function
The concept of file handling is used to preserve the data or information generated after running the program. Like other programming languages like C, C++, Java, Python also support file handling. Refer the below article to understand the basics of File Handling. File Handling in Python. Reading and
2 min read
Python open() Function
The Python open() function is used to open internally stored files. It returns the contents of the file as Python objects. Python open() Function SyntaxThe open() function in Python has the following syntax: Syntax: open(file_name, mode) Parameters: file_name: This parameter as the name suggests, is
3 min read