This is, in many ways, a followup to chipmunk's excellent post, but
with a slight twist in that I do not consider our() or 'use vars' as
ways of declaring global variables (there is a subtle but important
distinction to be made). OzzyOsbourne asks:
1.Is there any difference between declaring variables at the
beginning of scripts with my or our? Won't their scope be the same?
Yes, there is a difference, and the difference is that No, the scopes
of the *variables* will not be the same --- one will be a lexical
variable (my) and the other will be a package-global variable (our).
I think one major problem people have with my(), our(), and 'use
vars' is that we all tend to discuss them as ways of declaring
'variables', and then confusion sets in because the scope of what we
declared doesn't always coincide with the scope of the variable which
doesn't seem to make sense. Do not think of our() and 'use vars' as
ways of declaring *variables* and things become clearer.
Lexical variables *are* declared and created with the my()
declaration. Package variables are never really "declared" at all ...
what is really being declared with either 'use vars' or our() is not
the variable per se, but "unqualified access" to a package variable
under the 'strict' pragma.
|