readFILEHANDLE
,SCALAR
,LENGTH
,OFFSET
readFILEHANDLE
,SCALAR
,LENGTH
This function attempts to read
LENGTH
bytes of data into variable
SCALAR
from the specified
FILEHANDLE
. The function returns the number of bytes actually read, 0 at end-of-file. It returns the undefined value on error.
SCALAR
will be grown or shrunk to the length actually read. The
OFFSET
, if specified, says where in the variable to start putting bytes, so that you can do a 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 that the opposite of read is simply a print , which already knows the length of the string you want to write, and can write a string of any length.
Perl's
read
function is actually implemented in terms of standard I/O's
fread
(3) function, so the actual
read
(2) system call may read more than
LENGTH
bytes to fill the input buffer, and
fread
(3) may do more than one system
read
(2) in order to fill the buffer. To gain greater control, specify the real system call using
sysread
. Calls to
read
and
sysread
should not be intermixed unless you are into heavy wizardry (or pain).