See the first four items of Perl Best Practices Chapter 10 (I/O), namely:
- Don't use bareword filehandles
- Use indirect filehandles
- If you have to use a package filehandle, localize it first
- Use either the IO::File module or the three-argument form of open
Using lexical file handles is better style because:
- They are local variables and so avoid the generally evil programming practice of keeping state in global variables.
- They close themselves automatically when their lexical variable goes out of scope.
- They avoid the confusion of barewords. IMHO, barewords are an unfortunate Perl feature. Admittedly, they're handy when writing poems or playing golf. Historically, they probably exist only because of the improbable chance circumstance of Larry sitting right next to a brilliant poet (Sharon Hopkins) when designing early versions of Perl at JPL in the early 1990s and being led astray by her beautiful poetry readings.
The old two-argument form of open is subject to various security exploits as described
at Opening files securely in Perl.