10.12. Printing to Filehandles
It's easy to lose a lexical filehandle that's being used in the argument list of a print: print $file $name, $rank, $serial_num, "\n"; Putting braces around the filehandle helps it stand out clearly: print {$file} $name, $rank, $serial_num, "\n"; The braces also convey your intentions regarding that variable; namely, that you really did mean it to be treated as a filehandle, and didn't just forget a comma. You should also use the braces if you need to print to a package-scoped filehandle: print {*STDERR} $name, $rank, $serial_num, "\n"; Another acceptable alternative is to load the IO::Handle module and then use Perl's object-oriented I/O interface: use IO::Handle; $file->print( $name, $rank, $serial_num, "\n" ); *STDERR->print( $name, $rank, $serial_num, "\n" ); |