A filehandle is an internal structure in Perl that associates a physical file with a name. All filehandles have read/write access, so once a filehandle is attached to a file, it can be read/written. However, we must specify how the filehandle is open when mounting a file with a filehandle.
We use the open()
function to open a new or existing file.
Below is the syntax of the open()
function:
open( Filehandle, Mode, filename )
The open()
function accepts three parameters:
filehandle
: This is associated with the file.Mode
: This opens the file for reading, writing, or appending.Filename
: This is the path and filename of the file, or just the filename.The following table shows the Mode
in which various operations can be performed on files:
Mode | Operation |
> | Read |
< | Write |
>> | Append |
The file opening method is explained in detail as follows:
<
): We can only read the file, but not change its contents.>
): We can create a new file if the file does not exist. The contents of the file will be erased If the file already exists, so we must be careful when using the write mode.>>
): As the name suggests, we can open a file to append new content to the existing content of that file. However, we cannot change the existing content in the file.Once a file is allocated to a filehandle, various operations such as reading, writing, and appending can be performed. There are many different ways to read files.
The primary way to read information from an open filehandle is to use the < >
operator. When the < >
operator is used in a list context, it returns a list of rules from the specified filehandle. The following example reads a line from a file and stores it in a scalar value:
# Opening the fileopen(fh, "edu.txt") or die "File '$filename' can't be opened";# Reading First line from the file$firstline = <fh>;print "$firstline\n";
getc
functionThe getc()
function only returns a single character.
Below is the syntax of the getc()
function:
getc(filehandle)
The getc()
function accepts only one parameter:
filehandle
: This is associated with the file.The following example reads a character from a file and displays it:
# Opening the fileopen(fh, "edu.txt") or die "File '$filename' can't be opened";# Reading First char from the file$firstchar = getc(fh);print "$firstchar\n";
read
FunctionThe read()
function is used to read binary data from a file using a filehandle. If the file is read successfully, the function returns the number of bytes read—zero at the end of the file, or unddef
if an error occurs during the reading process.
Below is the syntax of the read()
function:
read(filehandle, var, length, offset)
The read()
function accepts four parameters:
filehandle
: This is associated with the file.length
: This indicates the length of the data that needs to be read.var
: This is where the data will be placed.offset
: The data will be placed in var
after the bytes offset. This is an optional parameter value.The following example reads multiple lines from a file and displays it:
# Opening the fileopen(fh, "edu.txt") or die "File '$filename' can't be opened";# Reading the file till FH reaches EOFwhile(<fh>){# Printing one line at a timeprint $_;}close;