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

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.

Replies are listed 'Best First'.
Re^7: Using an "outer" lexical in a sub?
by Joost (Canon) on Nov 02, 2006 at 01:26 UTC
      You say "package Foo" in two different files. :-)

      As I said, this should be very rare. But sometimes you want to add or modify functionality in a package someone else wrote. (I've done this several times in Ruby where I'm changing core classes.)

      However there is a more common variation of the same thing. If you take a module and refactor it by using AutoLoader, then our variables are not visible in the autoloaded subroutines, but use vars would be.