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.
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.
Use the
tee
PerlIO layer.If you're using a Unix-like system, use the tee utility.
To set up this duplication from within your program, set up a pipe from your
STDOUT
to an external tee process. Passing"|-"
toopen
makes this easy to do.Demo:
File::Tee provides the functionality you need.
You can also use
IO::Tee
.And yes, the output works: