出于好奇,是否可以在 Perl 脚本中创建、实例化或以其他方式访问除 STDOUT 和 STDERR 之外的其他输出缓冲区?
用例是额外的输出以通过管道传输到文件或其他命令,例如 ./doublerainbow.pl 3>full_on.txt 4>all_the_way!.txt
最佳答案
当然。使用 >&=
模式的 open
命令允许您打开任意文件描述符上的文件句柄。
# perl 4fd.pl > file1 2> file2 3> file3 4> file4 5< file5
open NONSTDFOO, '>&=3';
open NONSTDBAR, '>&=4';
open NONSTDBAZ, '<&=5'; # works for input handles, too
print STDOUT "hello\n";
print STDERR "world\n";
print NONSTDFOO "42\n";
print NONSTDBAR <NONSTDBAZ>;
$ echo pppbbbttt > file5
$ perl 4fd.pl >file1 2>file2 3>file3 4>file4 5<file5
$ cat file1
hello
$ cat file3
42
$ cat file4 file2
pppbbbttt
world
关于Linux/珀尔 : Additional output buffers other than STDOUT and STDERR?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3347940/