如果我在Perl 6中重新分配OUT,我怎么能把它改回到stdout?

时间:2017-11-15 22:08:18

标签: perl6

一个非常简单的问题,但我不能轻易找到答案。

我希望块中的所有async updateUser(obj, args) { const resultArray = await User.update( ... ) return resultArray[1] } 都转到文件中。但后来我希望我的输出返回say。怎么做?

STDOUT

1 个答案:

答案 0 :(得分:8)

简单的答案是只能在词汇上改变它

my $fh-foo = open "foo.txt", :w;
{
  my $*OUT = $fh-foo;
  say "Hello, foo! Printing to foo.txt";
}

say "This should be printed on the screen";
my $fh-foo = open "foo.txt", :w;

with $fh-foo -> $*OUT {
  say "Hello, foo! Printing to foo.txt";
}

say "This should be printed on the screen";

如果你必须解决其他人的代码,你可以像开头一样重新打开它。

my $fh-foo = open "foo.txt", :w;
$*OUT = $fh-foo;
say "Hello, foo! Printing to foo.txt";

$*OUT = IO::Handle.new( path => IO::Special.new('<STDOUT>') ).open();

say "This should be printed on the screen";