I want to make a quick script that writes to a file if a file is given, or stdout if no file is given. It would be much easier for me to do this if I could start the script by pointing a new file handle, OUTFILE, to whichever of these is appropriate.
I know I can accomplish what I want by just pointing STDOUT to my output file when appropriate, but I’d rather not do that as I don’t want to confuse anyone using the script later and wondering why their print statements don’t work. Also, I’d still like to use the occasional print statement myself for troubleshooting purposes, and never want this print to the output file.
All of this said, what I’m looking for is something along the lines of:
open( OUTFILE, ($output ? ">$output" : STDOUT );
except that doesn’t work.
Ideas?
Some ways (globs):
Some ways (lexical var):
Some ways (other):