| [reply] [d/l] [select] |
| [reply] [d/l] [select] |
Imagine you had a package:
package MyPack;
my $a;
As defined above, there is no way for anything outside the scope of MyPack to access $a. Of course, with Exporter and a few other standard class tricks, this is not hard to fix, but using Exporter for every class is a nuicence.
Using 'our' makes the variable known at the global scope level (that is, everyone can access it now):
package MyPack;
our $a;
Anywhere outside of MyPack, I can now get the value of $a via the variable $MyPack::a.
So 'our' can be considered to be declaring which variables are public in a object-oriented sense. It mostly replaces the functionality of Exporter which can be awkward to use.
Dr. Michael K. Neylon - mneylon-pm@masemware.com
||
"You've left the lens cap of your mind on again, Pinky" - The Brain
| [reply] [d/l] [select] |
Never. (: Use use vars when you need a package global, such as a variable you plan to export. This is the same case when you might want to use our but doing so just makes your code less portable. If you aren't writing a module, then my is what you should use nearly all of the time. See other response for more information on some of the exceptions.
-
tye
(but my friends call me "Tye")
| [reply] |
For an indepth analysis of this look no further than the
pages of the the one and only M-J. Dominus.
Short answer (stolen from MJD)
'Always use my, never use local.'
To learn about namespaces my/our/local see:
Coping with Scoping
http://perl.plover.com/FAQs/Namespaces.html
In this next article MJD partially recants from the brief advice
to never use local, always use my
Seven Useful Uses of local
http://perl.plover.com/local.html
Hope this helps, I found these articles very useful
Cheers
tachyon
my $answer =[q;7566626f206f7420756f7920656d6f636c65772049;];
print join''=>reverse split''=>pack H42=>$answer->[0]=>eval;
| [reply] [d/l] |
I answered you in the Categories Q&A section.
(there is no section for "modules" so I put it under subs, since the my/local was there.)
—John | [reply] |