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


In reply to Re: Type globs, strict, and lexical variables by davido
in thread Type globs, strict, and lexical variables by cems2

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.