Perl:在“显示器"上打印并成一个文件 [英] Perl: Print on the "display" and also into a file

查看:55
本文介绍了Perl:在“显示器"上打印并成一个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在不重复打印字符串"代码的情况下同时打印到显​​示器"和文件中?

Is there any way to print both onto the "display" and also into a file at the same time without repeating the print "string" code?

我想做什么:

if ($ofile) {
   open (FILE, '>>', "file");
   print "Hello" #some code#; #prints on the display and into the file
}

代替:

if ($ofile) { open (FILE, '>>', "file"); }
print "Hello";
if ($ofile) { print FILE "Hello"; }

尝试使用谷歌搜索,但我发现的只是一个功能,或者两个功能都不在一起.

Tried googling but all I found was either or, not both features together.

编辑以添加问题:

然后使用IO :: Tee创建一个新的tee'd句柄,然后选择$ tee,以便打印默认情况下使用它.–埃里克·斯特罗姆

Then use IO::Tee to create a new tee'd handle, and then select $tee so that print uses it by default. – Eric Strom

@EricStrom创建一个新的T型手柄是什么意思?您是说这个 Local :: TeeOutput 吗?search.cpan.org/~mschilli/Log-Log4perl-1.34/lib/Log/Log4perl.pm

@EricStrom What do you mean by create a new tee'd handle? Do you mean this Local::TeeOutput? search.cpan.org/~mschilli/Log-Log4perl-1.34/lib/Log/Log4perl.pm

@EricStrom您有例子吗?

@EricStrom Do you have an example?

@EricStrom Local :: TeeOutput在Strawberry Perl的默认库中不可用.默认库中有其他替代方法吗?

@EricStrom Local::TeeOutput is not available in the default library for Strawberry Perl. Is there any alternative that's inside the default library?

推荐答案

当然,使用 IOCPAN上的::: Tee .

my $tee = IO::Tee->new( \*STDOUT, \*STDERR, $John, $Tan );
print $tee "HELLO!\n";

要更改perl的默认句柄:

To change perl's default handle:

select $tee;
print "HELLO!\n";

这篇关于Perl:在“显示器"上打印并成一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆