Perl read Function



Description

This function reads, or attempts to read, LENGTH number of bytes from the file associated with FILEHANDLE into BUFFER. If an offset is specified, the bytes that are read are placed into the buffer starting at the specified offset.

Syntax

Following is the simple syntax for this function −

read FILEHANDLE, SCALAR, LENGTH, OFFSET

read FILEHANDLE, SCALAR, LENGTH

Return Value

This function the number of bytes read or the undefined value.

Example

Following is the example code showing its basic usage −

#!/usr/bin/perl -w

my($buffer) = "";
open(FILE, "/etc/services") or
     die("Error reading file, stopped");
while(read(FILE, $buffer, 100) ) {
   print("$buffer\n");
}
close(FILE);

When above code is executed, it produces the following result −

kerberos_master 751/udp  # Kerberos authentication
kerberos_master 751/tcp  # Kerberos authentication
passwd_server   752/udp  # Kerberos passwd server
perl_function_references.htm
Advertisements