在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.01从Rakudo星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) 假设我们有这个模块:
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到一个字符串,或者,如果不可能,重定向到一个文件.是这样吗?