Search code examples
perlwindows-console

In Perl, how to clone output to stdout and a log file in real time


I want to clone the output to the console as well as to a log file simultaneously. Here is my code snippet:

open STDOUT, ">>", "temp.txt" or die "";
system("echo This is a sample output..........");

But, here I only get output in the temp.txt and not on the console. Can you suggest a solution? I am using Windows.


Solution

  • It will output to STDOUT and temp.txt,

    use Capture::Tiny::Extended 'tee';
    
    tee(
      sub { 
        system("echo This is a sample output..........");
      },
      { stdout => "temp.txt" }
    );