Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

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

by haukex (Archbishop)
on Jul 01, 2020 at 19:39 UTC ( [id://11118770]=note: print w/replies, xml ) Need Help??


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

What's so wrong with doing open RD, '<', 'file.txt' ?

I was involved with two discussions about this recently, lexical vs. local file handles and Re^2: Summing numbers in a file, so here are my two cents. To summarize the disadvantages of bareword filehandles:

  • They don't protect against typos.
  • They're package-globals.
  • They clash with package names and subs.
  • If you do want to localize them, that comes with even more disadvantages.
  • Package-global filehandles usually go out of scope much later than lexicals, so the automatic close doesn't really help.

I don't think that bareword filehandles should "never, ever" be used, and removing them would most likely break a lot of CPAN. But IMHO they shouldn't be used in new code, and newcomers should use lexicals instead. My understanding of Perl 7 is that it is an attempt to introduce defaults that are modern and reduce the cognitive load on the coder - sure, you can use bareword filehandles if you know what you're doing and are willing to manually check for all potentially typos and name clashes caused by them. In the beginning, I used to code without strict and warnings, and I can still remember the amount of time I would spend proofreading my code for such issues.

use of barewords as filehandles will be disallowed in perl 7

My understanding of Sawyer's talk is that the new default will be no bareword::filehandles, and you are free to write use bareword::filehandles.

Replies are listed 'Best First'.
Re^2: Is there a problem with using barewords as filehandles ?
by syphilis (Archbishop) on Jul 01, 2020 at 23:58 UTC
    .... you are free to write use bareword::filehandles

    Thanks for pointing that out.

    And thanks to everyone for all the additional relevant points and information.

    I don't do much with perl filehandles, but when I do, I've generally used barewords.
    I thought I would have made every dumb mistake that was possible with them, but I can see there are some mistakes that I hadn't encountered.
    (I wonder if that implies that I'm actually smarter than I thought ;-)

    Cheers,
    Rob

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2024-04-23 19:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found