本文介绍了Perl:将STDOUT重定向到两个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将 STDOUT
流重定向到我的Perl脚本中的两个文件(重复项)?目前我只是流式传输到一个日志文件中:
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";
我需要更改什么? Thx。
What do I have to change? Thx.
推荐答案
您也可以使用。
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"
是的,输出有效:
> cat tee1
Hey!
> cat tee2
Hey!
这篇关于Perl:将STDOUT重定向到两个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!