bareword filehandles are not protected against typos

This is not entirely correct. While a typo in a bareword filehandle will not produce a compile-time error, it will produce a warning at run-time about using an unopened filehandle, and a unique typo will produce a compile-time warning about a name "used only once: possible typo".

they clash with package and sub names ... There is no remedy for this other than "being careful"

I strongly disagree with that statement; the standard conventions in Perl are to make bareword filehandles ALL UPPERCASE, while package names are MixedCase. Bareword filehandles do not clash with sub names at all: they are distinguished by syntax in all reasonable cases and are separate GV slots. The same name is allowed to have both a CODE value and an IO value in Perl.

While not wrong, this is only true under certain circumstances: if there are multiple packages in the same file, and those packages are in the same lexical scope, and the user has similarly named lexicals and makes a typo that happens to reference a previously declared lexical.

That was not specific to filehandles, but applies to file-scope lexical variables in general. A file-scope lexical is in-scope for the rest of the file. If you define multiple packages in the same file after declaring a file-scope lexical, they will all invisibly share that value. A bareword filehandle is in-scope for the rest of the containing package, possibly also the containing file, since the parser takes its use as a hint. These scopes have the same extent. A lexical declared at file-scope is indistinguishable from a global variable in good practice. (Declaring multiple packages in the same file is not good practice unless the "inner" packages are very closely related to the "main" package in the file.)

I find this insistence quite misleading

I find similar insistences that a lexical declared at file-scope is somehow not effectively a global variable merely because it is not in the symbol table similarly misleading. File-scope cuts across all other structures in the same file defined after a declaration. All following subs close over file-scope lexicals. Lexical variables declared at file scope have the same risks as global variables (use strict / use vars) and need to be recognized as such. This is not limited to filehandles, but it is often touted as a problem with bareword filehandles. At file-scope, lexicals have the same problem.


In reply to Re^5: Is there a problem with using barewords as filehandles ? by jcb
in thread Is there a problem with using barewords as filehandles ? by syphilis

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.