1.4 Packages


Packages are a different way to protect a program's variables from interacting unintentionally. In Perl, you can easily assign separate namespaces to entire sections of your code, which helps prevent namespace collisions and lets you create modules.

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 name , $dna . However, they are in three different packages, so they appear in three different symbol tables and are managed separately by the running Perl program.

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 demonstrates :

 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 putting the package name and two colons before the variable name (but after the $ , @ , or % that specifies the type of variable). If you don't specify a package in this way, Perl assumes you want the current package, which may not necessarily be the main package, as the following example shows:

 # # 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 next package declaration or until the end of the file. (You can also declare packages within blocks, evals, or subroutine definitions, in which case the package stays in effect until the end of the block, eval, or subroutine definition.)

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.



Mastering Perl for Bioinformatics
Mastering Perl for Bioinformatics
ISBN: 0596003072
EAN: 2147483647
Year: 2003
Pages: 156

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net