`read` 和 `sysread` 有什么区别? [英] What is the difference between `read` and `sysread`?

查看:36
本文介绍了`read` 和 `sysread` 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

readsysread 有非常相似的文档.两者有什么区别?

read and sysread have very similar documentation. What are the differences between the two?

推荐答案

关于 read:

  • read 支持 PerlIO 层.
  • read 适用于任何 Perl 文件句柄[1].
  • 读取缓冲区.
  • read 以 8 KiB 的固定大小块的形式从系统获取数据[2].
  • read 如果可用数据少于请求的数据,则可能会阻塞[3].
  • read supports PerlIO layers.
  • read works with any Perl file handle[1].
  • read buffers.
  • read obtains data from the system in fixed sized blocks of 8 KiB[2].
  • read may block if less data than requested is available[3].

关于sysread:

  • sysread 不支持 PerlIO 层(意味着它需要一个原始的也就是二进制句柄).
  • sysread 仅适用于映射到系统文件句柄/描述符的 Perl 文件句柄[4].
  • 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[4].
  • 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 文件句柄.
  • readselect[5],而 sysreadselect 兼容.
  • read可以为你解码,而sysread需要你自己解码.
  • read 对于非常小的读取应该更快,而 sysread 对于非常大的读取应该更快.
  • read works with any Perl file handle, while sysread is limited to Perl file handles mapped to a system file handle/descriptor.
  • read isn't compatible with select[5], while sysread is compatible with select.
  • read can perform decoding for you, while sysread requires that you do your own decoding.
  • read should be faster for very small reads, while sysread should be faster for very large reads.

注意事项:

  1. 例如,这些包括绑定文件句柄和使用 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` 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆