gpt4 book ai didi

raku - 如果我在 Perl 6 中重新分配了 OUT,如何将其改回 stdout?

转载 作者:行者123 更新时间:2023-12-04 17:43:36 26 4
gpt4 key购买 nike

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

我要全say在一个块中转到一个文件。但是我希望我的输出返回到 STDOUT .怎么做?

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

$*OUT = ????;
say "This should be printed on the screen";

最佳答案

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

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";

关于raku - 如果我在 Perl 6 中重新分配了 OUT,如何将其改回 stdout?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47318139/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com