1. Perl
  2. builtin functions
  3. here

read function - Read from a file by specifying the number of bytes

You can use the read function to read a file by specifying the number of bytes.

read $fh, $buffer, $byte_size;

The first argument is the file handle, the second argument is the scalar variable that stores the read data, and the third argument is the byte size.

When the read function is executed, the data of the specified byte size is read into the scalar variable specified by the second argument.

See the official documentation for a detailed explanation of the read function.

Read function example

This is an example program of the read function. I'm reading 8 bytes from a file using the read function.

use strict;
use warnings;

# Open the file to read
my $file = 'data.txt';
open my $fh, '<', $file
  or die "Can't open file $file:$!";

# read function
my $buffer;
my $byte_size = 8;
read $fh, $buffer, $byte_size;

# Output the read data
print $buffer;

Related Informatrion