Perl | File I/O Functions

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/ usr / bin / perl - w
  
my($data) = "";
open(F, "Hello.txt") or 
 die("Error encountered while reading file");
  
$data = <F>;
print("$data");
  
$data = readline(*F);
print("$data");
  
close(F);

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/ usr / bin / perl
   
my($read_data);
open(DATA, "<Hello.txt") or die "Error in reading the file";
  
# To convert the file 
# in binary mode
binmode(DATA);
  
close(DATA);

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/ usr / bin / perl
   
my($read_data);
open(DATA, "<Hello.txt") or die "Error in reading the file";
read(DATA, $read_data, 4);
   
print($read_data);
close(DATA);

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/ usr / bin / perl
  
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);

chevron_right


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:


filter_none

edit
close

play_arrow

link
brightness_4
code

#!/ usr / bin / perl
  
my($read_data);
open(DATA, "<Hello.txt") or
die "Error in reading the file";
  
# Here, WHENCE is set to 1
seek(DATA, 10, 1);
close(DATA);

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/ usr / bin / perl
  
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);

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

#!/ usr / bin / perl
  
my($read_data);
open(DATA, "<data.txt") or 
 die "Error in reading the file";
  
close(DATA);

chevron_right




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.