Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

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


In reply to Re^4: Is there a problem with using barewords as filehandles ? by haukex
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 having an uproarious good time at the Monastery: (4)
As of 2024-04-25 16:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found