如何在 Perl 6 中打开一个字符串的文件句柄? [英] How to open a file handle on a string in Perl 6?

查看:71
本文介绍了如何在 Perl 6 中打开一个字符串的文件句柄?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Perl 5 中,我可以像这样在字符串上打开文件句柄:

In Perl 5, I can open a filehandle on string like this:

open my $kfh, "<", \$message->payload;

我有一个使用字符串作为文件句柄并将其传递给 open 方法的场景:

I have a scenario that uses string as a filehandle and passes it to the open method:

my $fh = new IO::Zlib;
open my $kfh, "<", \$message->payload;
if($fh->open($kfh, 'rb')){
   print <$fh>;
   $fh->close;
}

其中 $message->payload 是从 Kafka 读取的,并且内容是一个字节数组.raiph 有一个 类似问题,但它没有回答我的问题.

where $message->payload is read from Kafka, and the content is a byte array. raiph had a similar question, but it didn't answer my question.

所以我想知道如何像 Perl 5 那样在 Perl 6 中打开字符串上的文件句柄?这些文档页面没有这方面的信息:

So I want to know how to open a filehandle on a string in Perl 6 like Perl 5 does? These documentation pages have no information on this:

推荐答案

这个问题 关于如何按照@raiph 所说的打开一个字符串的文件句柄.另外,请阅读@raiph 的评论.

See this question for how to do what @raiph says about opening a filehandle to a string. Also, read @raiph's comments.

这是如何从字符串打开文件句柄到文件,而不是如何在不涉及文件的情况下打开文件句柄到字符串.感谢@raiph 澄清了 OP 的含义.

This is how to open a filehandle to a file from a string, not how to open a filehandle to a string without a file being involved. Thanks to @raiph for clarifying the OP's meaning.

文档中有一个名为输入/输出的部分描述了这个过程.

The documentation has a section called Input/Output that describes this process.

读取文件内容的一种方法是通过带有 :r(读取)文件模式选项的 open 函数打开文件并吞咽内容:

One way to read the contents of a file is to open the file via the open function with the :r (read) file mode option and slurp in the contents:

my $fh = open "testfile", :r;
my $contents = $fh.slurp-rest;
$fh.close;

这里我们使用 IO::Handle 对象上的 close 方法显式关闭文件句柄.这是一种非常传统的读取文件内容的方式.但是,同样可以更轻松、更清晰地完成,如下所示:

Here we explicitly close the filehandle using the close method on the IO::Handle object. This is a very traditional way of reading the contents of a file. However, the same can be done more easily and clearly like so:

my $contents = "testfile".IO.slurp;
# or in procedural form: 
$contents = slurp "testfile"

通过将 IO 角色添加到文件名字符串中,我们可以有效地将字符串引用为文件对象本身,从而直接读取其内容.请注意,slurp 负责为您打开和关闭文件.

By adding the IO role to the file name string, we are effectively able to refer to the string as the file object itself and thus slurp in its contents directly. Note that the slurp takes care of opening and closing the file for you.

这也可以在 Perl5 到 Perl6 页面中找到.

在 Perl 5 中,读取文本文件行的常用习惯用法如下所示:

In Perl 5, a common idiom for reading the lines of a text file goes something like this:

open my $fh, "<", "file" or die "$!";
my @lines = <$fh>;                # lines are NOT chomped 
close $fh;`

在 Perl 6 中,这被简化为

In Perl 6, this has been simplified to

my @lines = "file".IO.lines;# 自动压缩

IO::Handle 文档:

IO::Handle 的实例封装了一个句柄来操作输入/输出资源.通常不需要直接创建 IO::Handle 实例,因为它会由其他角色和方法来完成.例如,IO::Path 对象提供了一个返回 IO::Handle:

Instances of IO::Handle encapsulate an handle to manipulate input/output resources. Usually there is no need to create directly an IO::Handle instance, since it will be done by other roles and methods. For instance, an IO::Path object provides an open method that returns an IO::Handle:

my $fh = '/tmp/log.txt'.IO.open;
say $fh.^name; # OUTPUT: IO::Handle 

这篇关于如何在 Perl 6 中打开一个字符串的文件句柄?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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