Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^2: Is there a problem with using barewords as filehandles ?

by Anonymous Monk
on Jul 05, 2020 at 22:04 UTC ( [id://11118942]=note: print w/replies, xml ) Need Help??


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

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"

  • Comment on Re^2: Is there a problem with using barewords as filehandles ?

Replies are listed 'Best First'.
Re^3: Is there a problem with using barewords as filehandles ?
by jcb (Parson) on Jul 06, 2020 at 00:58 UTC

    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.

        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.

      > 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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11118942]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-04-24 20:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found