Some say we should use a lexical filehandle instead of a typeglob, like this:
open $fh, $filename;
But most Perl books, including The Llama Book, use a typeglob, like this:
open LOGFILE, $filename;
So what are the differences? Which one is considered a better practice?
Lexical filehandles can be passed easily as arguments, filehandles cannot. Typeglobs can (or at least references to them can), but that’s kinda messy. Consider sticking with lexical variables, and make sure to declare them first, so you know that they’re really lexical and not local or global. I.e.
Also consider using
IO::HandleorIO::Fileas options. Used to beFileHandlebut was informed by ysth below thatFileHandlenow just uses ‘IO::Handle’ in turn, which is news to me since 5.6, but there’s a lot to learn here. 🙂Also, don’t forget
use strict🙂