您可以随时使用以下过程来转储直接输出到文件中。
1)DUP系统STDOUT
文件描述符,2)open STDOUT
,3)系统,4)复制IO插槽回STDOUT
:
open(my $save_stdout, '>&1'); # dup the file
open(STDOUT, '>', '/path/to/output/glop'); # open STDOUT
system(qw<cmd.exe /C dir>); # system (on windows)
*::STDOUT = $save_stdout; # overwrite STDOUT{IO}
print "Back to STDOUT!"; # this should show up in output
但qx//
可能是你在找什么。
参考:perlopentut
当然,这可以概括:
sub command_to_file {
my $arg = shift;
my ($command, $rdir, $file) = $arg =~ /(.*?)\s*(>{1,2})\s*([^>]+)$/;
unless ($command) {
$command = $arg;
$arg = shift;
($rdir, $file) = $arg =~ /\s*(>{1,2})\s*([^>]*)$/;
if (!$rdir) {
($rdir, $file) = ('>', $arg);
}
elsif (!$file) {
$file = shift;
}
}
open(my $save_stdout, '>&1');
open(STDOUT, $rdir, $file);
print $command, "\n\n";
system(split /\s+/, $command);
*::STDOUT = $save_stdout;
return;
}
你的问题是混乱的。你想要输出原始命令“p4 change -o 3456789”吗?还是想要别的东西?如果你想要原始命令的输出,不要使用'system'。相反,使用反引号。 – sholsapp 2010-08-04 18:42:10