我知道输出流 STDOUT 和 STDERR。每当你打印到 STDOUT 时,在 unix shell 中你可以像这样重定向输出......
deviolog@home:~$ perl test_script.pl > output.txt
或
deviolog@home:~$ perl test_script.pl 1> output.txt
当你打印到 STDERR 时,它看起来是一样的,但你切换到“ channel ”(?)编号 2:
deviolog@home:~$ perl test_script.pl 2> output.txt
我可以在output.txt中找到我当时打印为错误输出的内容。
我的问题是,我可以以某种方式访问“ channel ”3 吗? 有没有类似...
print STDX "Hello World!\n";
...允许像下面这样的重定向?
deviolog@home:~$ perl test_script.pl 3> output.txt
附:一个子问题是关于这些“ channel ”的术语^_^
最佳答案
您可以使用 open
为打开的文件描述符 (fd) 创建 Perl 文件句柄,将 &=
附加到模式并使用文件描述符作为文件名。在您的情况下,您将使用以下内容:
open(my $fh, '>&=', 3)
例如,
$ perl -E'
open(my $fh, ">&=", 3) or die $!;
say fileno($fh);
say $fh "meow";
' 3>output.txt
3
$ cat output.txt
meow
关于perl - Perl 中的输出流 : STDERR, STDOUT,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23159012/