Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

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

by Discipulus (Canon)
on Jul 01, 2020 at 10:19 UTC ( [id://11118756]=note: print w/replies, xml ) Need Help??


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

Hello syphilis,

I imagine you already know why is better to use lexical filehandles with 3 arguments (enforcing the scope, autoclose,...), but as you are asking about official docs I cannot find anything. Well generally speaking perl documentation is very eterogenous in style. At least.

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

Nothing if you know what happens in this line and in the whole code you are writing.

perl -we "use strict; open FH, '>', 'text.txt'; open FH, '>', 'text.tx +t';" perl -we "use strict; open my $fh, '>', 'text.txt'; open my $fh, '>', +'text.txt';" "my" variable $fh masks earlier declaration in same scope at -e line 1 +.

I consider the second line less error prone, if you have thousand lines of code with many open FH statements the risk to forget to close one of them and mess the whole thing is high.

So it is a matter of (better) habits and nothing wrong per se. See also why-the-modern-perl-book-avoids-bareword-filehandles. And perlmaven.

More: if it is a matter of good habits I will enforce an exception also in the 2 arguments form of open What happens if you feed open my $in, $filename; with >/etc/passwd ? Infact 2 arguments form is dangeourous.

If it was my decision I will enhance strict pragma to deal with this:

use strict; open FH, '<', 'filename'; # dies open my $fh, 'filename'; # dies no strict 'open'; open FH, '<', 'filename'; # now ok open my $fh, 'filename'; # now ok

L*

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Replies are listed 'Best First'.
Re^2: Is there a problem with using barewords as filehandles ?
by tobyink (Canon) on Jul 01, 2020 at 16:13 UTC

    Ooh, I do like the idea of a new strict category as a way of enforcing this. Could also work well for traditional prototypes.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (3)
As of 2024-04-24 22:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found