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 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 a
readinto 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 ofreadis simplyprint, which already knows the length of the string you want to write and can write a string of any length.Perl's
readfunction is actually implemented in terms of standard I/O'sfreadfunction, so the actualreadsystem call may read more than length bytes to fill the input buffer, andfreadmay do more than one systemreadin order to fill the buffer. To gain greater control, specify the real system call usingsysread.