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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |