1. Perl
  2. builtin functions
  3. here

sysread function - Read from a file with a specified number of bytes (no buffering)

You can use the sysread function to read a file from a file with a specified number of bytes.

sysread $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 sysread function is executed, the data of the specified byte size is read into the scalar variable specified by the second argument. The sysread function is paired with syswrite function.

The difference from read function is that the sysread function is not buffered.

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

Example sysread function

This is an example program of the sysread function. I'm reading 8 bytes from a file using the sysread 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:$!";

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

# Output the read data
print $buffer;

Related Informatrion