sachaer has asked for the wisdom of the Perl Monks concerning the following question:

I have two script files: program1.pl and program2.pl.

I would like the execution output to be recorded in a text file: output.txt.

What I have done is in program2.pl:

$filename = "output.txt"; system("perl /absolute/path/to/program1> $filename");
Then add progam2.pl to cron job list. The next morning I will be able to read output.txt.

What's in program1.pl:

... print "Start xxx job..."; # some code to do the job print "End of doing xxx job.";
Notice that I didn't define the output stream in program1.pl, so by default it goes to STDOUT.

I have no problem finding the two print statements in my output file: output.txt. But when I do the following, I started getting problems:

(program1.pl) $projectSourceDir ='/absolute/path'; chdir $projectSourceDir; print "Do cvs update ...\n"; open INFO, "cvs update -dP |"; my @cvsInfo = <INFO>; close INFO; my $cvsInfo= join("", @cvsInfo); print "$cvsInfo"; print "done\n\n";
What I want to do is perform a "cvs update" on a project's source code and then do something with the updated source code. I want to record the cvs update information, i.e. what file has been changed, what not, etc.. But if I do it in the way listed above, I sometimes get the CVS update in output.txt, most of the time I don't, although I am sure that source files are changed.

Thanks ahead.

Replies are listed 'Best First'.
Re: output to a file
by pzbagel (Chaplain) on Jun 19, 2003 at 19:15 UTC

    Are you sure files have changed? cvs outputs only files with a status to STDOUT which you are capturing. All the messages that start with "cvs update: Updating dir/subdir" are written to STDERR. I know you may not want to capture these messages, but perhaps for testing you should capture STDERR as well so you can at least ensure that the cvs is running when you run your script through cron. At least then you know your script is working. You can do this simply by changing:

    open INFO, "cvs update -dP |"; #to open INFO, "cvs update -dP 2>&1|";

    I tested your snippet and it seemed to work as advertised.

    HTH