Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I write the same text to two separate file handles using Perl?

I need to output the same text to two different files (it is a application requirement, which I am testing). Now, I do not wish to open two file handles, write two lines to each, then close them a dozen times in my code.

Is there a simple way, perhaps using a single line in Perl (but not in the CLI!), to send the same text to two different files?

like image 486
gagneet Avatar asked Dec 03 '22 08:12

gagneet


1 Answers

Use IO::Tee.

From the documentation's example:

use IO::Tee;
$tee = IO::Tee->new($handle1, $handle2);
print $tee "foo", "bar";
like image 93
Rob Kennedy Avatar answered May 15 '23 04:05

Rob Kennedy