问题描述
read
和 sysread
有非常相似的文档.两者有什么区别?
read
and sysread
have very similar documentation. What are the differences between the two?
推荐答案
关于 read
:
read
支持 PerlIO 层.read
适用于任何 Perl 文件句柄.读取
缓冲区.read
以 8 KiB 的固定大小块的形式从系统获取数据.read
如果可用数据少于请求的数据,则可能会阻塞.
read
supports PerlIO layers.read
works with any Perl file handle.read
buffers.read
obtains data from the system in fixed sized blocks of 8 KiB.read
may block if less data than requested is available.
关于sysread
:
sysread
不支持 PerlIO 层(意味着它需要一个原始的也就是二进制句柄).sysread
仅适用于映射到系统文件句柄/描述符的 Perl 文件句柄.sysread
不缓冲.sysread
执行单个系统调用.sysread
如果数据可返回,则立即返回,即使数据量少于请求的数量.
sysread
doesn't support PerlIO layers (meaning it requires a raw a.k.a. binary handle).sysread
only works with Perl file handles that map to a system file handle/descriptor.sysread
doesn't buffer.sysread
performs a single system call.sysread
returns immediately if data is available to be returned, even if the amount of data is less than the amount requested.
总结和结论:
read
适用于任何 Perl 文件句柄,而sysread
仅限于映射到系统文件句柄/描述符的 Perl 文件句柄.read
与select不兼容code>
,而sysread
与select
兼容.read
可以为你解码,而sysread
需要你自己解码.read
对于非常小的读取应该更快,而sysread
对于非常大的读取应该更快.
read
works with any Perl file handle, whilesysread
is limited to Perl file handles mapped to a system file handle/descriptor.read
isn't compatible withselect
, whilesysread
is compatible withselect
.read
can perform decoding for you, whilesysread
requires that you do your own decoding.read
should be faster for very small reads, whilesysread
should be faster for very large reads.
注意事项:
例如,这些包括绑定文件句柄和使用
open(my $fh, '<', $var)
创建的文件句柄.
在 5.14 之前,Perl 读取 4 KiB 块.从 5.14 开始,构建 perl
时可以配置块的大小,默认为 8 KiB.
Before 5.14, Perl read in 4 KiB blocks. Since 5.14, the size of the blocks is configurable when you build perl
, with a default of 8 KiB.
根据我的经验,read
在从普通文件读取时将准确返回请求的数量(如果可能),但在从管道读取时可能返回较少.绝不保证这些结果.
In my experience, read
will return exactly the amount requested (if possible) when reading from a plain file, but may return less when reading from a pipe. These results are by no means guaranteed.
fileno
返回一个非这些的负数.例如,这些包括从普通文件、管道和套接字读取的句柄,但不包括 [1] 中提到的那些.
fileno
returns a non-negative number for these. These include, for example, handles that read from plain files, from pipes and from sockets, but not those mentioned in [1].
我指的是 IO::选择.
这篇关于`read` 和 `sysread` 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!