Learning Perl

Learning PerlSearch this book
Previous: D.8 Additional Regular-Expression FeaturesAppendix D
Topics We Didn't Mention
Next: D.10 Embeddible, Extensible
 

D.9 Packages

When multiple people work on a project, or if you're slightly schizophrenic, you can carve up the variable namespace using packages. A package is just a hidden prefix put in front of most variables (except variables created with the my operator). By changing the prefix, you get different variables. Here's a brief example:

$a = 123;            # this is really $main::a
$main::a++;          # same variable, now 124
package fred;        # now the prefix is "fred"
$a = 456;            # this is $fred::a
print $a - $main::a; # prints 456-124
package main;        # back to original default
print $a + $fred::a; # prints 124+456

So, any name with an explicit package name is used as-is, but all other names get packaged into the current default package. Packages are local to the current file or block, and you always start out in package main at the top of a file. For details, the perlsub (1) manpage will help here.


Previous: D.8 Additional Regular-Expression FeaturesLearning PerlNext: D.10 Embeddible, Extensible
D.8 Additional Regular-Expression FeaturesBook IndexD.10 Embeddible, Extensible



Banner.Novgorod.Ru