x> Aren't global variables the same thing as package variables?
x> And aren't those mutually exclusive with lexical variables?
x> The scope of the lexical variable may be broad, but that doesn't
x> make it global, at least not in Perl vernacular as I understand it.
i have been watching this thread with bemusement and seeing the group
miscommunicate about this topic. let me try to clear up the mess.
if you declare a lexical (my) variable in the body of a perl source file
outside of any sub or block, it is best called a file scoped lexical. it
is visible from the point (or just after
of the declaration to the
end of the file. you can even declare another my variable of the same
name and it will now be scoped to the end of the file (but it will
generate a warning so don't do it). these types of variable are also
called file globals as they are usually global to the entire file (even
though the declararation is lexical.
what most perl uses of global mean are what is more properly called
package globals. this means they are in the symbol table and truly
global to the entire program (as long as you know where to find it in
the symbol table, you can mung it at a distance).
so we have the lexically declared file scoped 'globals' and
program/package 'globals'. please note the difference now and when you
discuss these in the future. i won't even bring up 'our' which just adds
to the confusion by being a lexical declaration of a package global!
uri
I agree with everything you said. 'File scoped globals', ie lexical's
in the outer scope. I'm not sure package globals are available for
access throughout the program without explicetly including the namespace
hash key, unless its imported (aliased) withing a use statement.
At least that's what I get out of the docs. I'm probably wrong, dunno.
Concerning package globals..
From perlmod:
Perl provides a mechanism for alternative namespaces to protect packages from stomping on each other's variables. In fact, there's really no such thing as a global variable in Perl. The package
statement declares the compilation unit as being in the given namespace. The scope of the package declaration is from the declaration itself through the end of the enclosing block, eval, or file,
whichever comes first (the same scope as the my() and local() operators). Unqualified dynamic identifiers will be in this namespace, except for those few identifiers that if unqualified, default to
the main package instead of the current one as described below. A package statement affects only dynamic variables--including those you've used local() on--but not lexical variables created with my().
I'm relatively new to Perl compared to all you guys. It appears
to me that 'my()' is a late bloomer to Perl, you know? Lexical's.
I have no idea what Perl means by a lexical. I know scoping thouroughly.
To me, lexical's were dropped in as an add-on to un-confuse what appears
to be a massive namespace problem that existed with every variable being
dropped into the hash, and the look-up overhead nightmare performance hit,
collissions with other namespaces, etc..
I've heard all the 'you better use strict/warnings' mantra. So I always do,
and it helps alot. But you can't define a package global with strict on, so
I don't. I use all file-scoped lexical globals. I really don't have the need
for users to see my file scoped variables anyway.
So use strict is something I usually don't see in Modules (.pm files). Sure
enough, there are non-lexical scalar variables. So what is strict, just for
debug? Probably is a way to tell it to go lightly on package globals huh?
Package FunStuff;
$IwannaBeApackageGlobal = "don't use strict then";
use Strict; # down here then..
Its all clear as muddy waters.
Thanks!
sln