read

readFILEHANDLE,SCALAR,LENGTH,OFFSETreadFILEHANDLE,SCALAR,LENGTH
This function tries to read LENGTH
characters (meaning codepoints, not graphemes) of
data into variable SCALAR from the specified
FILEHANDLE. The function returns the number
of characters read or 0 at end-of-file. It returns undef on error.
SCALAR will grow or shrink to the length
actually read. The OFFSET, if specified,
determines where in the variable to start putting characters so that you
can read into the middle of a string.
To copy data from filehandle FROM into filehandle TO, you could say:
while (read(FROM, $buf, 16384)) {
print TO $buf;
}Note the characters: depending on the status
of the filehandle, either (8-bit) bytes or characters are read. A byte
is just Perl’s way of talking about undecoded codepoints with small
values. By default, all filehandles operate on bytes. But, for example,
if the filehandle has been opened with the :utf8 I/O layer, the I/O will operate on
UTF-8-encoded Unicode characters, not bytes. Similarly for the
two-argument form of binmode, the
middle argument to open, or via the ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access