Lexical variables have a tighter scope than package variables. There are numerous advantages to limiting the scope of a variable. Predominantly, your handle will be closed if even if you don't execute the block to its end.
Reasons not to use a lexical variable for a file handle: Supporting Perl versions older than 5.6.0.
The 3-arg open can open files 2-arg open can't open. The 3-arg open is not subject to the injection bugs and attacks to which 2-arg open is vulnerable.
Reasons not to 3-arg open. Wanting to open STDIN/STDOUT using '-'. Wanting to work file descriptors. Wanting to open a pipe.
|