I’m attempting to print out to two different files. For some reason, print statements work fine for one file, but not for the other. When I run this program, filter2.out consists of a single line that reads “Beginning”. filter2.err remains empty.
open(OUTPUT, "> Filter2/filter2.out");
open(ERROR, "> Filter2/filter2.err");
print OUTPUT "Beginning\n";
print ERROR "Beginning\n";
UPDATE: So I was running this at the beginning of a larger program and realized that it only updates the ERROR file in batches or when the file is closed. Any idea why this occurs?
Consider adding
to the top of your script. These statements will help catch errors that are otherwise silently ignored by Perl. In addition, consider adding error checking to your
opencalls: in all likelihood, it’s not actually opening. I’d write it like this:for example, by just adding adding strict and warnings I got:
Hmm…!
By adding error checking, I got:
Aah! Looking, I didn’t have the folder. After I added the folder, everything ran fine. You’ll probably find something similar.
Finally, you should probably also use the modern, lexical file handles. This helps catch other errors (like re-used handle names.) Thus, the final script would look like:
Viola! Now you can see exactly where the problem fails, as it fails, and make sure that other libraries or code you write later can’t accidentally interfere with your file handles.