Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

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

by hippo (Chancellor)
on Jul 01, 2020 at 09:57 UTC ( #11118754=note: print w/replies, xml ) Need Help??


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

My understanding is that the problem is with the lack of restricted scope. Bareword filehandles are essentially globalpackage scoped (thanks, LanX) so your RD could easily stomp on someone else's RD in a module which your code usesthe same package and vice-versa. These possibilities for action-at-a-distance are the downside.

Is there some official perl documentation about any such issue ?

Not that I'm aware of. There's an oblique mention in the FAQ which says "Since you want to be a good programmer, you probably want to use a lexical filehandle" but makes no mention of why good programmers want to use lexical filehandles.

Update: Just found this very interesting counterpoint where luminaries such as brian_d_foy and merlyn argue in favour of keeping them (almost a decade ago). The last point about strict is also interesting in the light of recent events.

See also:

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

Replies are listed 'Best First'.
Re^2: Is there a problem with using barewords as filehandles ?
by LanX (Cardinal) on Jul 01, 2020 at 10:57 UTC
    > Bareword filehandles are essentially global

    Sorry for nitpicking but they are package variables not full globals.

    Conflicts can be avoided with proper use of package directives.

    Special variables are real globals, they are available everywhere but always belong to main:: package (IIRC)

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

      Sorry for nitpicking but they are package variables not full globals.

      No need to be sorry! You are quite correct and it's an important distinction. It has been so long since I actually used a user-defined bareword filehandle that I had forgotten that they are indeed package scoped.

      Package variables *are* global (visible everywhere).

Re^2: Is there a problem with using barewords as filehandles ?
by eyepopslikeamosquito (Bishop) on Jul 02, 2020 at 10:32 UTC

    A review of PBP which praises Damian's rationale without quoting it, alas

    If you are really keen you might be able to read at least part of the rationale by searching via google books, I just did that and here is some of Damian's rationale:

    ... using a bareword as a file handle causes Perl to store the corresponding input stream descriptor in the symbol table of the current package ... and if that symbol has already been used as a filehandle anywhere else in the same package, executing this open statement will close the previous file handle and replace it with the newly opened one ... bareword file handles are even more unreliable if there happens to be a subroutine of the same name currently in scope ...

    For completeness, from Perl Best Practices here are all Perl Best Practices that mention bareword or filehandle:

    • 45. Don't use barewords.
    • 125. Don't use bareword filehandles.
    • 126. Use indirect filehandles.
    • 127. If you have to use a package filehandle, localize it first.
    • 130. Close filehandles explicitly, and as soon as possible.
    • 133. Slurp a filehandle with a do block for purity.
    • 136. Always put filehandles in braces within any print statement.
    • 246. Don't tie variables or filehandles.

Re^2: Is there a problem with using barewords as filehandles ?
by ForgotPasswordAgain (Curate) on Jul 01, 2020 at 14:19 UTC
    As an example, one of my worst debugging sessions involved open's scope. Imagine you have a generic open_file() function in a script. The function uses a bareword FH, because whoever wrote it wasn't thinking about scope, they were just making sure a file was "open" (kinda related to the counterpoint you mentioned, that person might have learned enough to get by but not yet realize all the implications of what they were doing). Sometimes, deep in the code, you might open *another* file while processing the first one, leading to some bewildering action at a distince bugs...
      > because whoever wrote it wasn't thinking about scope,

      FWIW you can localize a FH inside the function's scope, but this implies some glob syntax IIRC.

      I think local *FH but ...

      • I'd need to look it up
      • it'll localize all other vars with the same symbol, like $FH too
      • it's a dynamic runtime scope not a lexical, ie don't call another sub from inside which doesn't localize the same symbol before using it

      Not that beginner friendly

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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (7)
As of 2020-08-10 14:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which rocket would you take to Mars?










    Results (58 votes). Check out past polls.

    Notices?