read
read filehandle, $var, length, [offset]
- Attempts to read length bytes of data into variable var from the specified filehandle. The function returns the number of bytes actually read, or
0
at end-of-file. It returns the undefined value on error. var will grow or shrink to the length actually read. The offset, if specified, says where in the variable to start putting bytes, so that you can do aread
into the middle of a string.To copy data from the filehandle FROM into the filehandle TO, you could say:
while (read FROM, $buf, 16384) { print TO $buf; }
Note that the opposite ofread
is simplyprint
, 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'sfread
function, so the actualread
system call may read more than length bytes to fill the input buffer, andfread
may do more than one systemread
in order to fill the buffer. To gain greater control, specify the real system call usingsysread
.