代码之家  ›  专栏  ›  技术社区  ›  Tom Corelis

Linux/Perl:除了stdout和stderr之外的其他输出缓冲区?

  •  8
  • Tom Corelis  · 技术社区  · 15 年前

    出于好奇,是否可以从Perl脚本中创建、实例化或访问除stdout和stderr之外的其他输出缓冲区?

    用例将是附加的输出,以导入到文件或其他命令,例如 ./doublerainbow.pl 3>full_on.txt 4>all_the_way!.txt

    1 回复  |  直到 11 年前
        1
  •  12
  •   ikegami Gilles Quénot    11 年前

    当然。这个 open 命令 >&= 模式允许您打开任意文件描述符上的文件句柄。

    # perl 4fd.pl > file1 2> file2 3> file3 4> file4 5< file5
    
    open STDFOO, '>&=3';
    open STDBAR, '>&=4';
    open STDBAZ, '<&=5';   # works for input handles, too
    
    print STDOUT "hello\n";
    print STDERR "world\n";
    print STDFOO "42\n";
    print STDBAR <STDBAZ>;
    

    $ 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