我知道输出流STDOUT和STDERR.每当你打印到STDOUT时,在unix shell中你可以像这样重定向输出......
deviolog@home:~$ perl test_script.pl > output.txt
Run Code Online (Sandbox Code Playgroud)
要么
deviolog@home:~$ perl test_script.pl 1> output.txt
Run Code Online (Sandbox Code Playgroud)
当您打印到STDERR时,它看起来相同,但您切换到"频道"(?)编号2:
deviolog@home:~$ perl test_script.pl 2> output.txt
Run Code Online (Sandbox Code Playgroud)
我可以在output.txt中找到我当时正在打印的错误输出.
我的问题是,我可以以某种方式访问"频道"3号吗?有什么像......
print STDX "Hello World!\n";
Run Code Online (Sandbox Code Playgroud)
...允许重定向,如下所示?
deviolog@home:~$ perl test_script.pl 3> output.txt
Run Code Online (Sandbox Code Playgroud)
ps一个子问题将是那些"通道"^ _ ^的术语
您可以使用,附加到模式并使用文件描述符作为文件名,为打开的文件描述符(fd)创建Perl文件句柄.在您的情况下,您将使用以下内容:open&=
open(my $fh, '>&=', 3)
Run Code Online (Sandbox Code Playgroud)
例如,
$ perl -E'
open(my $fh, ">&=", 3) or die $!;
say fileno($fh);
say $fh "meow";
' 3>output.txt
3
$ cat output.txt
meow
Run Code Online (Sandbox Code Playgroud)