How can I print the output to terminal and file at the same time?
$ perl foo.pl > foout.txt
does not allow me to see live process.
Is there any way I can see the output process real time and getting at the end the output of the screen on a file?
How can I print the output to terminal and file at the same time?
$ perl foo.pl > foout.txt
does not allow me to see live process.
Is there any way I can see the output process real time and getting at the end the output of the screen on a file?
See IO::Tee. This module will allow you to do this selectively with fine grained control within your program (there is also a less mature module called File::Tee which worked for me once but I would not recommend that for any serious project).
See also Log4perl for really fine grained control over what gets logged where and how.
For one off usage from the command line, as others have recommended, you can, of course, utilize the command line utility tee if you have access to it.
Or you could pipe it into a perl programs to print to the screen and a log file (that is if you are not on Unix or have a tee program)
perl foo.pl | perl myPipe.pl myFile.txt
where the data is captureed to myFile.txt and
myPipe.pl is
#
open OUTFILE,">$ARGV[0]" or die "Unable to open file: $ARGV[0]\n";
while(<>)
{
print;
print OUTFILE;
}
close OUTFILE;
This reads a line of input from STDIN and prints it to the screen and then to the file. When the end is reached, it will close the file