Perl: Print on the “display” and also into a file

2020-02-14 04:41发布

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?

What I want to do:

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

instead of:

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.

Edit to add questions:

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 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 Do you have an example?

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

4条回答
我只想做你的唯一
2楼-- · 2020-02-14 04:51

Sure, with IO::Tee on CPAN.

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

To change perl's default handle:

select $tee;
print "HELLO!\n";
查看更多
我命由我不由天
3楼-- · 2020-02-14 04:55

If this is for logging, Consider using Log::Log4perl

You can have the same stuff log to the screen , file and even a database.

This is a complete example of logging to both the screen and a file.

use strict;
use Log::Log4perl qw(:easy);
Log::Log4perl->easy_init("INFO");

my $logConfiguration = qq(
log4perl.logger        = INFO, Logfile, Screen
log4perl.appender.Logfile          =  Log::Log4perl::Appender::File
log4perl.appender.Logfile.filename = C:\\temp\\file.log
log4perl.appender.Logfile.layout   = Log::Log4perl::Layout::PatternLayout
log4perl.appender.Logfile.layout.ConversionPattern =%p[%d{MM/dd HH:mm} %3L] %m%n
log4perl.appender.Screen         = Log::Log4perl::Appender::Screen
log4perl.appender.Screen.stderr  = 0
log4perl.appender.Screen.layout = Log::Log4perl::Layout::SimpleLayout
log4perl.appender.Screen.layout.ConversionPattern = %m%n
);

Log::Log4perl::init( \$logConfiguration );

INFO "This line will be logged";

ERROR "This error will be logged";

LOGDIE "Fatal error will be logged";

The configuration illustrates that both a file (Logfile) and the screen (Screen) will be appended when either INFO , ERROR etc are called.

The FAQ covers more questions

查看更多
成全新的幸福
4楼-- · 2020-02-14 05:18

Why not make subroutine for the task?

Also, many logging packages like Log::Log4perl support writing to both console and the file via quite simple configuration.

查看更多
登录 后发表回答