perl中怎么让想输出的内容既输出到终端也写入文件
1个回答
展开全部
仅供参考:
tee("aa.out","bb.out");
print "anything you want\n";
print "other things";
close(STDOUT) || die "can't close STDOUT: $!";
sub tee {
my @output = @_;
my @handles = ();
for my $path (@output) {
my $fh; # open will fill this in
unless (open ($fh, ">", $path)) {
warn "cannot write to $path: $!";
next;
}
push @handles, $fh;
}
# reopen STDOUT in parent and return
return if my $pid = open(STDOUT, "|-");
die "cannot fork: $!" unless defined $pid;
# process STDIN in child
push @handles,STDOUT;
while (<STDIN>) {
for my $fh (@handles) {
print $fh $_ || die "tee output failed: $!";
}
}
for my $fh (@handles) {
close($fh) || die "tee closing failed: $!";
}
exit; # don't let the child return to main!
}
tee("aa.out","bb.out");
print "anything you want\n";
print "other things";
close(STDOUT) || die "can't close STDOUT: $!";
sub tee {
my @output = @_;
my @handles = ();
for my $path (@output) {
my $fh; # open will fill this in
unless (open ($fh, ">", $path)) {
warn "cannot write to $path: $!";
next;
}
push @handles, $fh;
}
# reopen STDOUT in parent and return
return if my $pid = open(STDOUT, "|-");
die "cannot fork: $!" unless defined $pid;
# process STDIN in child
push @handles,STDOUT;
while (<STDIN>) {
for my $fh (@handles) {
print $fh $_ || die "tee output failed: $!";
}
}
for my $fh (@handles) {
close($fh) || die "tee closing failed: $!";
}
exit; # don't let the child return to main!
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询