有没有办法在不重复打印"字符串"代码的情况下同时打印到"显示"和文件中?
我想做的事:
if ($ofile) {
open (FILE, '>>', "file");
print "Hello" #some code#; #prints on the display and into the file
}
Run Code Online (Sandbox Code Playgroud)
代替:
if ($ofile) { open (FILE, '>>', "file"); }
print "Hello";
if ($ofile) { print FILE "Hello"; }
Run Code Online (Sandbox Code Playgroud)
尝试使用谷歌搜索但我发现的只是或者,不是两个功能在一起.
编辑以添加问题:
然后使用IO :: Tee创建一个新的T形手柄,然后选择$ tee,以便print默认使用它. - Eric Strom
@EricStrom创建一个新的T恤手柄是什么意思?你是说这个Local::TeeOutput吗?search.cpan.org/~mschilli/Log-Log4perl-1.34/lib/Log/Log4perl.pm
@EricStrom你有一个例子吗?
@EricStrom Local :: TeeOutput在Strawberry Perl的默认库中不可用.在默认库中是否有任何替代方案?
我对下面引用的这个答案提出了一个问题,由friedo提出另一个问题.(我无权对此发表评论,所以我问这是一个问题.)
"你可以使用File :: Tee.
Run Code Online (Sandbox Code Playgroud)use File::Tee qw(tee); tee STDOUT, '>>', 'some_file.out'; print "w00p w00p";如果
File::Tee不可用,则可以使用管道轻松模拟:Run Code Online (Sandbox Code Playgroud)open my $tee, "|-", "tee some_file.out"; print $tee "w00p w00p"; close $tee;
这两个发球台都是一样的吗?或者是Perl和Linux/Unix中的一个?