相关疑难解决方法(0)

我可以在Perl 5中为字符串创建文件句柄,我该如何在Perl 6中完成?

在Perl 5中,我可以创建一个字符串的文件句柄,并从字符串中读取或写入,就好像它是一个文件一样.这非常适合使用测试或模板.

例如:

use v5.10; use strict; use warnings;

my $text = "A\nB\nC\n";

open(my $fh, '<', \$text);

while(my $line = readline($fh)){
    print $line;
}
Run Code Online (Sandbox Code Playgroud)

我怎么能在Perl 6中做到这一点?以下为Perl 6的工作(至少对于上运行我的Perl6的实例MoarVM 2015.01Rakudo星2015年1月发布 64位的CentOS 6.5):

# Warning: This code does not work
use v6;

my $text = "A\nB\nC\n";

my $fh = $text;

while (my $line = $fh.get ) {
    $line.say;
}
# Warning: Example of nonfunctional code
Run Code Online (Sandbox Code Playgroud)

我收到错误消息:

No such method 'get' for invocant of type 'Str'
   in block <unit> …
Run Code Online (Sandbox Code Playgroud)

string perl filehandle perl6 raku

13
推荐指数
1
解决办法
436
查看次数

捕获模块输出

假设我们有这个模块:

unit module outputs;

say "Loaded";
Run Code Online (Sandbox Code Playgroud)

我们这样加载它

use v6;

use lib ".";

require "outputs.pm6";
Run Code Online (Sandbox Code Playgroud)

这将打印出"已加载" require.假设我们想要捕获该加载模块的标准输出.我们可以这样做,如果它是一个外部进程,但似乎没有办法重定向*OUT到一个字符串,或者,如果不可能,重定向到一个文件.是这样吗?

io perl6 raku

5
推荐指数
2
解决办法
99
查看次数

标签 统计

perl6 ×2

raku ×2

filehandle ×1

io ×1

perl ×1

string ×1