How to write in multiple output files in perl?

hi,

Hope you are doing good.

During my coding yesterday i got this challenge, actually not a challenge it like to optimize the code.

I am printing some statement to monitor the file progress in the log file an also to display it in the screen. so i ended up in the below statements.

print "\n   --- The file  was uploaded successfully\n";
print LOGFILE "\n   --- The file was uploaded successfully\n";

The first print statement was showing the user in the screen about the progress and the second statement was printing the statement in the logfile.

So the challenge to me is how to make it in the single statement instead two statements. is it possible?

Thanks.

Hi, try:

print "\n   --- The file was uploaded successfully\n" | tee LOGFILE

or to append to the log file:

print "\n   --- The file was uploaded successfully\n" | tee -a LOGFILE

Scrutinizer, this is perl, not shell.

You can use a function to make it more elegant:

sub log {
        my $arg=shift;
        print $arg;
        print LOGFILE $arg;
}

log("hey guys");
2 Likes

My perl is a little rusty, but how about:

open(LOG,"| tee logfile") or die "cannot open logfile";
...
print LOG "message\n";

However Corona's function allows for greater flexibility, ie different formatting for terminal and log file, or sending output to multiple log files.

Andrew

Oops, apparently did not read the last word in the title.

I thought it was Korn Shell, because he first is also a perfectly legitimate ksh command :slight_smile: (the second is not though)..

check out dup(). Find out more...

dodona, that's exactly what dup() doesn't do.