Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
With 5.6 Perl introduced a new keyword, the latest and greatest scoping mechanism, our. I happen to understand exactly what it does and I believe I understand what it is for. But having just had the topic come up today, I am currently not convinced that it is actually better than what it replaces.

My criteria are based on what I have said before. By those criteria strict is good because it keeps you from accidentally using a variable that you didn't intend to (thereby catching spelling mistakes). my is wonderful because it gives you a way to make private variables guaranteed to be private. After all who cares that my $line may have the same name as another variable when it is guaranteed to be private and limited to 5 lines out of a 10 line function? And finally using vars takes care of the need for making it easy to keep track of shared variables.

So what do we get that we should want with the new keyword? And is it really worthwhile?

Well what it does differently is give us lexically scoped access to a global variable. So it does what vars is for but scoped like my. The idea is so we can share variables and make them limited to just a few functions. Just declare then within the places you need access.

But do I want that?

No. I don't want to have to put that declaration in multiple places. What happens if I get the name wrong in one block? That is an easy error to make (read I catch myself making it quite often) and I like having that caught. So I don't want the unnecessary synchronization. Also when you make it harder for me to find out in one place what all the shared variables I am using are, it is easier for me to get into namespace collisions.

So what is it for? I have been told it is a good way to get variables to be scoped by file. Well since my files tend to match my package declaration, that isn't a need I often feel. But when I do feel it I can use my. And I get the win of having it truly protected.

But, you say, what if I want it file scoped and I need access from two files? Well that synchronization problem I gave above? It is only worse when I have several files. But if I create a module that uses Exporter to export the variables then I can use it in multiple places with a check so that if I get the name wrong anywhere I am told about it. But, you say, I need to now synchronize the use command with the use vars. Right. But I always follow the directions in Exporter and use @EXPORT_OK. I find that I don't often get into synchronization errors between two adjacent lines. I get them between two files or between functions a page or two apart. My brain has lost track and I misname it. But I don't do that when the lines are right next to each other.

And so it goes. The very name, "our", suggests that its purpose is for sharing. But by the way it works if you try to use it for sharing then you leave yourself open for synchronization errors. And you can already accomplish the same tasks without that risk. And if you don't use it for sharing then it does the same thing as other declarations that we have without the incentive to make it easy for someone else learning the code to find out what is already shared because that information has been scattered through the code.

Really, until today I had never thought about it. But now that I have thought about it I don't like it. The more I think about it the less I like this feature. And so I am asking what need it fills that makes it worthwhile. Other than being the cool feature of the day...


In reply to Why is 'our' good? by tilly

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • 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 admiring the Monastery: (4)
    As of 2024-09-15 05:49 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      The PerlMonks site front end has:





      Results (21 votes). Check out past polls.

      Notices?
      erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.