How do I redirect stdout and stderr output from a Perl script to a file on Windows? How do I redirect stdout and stderr output from a Perl script to a file on Windows? selenium selenium

How do I redirect stdout and stderr output from a Perl script to a file on Windows?


There is general problem with redirection in Perl for Windows.

The line that fails in Test::More package says:

open TESTOUT, ">&STDOUT" or die $!;

This fails when you invoke command as test.pl > outlog.log, as the file that you are redirecting STDOUT to is locked by cmd.exe, not by perl.exe. You cannot dup() it from perl.exe

You need to run:

perl test1.pl >output.log 2>&1

instead.


In all my test scripts I always configure test reporting and logging options (instead of using stdout). I also ran into the very same issue of redirecting output. You can use the solution listed above mine OR you can do what I did:

my $res_file = "C:\\Automation\\Results\\Test_Logs\\login_test_output.txt"; my $err_file = "C:\\Automation\\Results\\Test_Logs\\login_error_output.txt";open FH, ">$res_file" or die "couldn't open file: $!";FH->autoflush(1); # Make FileHandle HOT. Set to 0 to turn autoflush offTest::More->builder->output (*FH{IO}); # Redirect to test result file ($res_file)Test::More->builder->failure_output ($err_file); # and test failure output file

Using this approach I am able to redirect stdout and stderr output from my perl scripts to a file on Windows.