From the camel book (3rd ed, p. 756): The primary use of an our declaration is to hide the variable from the effects of a use strict "vars" declaration; since the variable is masquerading as a my variable, you are permitted to use the declared global variable without qualifying it with its package. However, just like the my variable, this only works within the lexical scope of the our declaration. In this respect, it differs from use vars, which affects the entire package and is not lexically scoped.
I am trying to think of some examples where this is a clear advantage. Either some tricks it makes work or ways it can be leveraged to make code cleaner. If all it was used for is declaring global variables then either declaring them up front with "my" or use vars would do. I figure there must be some higher reason for the "our" feature. | [reply] |
You don't declare global variables, like you do my variables, or even local variables. You didn't declare them with use vars, and you don't declare them with our. Global variables spring into existence upon being used; they are not dependent upon mere mortals for their existence.
The only thing that our buys you is the ability to refer to a global variable without its package specifier. If you can accomplish what you need to with lexical variables, you should use them. If you need globals, you may use them. If you use globals, you may use our to save typing the fully-qualified name every time you reference them. But that is all it buys you. our did not introduce any new kind of variable (nor did use vars).
You could do it with use vars, but that (as the docs you quoted said) doesn't give you as tight of control over where the global can be referenced as a familiar. It is, according to its own docs, obsolete.
Caution: Contents may have been coded under pressure.
| [reply] [d/l] [select] |