Perl: Redirect STDOUT to two files

落花浮王杯 提交于 2019-12-10 01:23:17

问题


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!


来源:https://stackoverflow.com/questions/10519779/perl-redirect-stdout-to-two-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!