如何将stdout和stderr输出从Perl脚本重定向到Windows上的文件?

时间:2021-10-01 07:08:10

I tried this:

我试过这个:

test1.pl >output.log 2>&1

but this is the result:

但这是结果:

Can't dup STDOUT:  Permission denied at C:/Perl/lib/Test/Builder.pm line 1376.
Compilation failed in require at C:/Perl/lib/Test/Builder/Module.pm line 3.
BEGIN failed--compilation aborted at C:/Perl/lib/Test/Builder/Module.pm line 3.
Compilation failed in require at C:/Perl/lib/Test/More.pm line 22.
BEGIN failed--compilation aborted at C:/Perl/lib/Test/More.pm line 22.
Compilation failed in require at C:/Perl/site/lib/Test/WWW/Selenium.pm line 72.
BEGIN failed--compilation aborted at C:/Perl/site/lib/Test/WWW/Selenium.pm line 72.
Compilation failed in require at C:\Software\selenium-remote-control-1.0-beta-2\tests\test1.pl line 5.
BEGIN failed--compilation aborted at C:\Software\selenium-remote-control-1.0-beta-2\tests\test1.pl line 5.

The script runs file as long as I don't try to redirect the output from the command line in any way.

只要我不尝试以任何方式重定向命令行的输出,脚本就会运行文件。

Here's my script, just in case that helps. (It's a Selenium test script):

这是我的脚本,以防万一。 (这是Selenium测试脚本):

#!C:/perl/bin/perl.exe -w
use strict;
use warnings;
use Time::HiRes qw(sleep);
use Test::WWW::Selenium;
use Test::More "no_plan";
use Test::Exception;

my $sel = Test::WWW::Selenium->new( host => "localhost",
                                    port => 4444,
                                    browser => "*chrome",
                                    browser_url => "http://localhost/" );
print "Start Time: " . localtime() . "\n";
for (my $count = 3000; $count > 0; $count--)
{
    print $count . " tests remaining.\n";
    $sel->open_ok("/home");
    $sel->click_ok("link=News");
    $sel->wait_for_page_to_load_ok("30000");
    $sel->click_ok("video");
    $sel->wait_for_page_to_load_ok("30000");
    $sel->click_ok("link=Sports");
    $sel->wait_for_page_to_load_ok("30000");
    $sel->click_ok("link=Movies");
    $sel->wait_for_page_to_load_ok("30000");
    $sel->click_ok("moremovies");
    $sel->wait_for_page_to_load_ok("30000");
}

print "End Time: " . localtime() . "\n";

2 个解决方案

#1


There is general problem with redirection in Perl for Windows.

在Perl for Windows中重定向存在一般问题。

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

Test :: More包中失败的行说:

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

当您将命令调用为test.pl> outlog.log时,这会失败,因为您将STDOUT重定向到的文件被cmd.exe锁定,而不是由perl.exe锁定。你不能从perl.exe复制()它

You need to run:

你需要运行:

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

instead.

#2


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:

在我的所有测试脚本中,我总是配置测试报告和日志记录选项(而不是使用stdout)。我也遇到了重定向输出的问题。你可以使用我上面列出的解决方案,或者你可以做我做的事情:

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 off

Test::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.

使用这种方法,我能够将我的perl脚本中的stdout和stderr输出重定向到Windows上的文件。

#1


There is general problem with redirection in Perl for Windows.

在Perl for Windows中重定向存在一般问题。

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

Test :: More包中失败的行说:

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

当您将命令调用为test.pl> outlog.log时,这会失败,因为您将STDOUT重定向到的文件被cmd.exe锁定,而不是由perl.exe锁定。你不能从perl.exe复制()它

You need to run:

你需要运行:

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

instead.

#2


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:

在我的所有测试脚本中,我总是配置测试报告和日志记录选项(而不是使用stdout)。我也遇到了重定向输出的问题。你可以使用我上面列出的解决方案,或者你可以做我做的事情:

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 off

Test::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.

使用这种方法,我能够将我的perl脚本中的stdout和stderr输出重定向到Windows上的文件。