http://qs321.pair.com?node_id=11118779


in reply to Is there a problem with using barewords as filehandles ?

Bareword filehandles have their places, particularly in simple scripts, where there really is almost no difference between them and lexical file handles — declaring a lexical at file-scope has almost exactly the same effect, and exactly the same effect if the convention of maintaining a 1:1 mapping between files and packages is followed.

However, a sub that opens a file and returns a handle should always return a lexical filehandle, unless its purpose is to return "THE" singleton filehandle for some resource, and in that case it should be using our to store the filehandle in a global. Generally, bareword filehandles should be limited to the mainline code in the top-level script. Subroutines should use lexical filehandles and modules should only contain subroutines.

In Perl, all "globals" are actually package variables, although a few special names (including the "punctuation variables") are forced into package main. The distinction is that a "global" variable is stored in a symbol table slot in some package. Package variables in Perl are globally-accessible, since the package namespace is itself global. The our keyword creates lexically-scoped aliases to package variables and is very convenient when needed.

A lexical declared at file scope is effectively a global variable and has all of the same problems in complex code, with the additional risk that file-scope lexicals can be shared across packages if multiple packages are defined in the same file. Global variables are generally dangerous in programming and I strongly favor the use of bareword filehandles at top-level in preference to declaring lexicals at file-scope and thinking that you are safe.

Perhaps the best would be to default to use bareword::filehandles in package main and no bareword::filehandles in other packages. This keeps the feature where it is most useful, to avoid the "head in sand" problem of using file-scope lexicals in the main script, while still pushing modules to avoid the feature unless it really is appropriate.

Replies are listed 'Best First'.
Re^2: Is there a problem with using barewords as filehandles ?
by Anonymous Monk on Jul 05, 2020 at 22:04 UTC
    I strongly favor the use of bareword filehandles at top-level in preference to declaring lexicals at file-scope and thinking that you are safe.

    the "head in sand" problem

    "driving isnt safe anyway so dont wear a seatbelt, that'll make you drive more careful"

      No, more like "do not act like that fancy sash you are wearing is a seatbelt" — lexicals declared at file-scope are global variables and acting like they are somehow different is sticking your head in the sand.

        lexicals declared at file-scope are global variables

        Yes, you have a point, but:

        acting like they are somehow different

        No, they are in fact different: as I've said plenty of times now, bareword filehandles are not protected against typos and they clash with package and sub names, among other things. There is no remedy for this other than "being careful" (good luck in the long term) or using lexicals. On the other hand:

        the additional risk that file-scope lexicals can be shared across packages if multiple packages are defined in the same file

        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.

        package main; open my $fh1, '>', 'x.txt' or die $!; print $fh1 "foo"; close $fh1; package foo; open my $fh2, '>', 'y.txt' or die $!; print $fh1 "bar"; # oops! close $fh2;
        there really is almost no difference between [bareword filehandles] and lexical file handles — declaring a lexical at file-scope has almost exactly the same effect, and exactly the same effect if the convention of maintaining a 1:1 mapping between files and packages is followed

        Once again, no, and I find this insistence quite misleading. If you're going to recommend bareword filehandles, at least acknowledge the issues they have.

        > lexicals declared at file-scope are global variables

        No, it's a different quality if a variable can be accessed and altered in a different file from another author.

        Furthermore putting a file scoped lexical behind the last sub would protect it from becoming closed over.

        Package vars are accessible nonetheless, no matter where you define them.

        perlglossary even makes a distinction between package vars and special vars.

        In Perl, only certain special variables are truly global—most variables (and all subroutines) exist only in the current package.

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