in reply to Type globs, strict, and lexical variables

Typeglobs live in the package namespace. A typeglob is the 'name' and the various package variable containers (scalar variable, array variable, hash variable, filehandle, and so on) that will be associated with that name. Symbolic references can dial in to typeglobs and package variables, because symbolic refs are simply a means of accessing the package global namespace.

Lexicals don't have a 'namespace' or a package. They're, as their name implies, lexically scoped. They live in a different sort of pad, and are not accessible via typeglobs nor via symbolic refs.

You're correct that a lexical variable can mask a package variable. ...at least, that is if you only refer to the 'unqualified' package variable name. Consider this: In package main, a package global named $foo is really named $main::foo. That means that *foo is *main::foo. And that our $foo is also $main::foo. So if you have a package variable named $foo, in package main (thus also known by its fully qualified name as $main::foo), even if you declare my $foo;, thus masking the package variable $foo, you can still access the package variable by its fully qualified name: $main::foo.

But lexical (my) variables don't have a package, and don't reside in the package symbol table (a glorified hash, by the way). They have their own terms of existence, and thus are not tied to typeglobs.

If you want a more comprehensive understanding, better written and probably more accurate than my post here, do have a look at each of the following POD: perlref, perldata, and definitely perlsub. It's not a simple topic, but eventually vital toward understanding Perl.


Dave