Perl: Redirect STDOUT to two files

2019-04-20 00:36发布

问题:

How can I redirect the STDOUT stream to two files (duplicates) within my Perl script? Currently I am just streaming into a single log file:

open(STDOUT, ">$out_file") or die "Can't open $out_file: $!\n";

What do I have to change? Thx.

回答1:

You can also use IO::Tee.

use strict;
use warnings;
use IO::Tee;

open(my $fh1,">","tee1") or die $!;
open(my $fh2,">","tee2") or die $!;

my $tee=IO::Tee->new($fh1,$fh2);

select $tee; #This makes $tee the default handle.

print "Hey!\n"; #Because of the select, you don't have to do print $tee "Hey!\n"

And yes, the output works:

> cat tee1
Hey!
> cat tee2
Hey!


回答2:

Use the tee PerlIO layer.

use PerlIO::Util;
*STDOUT->push_layer(tee => "/tmp/bar");
print "data\n";

$ perl tee_script.pl > /tmp/foo
$ cat /tmp/foo
data
$ cat /tmp/bar
data


回答3:

File::Tee provides the functionality you need.

use File::Tee qw( tee );
tee(STDOUT, '>', 'stdout.txt');


回答4:

If you're using a Unix-like system, use the tee utility.

$ perl -le 'print "Hello, world"' | tee /tmp/foo /tmp/bar
Hello, world

$ cat /tmp/foo /tmp/bar
Hello, world
Hello, world

To set up this duplication from within your program, set up a pipe from your STDOUT to an external tee process. Passing "|-" to open makes this easy to do.

#! /usr/bin/env perl

use strict;
use warnings;

my @copies = qw( /tmp/foo /tmp/bar );

open STDOUT, "|-", "tee", @copies or die "$0: tee failed: $!";

print "Hello, world!\n";

close STDOUT or warn "$0: close: $!";

Demo:

$ ./stdout-copies-demo
Hello, world!

$ cat /tmp/foo /tmp/bar
Hello, world!
Hello, world!