CARVIEW |
- #sysread FILEHANDLE,SCALAR,LENGTH,OFFSET
- #sysread FILEHANDLE,SCALAR,LENGTH
-
Attempts to read LENGTH bytes of data into variable SCALAR from the specified FILEHANDLE, using read(2). It bypasses any PerlIO layers including buffered IO (but is affected by the presence of the
:utf8
layer as described later), so mixing this with other kinds of reads,print
,write
,seek
,tell
, oreof
can cause confusion because the:perlio
or:crlf
layers usually buffer data. Returns the number of bytes actually read,0
at end of file, or undef if there was an error (in the latter case$!
is also set). SCALAR will be grown or shrunk so that the last byte actually read is the last byte of the scalar after the read.An OFFSET may be specified to place the read data at some place in the string other than the beginning. A negative OFFSET specifies placement at that many characters counting backwards from the end of the string. A positive OFFSET greater than the length of SCALAR results in the string being padded to the required size with
"\0"
bytes before the result of the read is appended.open(my $FH, "<", "input.txt") or die("Cannot open file: $!"); my $buf = ""; my $num = 0; # Read up to 64 bytes $num = sysread($FH, $buf, 64); # Read up to 32 bytes into position 512 of $buf $num = sysread($FH, $buf, 32, 512);
There is no syseof() function, which is ok, since
eof
doesn't work well on device files (like ttys) anyway. Usesysread
and check for a return value of 0 to decide whether you're done.Note that if the filehandle has been marked as
:utf8
,sysread
will throw an exception. The:encoding(...)
layer implicitly introduces the:utf8
layer. Seebinmode
,open
, and the open pragma.
Perldoc Browser is maintained by Dan Book (DBOOK). Please contact him via the GitHub issue tracker or email regarding any issues with the site itself, search, or rendering of documentation.
The Perl documentation is maintained by the Perl 5 Porters in the development of Perl. Please contact them via the Perl issue tracker, the mailing list, or IRC to report any issues with the contents or format of the documentation.