in reply to Re^3: On Backwards Compatibility and Bareword Filehandles
in thread On Backwards Compatibility and Bareword Filehandles

That is a short summary of the impression that I got from previous discussions on the topic of indirect filehandles in Perl, particularly in the case of code at the top-level of the main script, where a (global) bareword filehandle and a file-scope lexical have effectively the same scope.

There seems to be some confusion here — I am arguing that bareword filehandles should remain in the language, but suggesting a compromise of making bareword filehandles lexical variables. I expect that the arguments in favor of declaring bareword filehandles (and giving them lexical scope) are essentially the rationale for use strict 'subs' prohibiting the old "bareword-as-unquoted-string" behavior.

Replies are listed 'Best First'.
Re^5: On Backwards Compatibility and Bareword Filehandles
by ikegami (Patriarch) on Jul 19, 2020 at 11:50 UTC

    where a (global) bareword filehandle and a file-scope lexical have effectively the same scope.

    Globs (which you call bareword file handle) and other package variables are global, which means visible everywhere.

    File-scoped lexicals are still lexically-scoped. They aren't seen outside the file.

      package variables are global, which means visible everywhere.

      Close — fully-qualified names are visible everywhere, but most "global" variables are implicitly scoped to the current package.

      File-scoped lexicals are still lexically-scoped. They aren't seen outside the file.

      If a 1:1 correspondence between files and packages (as is convention) is maintained, those are equivalent, with the exception that you can refer to a package "global" variable from elsewhere if you specifically do so. In terms of "accidents", the risks for a package variable and file-scope lexical are very similar.

        I spend much time training colleagues in Perl.

        I'm pretty happy when they understood scoping rules and follow the basics of declaring with my.

        And believe me, nobody there thought much about using package before I started there.

        There is simply not much room to tell a beginner about all the specialties of a package scoped bareword, which needs to be treated with local, the internals of type-globs and how to pass them around as reference with \*FH .

        Yes Perl has many ways to do it and it's impressive how I can always come up with a nifty trick to fix the sins of a 15 year old code base.

        But orthogonality rules, they don't need to learn how to use a baseball bat to drive a nail into the wall, if there is a hammer called my $FH and they already learned to use that hammer with many other nails ...

        And there is no issue with "backwards compatibility" whatsoever!

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery

        In terms of "accidents", the risks for a package variable and file-scope lexical are very similar.

        I didn't say otherwise. The fact that they have similar risks doesn't make them the same thing.