简体   繁体   English

如何在Perl 6中打开字符串上的文件句柄?

[英]How to open a file handle on a string in Perl 6?

In Perl 5, I can open a filehandle on string like this: 在Perl 5中,我可以在字符串上打开文件句柄,如下所示:

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

I have a scenario that uses string as a filehandle and passes it to the open method: 我有一个场景,使用字符串作为文件句柄并将其传递给open方法:

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

where $message->payload is read from Kafka , and the content is a byte array. 其中$message->payload是从Kafka读取的,内容是一个字节数组。 raiph had a similar question , but it didn't answer my question. raiph有一个类似的问题 ,但它没有回答我的问题。

So I want to know how to open a filehandle on a string in Perl 6 like Perl 5 does? 所以我想知道如何在Perl 6中的字符串上打开文件句柄,就像Perl 5一样? These documentation pages have no information on this: 这些文档页面没有关于此的信息:

EDIT: See this question for how to do what @raiph says about opening a filehandle to a string. 编辑:有关如何执行@raiph关于打开文件句柄到字符串的说法,请参阅此问题 Also, read @raiph's comments. 另外,阅读@ raiph的评论。

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. 感谢@raiph澄清OP的含义。


The documentation has a section called Input/Output that describes this process. 该文档有一个名为输入/输出的部分,用于描述此过程。

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: 读取文件内容的一种方法是通过open函数打开文件,使用:r(读取)文件模式选项并在内容中使用slurp:

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

Here we explicitly close the filehandle using the close method on the IO::Handle object. 这里我们使用IO :: Handle对象上的close方法显式关闭文件句柄。 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" 

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. 通过将IO角色添加到文件名字符串,我们实际上可以将字符串作为文件对象本身引用,从而直接在其内容中啜饮。 Note that the slurp takes care of opening and closing the file for you. 请注意,slurp负责为您打开和关闭文件。

This is also found in the Perl5 to Perl6 pages as well. 这也可以在Perl5到Perl6页面中找到。

In Perl 5, a common idiom for reading the lines of a text file goes something like this: 在Perl 5中,读取文本文件行的常用习惯用法如下:

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

In Perl 6, this has been simplified to 在Perl 6中,这已经简化为

my @lines = "file".IO.lines; # auto-chomped

Further references to do this are found in the IO::Handle documentation: IO::Handle文档中提供了进一步的参考资料:

Instances of IO::Handle encapsulate an handle to manipulate input/output resources. IO::Handle实例封装了一个句柄来操作输入/输出资源。 Usually there is no need to create directly an IO::Handle instance, since it will be done by other roles and methods. 通常不需要直接创建IO::Handle实例,因为它将由其他角色和方法完成。 For instance, an IO::Path object provides an open method that returns an IO::Handle : 例如, IO::Path对象提供了一个返回IO::Handle的open方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM