1.3 Namespaces
A
namespace
is implemented as a table containing the
Large programs often
The
package
declaration described in the
1.3.1 Namespaces Compared with Scoping: my and use strictThe unintentional interaction between variables with the same name is enough of a problem that Perl provides more than one way to avoid it. You are probably already familiar with the use of my to restrict the scope of a variable to its enclosing block (between matching curly braces {}) and should be accustomed to using the directive use strict to require the use of my for all variables. use strict and my are a great way to protect your program from unintentional reuse of variable names. Make a habit of using my and working under use strict . |
1.4 Packages
Packages are a different way to protect a program's
Packages are very easy to use. A one-line package declaration puts a new namespace in effect. Here's a simple example: $dna = 'AAAAAAAAAA'; package Mouse; $dna = 'CCCCCCCCCC'; package Celegans; $dna = 'GGGGGGGGGG';
In this snippet, there are three variables, each with the same
The first line of the code is an assignment of a poly-A DNA fragment to a variable $dna . Because no package is explicitly named, this $dna variable appears in the default namespace main . The second line of code introduces a new namespace for variable and subroutine definitions by declaring package Mouse; . At this point, the main namespace is no longer active, and the Mouse namespace is brought into play. Note that the name of the namespace is capitalized; it's a well-established convention you should follow. The only noncapitalized namespace you should use is the default main . Now that the Mouse namespace is in effect, the third line of code, which declares a variable, $dna , is actually declaring a separate variable unrelated to the first. It contains a poly-C fragment of DNA. Finally, the last two lines of code declare a new package called Celegans and a new variable, also called $dna , that stores a poly-G DNA fragment.
To use these three
$dna
variables, you need to explicitly state which packages you want the variables from, as the following code fragment
print "The DNA from the main package:\n\n"; print $main::dna, "\n\n"; print "The DNA from the Mouse package:\n\n"; print $Mouse::dna, "\n\n"; print "The DNA from the Celegans package:\n\n"; print $Celegans::dna, "\n\n"; This gives the following output: The DNA from the main package: AAAAAAAAAA The DNA from the Mouse package: CCCCCCCCCC The DNA from the Celegans package: GGGGGGGGGG
As you can see, the variable name can be specified as to a particular package by
# # Define the variables in the packages # $dna = 'AAAAAAAAAA'; package Mouse; $dna = 'CCCCCCCCCC'; # # Print the values of the variables # print "The DNA from the current package:\n\n"; print $dna, "\n\n"; print "The DNA from the Mouse package:\n\n"; print $Mouse::dna, "\n\n"; This produces the following output: The DNA from the current package: CCCCCCCCCC The DNA from the Mouse package: CCCCCCCCCC Both print $dna and print $Mouse::dna reference the same variable. This is because the last package declaration was package Mouse; , so the print $dna statement prints the value of the variable $dna as defined in the current package, which is Mouse .
The rule is, once a package has been declared, it becomes the current package until the
By far the most common use of package is to call it once near the top of a file and have it stay in effect for all the code in the file. This is how modules are defined, as the next section shows. |