capture output in perl

From: Geraldine 1 (geraldine_1_at_comcast.net)
Date: 02/28/05


To: beginners@perl.org
Date: Mon, 28 Feb 2005 13:54:03 +0000

hi,
I would like to capture all the output in my perl program during execution. I have code that basically opens and closes an output file multiple times just to write the output. Because I have system calls between lines of code, I have to close the file in order to capture the output from the system calls and write it to the same file (in this case /tmp/tmp.log) .

Not an elegant code. Is there a better way of rewriting the code?

Appreciate any suggestions/comments.

sample code:

open(OUTF, ">>/tmp/tmp.log" ) || die "Cannot write file";
if ($i==10) {
        print OUTF "i=10\n\n";
} else {
        print OUTF "i!=10\n\n";
}
close OUTF;
system ("ls nofile.txt 2>> /tmp/tmp.log");
...
<some operations>
...
open (OUTF, ">>/tmp/tmp.log" ) || die "Cannot write file";
if ($j==20) {
        print OUTF "j=20\n\n";
} else {
        print OUTF "j!=20\n\n";
}
close OUTF;

thanks.

Geraldine