相关疑难解决方法(0)

Perl:在"显示"上打印,也在文件中打印

有没有办法在不重复打印"字符串"代码的情况下同时打印到"显​​示"和文件中?

我想做的事:

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的默认库中不可用.在默认库中是否有任何替代方案?

printing string perl file

4
推荐指数
2
解决办法
7517
查看次数

File :: Tee和打开管道到"tee"之间有什么区别吗?

我对下面引用的这个答案提出了一个问题,由friedo提出另一个问题.(我无权对此发表评论,所以我问这是一个问题.)

"你可以使用File :: Tee.

use File::Tee qw(tee);
tee STDOUT, '>>', 'some_file.out';
print "w00p w00p";
Run Code Online (Sandbox Code Playgroud)

如果File::Tee不可用,则可以使用管道轻松模拟:

open my $tee, "|-", "tee some_file.out";
print $tee "w00p w00p";
close $tee;
Run Code Online (Sandbox Code Playgroud)

这两个发球台都是一样的吗?或者是Perl和Linux/Unix中的一个?

perl tee

2
推荐指数
1
解决办法
1202
查看次数

标签 统计

perl ×2

file ×1

printing ×1

string ×1

tee ×1