There's been a lot of traffic these days about my, local and our. I read some threads on this topic in this most venerable monastery, and I'm reading Amelia as well (aka the Camel Book, 3rd Edition), so I'm now quite comfortable saying that I do understand how all of this works, at least technically.

This is all fine from the compiler's point of view. But, I want now to focus on the reader's point of view, for a declaration gives hints to the reader about how the code works. Here are my thoughts about use vars vs. our, willfully exposed to your wisdom and enlightened comments|remarks|flames.

So, use vars displays a list of all globals for all to see, right at the beginning of the source file, whereas our highlights the parts of the code where the globals might be altered or referenced.

Now, what would be really cool, IMHO, would be to combine the best of both worlds, i.e. say at the beginning of the source file "Here's the list of all our globals, but no block of code will access any one of them unless it explicitly declares so."...

--bwana147

Replies are listed 'Best First'.
(satire) Re: Of the meaning of 'our'
by japhy (Canon) on Jun 25, 2001 at 19:53 UTC
    You can do that right now with the # globals directive:
    # globals: $foo, @bar, %quux
    After that point, you'll still have to use our to access them without a package name, but they're visible to the reader.

    japhy -- Perl and Regex Hacker
      # Global( $foo, @bar=, =%quux );

      Indicates the @bar is overwritten and %quux is not modified. (:

              - tye (but my friends call me "Tye")
       ++$japhy if /humour/;

      Good for you for beating me to the exact same comment!

      "Argument is futile - you will be ignorralated!"

(tye)Re: Of the meaning of 'our'
by tye (Sage) on Jun 25, 2001 at 21:40 UTC

    Well, our can be used in the way you mention. It can also be used very much like use vars is used. However, using our like use vars is a bad idea (see Why is 'our' good?).

    So, if I wasn't worried about supporting pre-5.6 Perl, I might write code like:

    use strict; package A; { our %OPT= ( fred => 1, barney => 2, ); } sub getopts { our %OPT; # ... }
    but probably only if %OPT needed to be global, that is accessible outside the scope of this file. Otherwise I'd just use:
    my %OPT= ( ... ); sub getopts { # Global( =%OPT );

            - tye (but my friends call me "Tye")