in reply to Re^3: Using an "outer" lexical in a sub?
in thread Using an "outer" lexical in a sub?

Some of us have figured out that it is better to use vars in modern days as well. And if you're going to use our, then use it in the biggest possible scope.

Seriously, in my eyes our is a language misfeature. My understanding is that it was introduced as a hook upon which further features can be hung. In Perl 6 those features will be hung upon it and it makes more sense. But in Perl 5 it is worse than what it replaced.

However it is great for surprising the maintainer. For instance:

package Foo; our $_ = "gotcha"; for (1, 2, 3) { print; print ": $_\n"; }

Replies are listed 'Best First'.
Re^5: Using an "outer" lexical in a sub?
by Joost (Canon) on Nov 01, 2006 at 23:49 UTC
      I first questioned its utility in Why is 'our' good?. There are four basic problems that I know of with it. (I'm not counting the silly one that I pointed out above.)
      1. You have 2 packages in one file and didn't intend to use the variable from one package in the code for the other. This should be uncommon but not that uncommon.
      2. You have 2 files that access one package and you introduce synchronization issues. This is rarer than the first.
      3. A developer mistakenly tries to use our like you should use my. Now you have lots of room for a typo causing the variable accessed in one place to be different than the one in another, and strict.pm doesn't help.
      4. Using our is a gratuitous portability problem for people on Perl 5.005_05. Back when I first thought about this issue, that was a much bigger deal than it is today.
      Its only advantage (in Perl 5) is that it involves slightly less typing. So while I consider it distinctly worse, it isn't really THAT bad.

      As Larry pointed out in response to my node, its real purpose was a place to hang declarations about the variable in scope. For instance I believe one can say things like our Dog $spot is constant; and that has told Perl 6 that $spot must be something of type Dog, and in this lexical scope we are not allowed to change $spot. That's all functionality that couldn't be done with the old declaration.