How do we read a file in Perl?

Overview

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.

Opening a file

We use the open() function to open a new or existing file.

Syntax

Below is the syntax of the open() function:

open( Filehandle, Mode, filename )

Parameter values

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:

  • Read(<): We can only read the file, but not change its contents.
  • Write (>): 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.
  • Append(>>): 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.

Reading a 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.

Read a file

1. The FileHandle operator

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:

main.perl
edu.txt
# Opening the file
open(fh, "edu.txt") or die "File '$filename' can't be opened";
# Reading First line from the file
$firstline = <fh>;
print "$firstline\n";

2. The getc function

The getc() function only returns a single character.

Syntax

Below is the syntax of the getc() function:

getc(filehandle)

Parameter value

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:

main.perl
edu.txt
# Opening the file
open(fh, "edu.txt") or die "File '$filename' can't be opened";
# Reading First char from the file
$firstchar = getc(fh);
print "$firstchar\n";

3. The read Function

The 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.

Syntax

Below is the syntax of the read() function:

read(filehandle, var, length, offset)

Parameter values

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.

Reading multiple lines at once

The following example reads multiple lines from a file and displays it:

main.perl
edu.txt
# Opening the file
open(fh, "edu.txt") or die "File '$filename' can't be opened";
# Reading the file till FH reaches EOF
while(<fh>)
{
# Printing one line at a time
print $_;
}
close;

Copyright ©2024 Educative, Inc. All rights reserved