newbie - Output to multiple Filehandles

J

Jeremy Phillips

Hi,

I am writing one of my first perl scripts and would like to send
output to a logfile and stdout simultaneously. In addition, I'd like
to be able to specify that some output only be sent to STDOUT or only
to the logfile.

Currently, I am using the following to print to stdout and the logfile
simultaneously.

open(STDOUT, "| tee -a $logFileName");

To print only to the logfile, I create a filehandle for it in the
usual sense

open(LOGFILE, ">>$logFileName");

The problem is that I have no way of specifying output to only
standard output, leaving the logfile alone. Can anyone tell me how to
do this? I would greatly appreciate any help.

Thanks,

Jeremy
 
J

Jürgen Exner

Jeremy said:
I am writing one of my first perl scripts and would like to send
output to a logfile and stdout simultaneously. In addition, I'd like
to be able to specify that some output only be sent to STDOUT or only
to the logfile.

Currently, I am using the following to print to stdout and the logfile
simultaneously.

open(STDOUT, "| tee -a $logFileName");

Just don't do this...
To print only to the logfile, I create a filehandle for it in the
usual sense

open(LOGFILE, ">>$logFileName");

The problem is that I have no way of specifying output to only
standard output, leaving the logfile alone.

.... then you won't have this problem.
Can anyone tell me how to
do this?

Just print the output twice, once to STDOUT and once to the logfile.
You can even write a little subroutine (untested, only for clarification):

sub print_both {
print STDOUT @_;
print LOGFILE @_;
}

jue
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top