To further clear (muddy??) the waters, it seems that you're asking about scoping as it relates specifically to packages. A package is nothing more or less than a new namespace. A namespace is nothing more than a hash, called a symbol table, that contains, as its members, references to all the non-lexical variables within the namespace. In fact, the definition of being within in a namespace is to be contained within that specific hash. (For the curious, the hash for a namespace 'Foo::Bar' is %Foo::Bar::, so you can iterate over it, etc.)

A lexical variable is a variable that is accessible only within its inner-most enclosing scope. It's declared using my and cannot be accessed outside that scope. Period. Now, a variable declared with my at a file level is considered to be withing the implicit scope of the package. Still unaccessible outside that scope. (Closures are an indirect access to a lexical variable, so aren't really part of this discussion.)

Variables that are either not declared at all or declared with our or local are inserted into the symbol table of that namespace. If declared, they're considered to be block-scoped globals. The primary difference, simply put, between our and local is that our is 5.6+, but works under strict vars. local does not. (There's more to it than that, but look around the Monastery and you'll find more on it than you'll ever want to know.)

What does all this have to do with packages? Well, if you want to access a variable declared in package Foo from package Bar, you have to make sure you either are not using strict vars (Bad idea!!!) or have declared your variables with our. At this point, you can do a number of things.

  1. Use Exporter. Read up on the documentation how this would work.
  2. Use the fully-qualified name. $Foo::MyVar or something like that.
  3. Make Foo a class and create a method to access it.
I hope this helps.

------
We are the carpenters and bricklayers of the Information Age.

Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.


In reply to Re: About packages and scopes by dragonchild
in thread About packages and scopes by nlafferty

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.