Packages provide namespaces for non-lexical variables (those declared with our or with a fully qualified name). Within any given namespace there may be only one variable (of each datatype) with a given name. Using namespaces is kind of like if you have several friends with the same first name (their last name provides different "packages" for each). Likewise in order to disambiguate when the context is not clear which one you're referring to you name them by their full name which includes the namespace (first and last).

## Since we use strict we have to declare the names of our package ## variables with our use strict; package Smith; our $John = 1; package Jones; our $John = 2; # prints 2, because package Jones is the current package; print $John, "\n"; # prints 1 because we explicitly name the other package's var print $Smith::John, "\n"; package main; print $John, "\n"; # Compile time error because there's no $main::Joh +n

Now as for your question, your hash might presumably be called %names. What if you're writing code which maps constellations to the names of astronomers who discovered some feature therein and you want to call that hash %names as well (and yes, this is against PBP variable naming advice; indulge my hypothetical :).

You could put the constellation names hash in a package Constellations, while the other goes in Astronomers. Then your code would use $Constellations::names{ "Big Dipper" } or keys %Astronomers::names or what not to access them. You also could use Exporter to import the variables into other package's namespaces and refer to them with an unqualified %names instead to save yourself some typing.


In reply to Re: overview/tutorial on packages - creation and use by Fletch
in thread overview/tutorial on packages - creation and use by chexmix

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.