I need to get the output of normal Perl code to the screen and into a logfile at the same time. However a problem is that the runtime of the tool can be hours. Using Capture::Tiny's tee that means the log file will only be written to once the script terminates, which is not very useful.
To further complicate things, i need to capture the output of straight perl from the same process, as well as that of processes called with system().
Lastly, due to employer restrictions it needs to work on Win32 as well.
What other options do i have?
If your program runs on a Linux/Unix platform, then you can use tee command. Tee reads stdin and writes to stdout and to a specified file.
Example:
The only caveat is that stdout and stderr will be merged in the same file.
Since you are on a Windows platform, you can search Google por tee.exe, or you can try this minimalistic perl version of tee:
Example:
I would really try to avoid modifying source code just to capture STDOUT and/or STDERR more and more if you are going to make system calls.
You can use IO::Tee.
If you prefer not using any module, create your own "myprint" function. It could print to stdout and, if a global flag is enabled, print to a logfile too.
Use PerlIO::Util.
Just tested it under Strawberry Perl 5.12.1 32 bit and it works perfectly, so it'll be cross platform. The below code does exactly as you'd expect. And since it modifies the actual STDOUT and STDERR file handles, any writes to them will automatically be teed.
Since none of the presented solutions were satisfactory i sat down and solved the problem on my own:
Capture::Tiny::Extended