On 3/29/07, lakshmi priya <[EMAIL PROTECTED]> wrote:
Hi all,
I have written a perl script that prints the output to the terminal. I
also want to write the output to a file. So, what I have done is write the
output to a file and then print the file. I was just wondering if there any
way to write to both - terminal and file at the same time?
With regards,
laddu
There sure is, and I just learned how the other day.
You will first need to open the file (like you have been), print to
it, print to STDOUT, and then close the file.
Try something like this:
use IO::Handle;
#open the output file
open(OUTFILE, 'somefile.txt');
# print to the output file
print OUTFILE "This goes to the file";
# print to STDOUT (i.e. the terminal)
print "This goes to STDOUT";
# use the following command to force Perl buffer to flush,
# otherwise it won't send to output until buffer is full, which
# could really look like the script is just printing at random intervals
OUTFILE->autoflush(1);
# you can do the same thing with STDOUT
STDOUT->autoflush(1);
# close the output file
close(OUTFILE);
Jason
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/