在Perl5中,我可以这样打开字符串上的文件句柄:
open my $kfh, "<", \$message->payload;
我有一个场景使用字符串作为文件句柄,并将其传递给 open 方法:
open
my $fh = new IO::Zlib; open my $kfh, "<", \$message->payload; if($fh->open($kfh, 'rb')){ print <$fh>; $fh->close; }
哪里 $message->payload 从中读取 Kafka ,内容是字节数组。 raiph 有一个 similar question 但它没有回答我的问题。
$message->payload
所以我想知道如何像Perl5一样在Perl6中打开一个字符串上的文件句柄?这些文档页没有关于以下内容的信息:
this question
Input/Output
my $fh = open "testfile", :r; my $contents = $fh.slurp-rest; $fh.close; my $contents = "testfile".IO.slurp; # or in procedural form: $contents = slurp "testfile"
my $fh = open "testfile", :r; my $contents = $fh.slurp-rest; $fh.close;
my $contents = "testfile".IO.slurp; # or in procedural form: $contents = slurp "testfile"
This is also found in the Perl5 to Perl6 pages as well.
open my $fh, "<", "file" or die "$!"; my @lines = <$fh>; # lines are NOT chomped close $fh;` my @lines = "file".IO.lines; # auto-chomped
open my $fh, "<", "file" or die "$!"; my @lines = <$fh>; # lines are NOT chomped close $fh;`
my @lines = "file".IO.lines; # auto-chomped
IO::Handle
IO::Path my $fh = '/tmp/log.txt'.IO.open; say $fh.^name; # OUTPUT: IO::Handle
IO::Path
my $fh = '/tmp/log.txt'.IO.open; say $fh.^name; # OUTPUT: IO::Handle