home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


D.9 Packages

When multiple people work on a project, or if you're slighly 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 documentation will help here.