Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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.

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (11)
As of 2024-04-18 14:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found