| 1 | =head1 NAME
|
|---|
| 2 |
|
|---|
| 3 | perlmod - Perl modules (packages and symbol tables)
|
|---|
| 4 |
|
|---|
| 5 | =head1 DESCRIPTION
|
|---|
| 6 |
|
|---|
| 7 | =head2 Packages
|
|---|
| 8 | X<package> X<namespace> X<variable, global> X<global variable> X<global>
|
|---|
| 9 |
|
|---|
| 10 | Perl provides a mechanism for alternative namespaces to protect
|
|---|
| 11 | packages from stomping on each other's variables. In fact, there's
|
|---|
| 12 | really no such thing as a global variable in Perl. The package
|
|---|
| 13 | statement declares the compilation unit as being in the given
|
|---|
| 14 | namespace. The scope of the package declaration is from the
|
|---|
| 15 | declaration itself through the end of the enclosing block, C<eval>,
|
|---|
| 16 | or file, whichever comes first (the same scope as the my() and
|
|---|
| 17 | local() operators). Unqualified dynamic identifiers will be in
|
|---|
| 18 | this namespace, except for those few identifiers that if unqualified,
|
|---|
| 19 | default to the main package instead of the current one as described
|
|---|
| 20 | below. A package statement affects only dynamic variables--including
|
|---|
| 21 | those you've used local() on--but I<not> lexical variables created
|
|---|
| 22 | with my(). Typically it would be the first declaration in a file
|
|---|
| 23 | included by the C<do>, C<require>, or C<use> operators. You can
|
|---|
| 24 | switch into a package in more than one place; it merely influences
|
|---|
| 25 | which symbol table is used by the compiler for the rest of that
|
|---|
| 26 | block. You can refer to variables and filehandles in other packages
|
|---|
| 27 | by prefixing the identifier with the package name and a double
|
|---|
| 28 | colon: C<$Package::Variable>. If the package name is null, the
|
|---|
| 29 | C<main> package is assumed. That is, C<$::sail> is equivalent to
|
|---|
| 30 | C<$main::sail>.
|
|---|
| 31 |
|
|---|
| 32 | The old package delimiter was a single quote, but double colon is now the
|
|---|
| 33 | preferred delimiter, in part because it's more readable to humans, and
|
|---|
| 34 | in part because it's more readable to B<emacs> macros. It also makes C++
|
|---|
| 35 | programmers feel like they know what's going on--as opposed to using the
|
|---|
| 36 | single quote as separator, which was there to make Ada programmers feel
|
|---|
| 37 | like they knew what was going on. Because the old-fashioned syntax is still
|
|---|
| 38 | supported for backwards compatibility, if you try to use a string like
|
|---|
| 39 | C<"This is $owner's house">, you'll be accessing C<$owner::s>; that is,
|
|---|
| 40 | the $s variable in package C<owner>, which is probably not what you meant.
|
|---|
| 41 | Use braces to disambiguate, as in C<"This is ${owner}'s house">.
|
|---|
| 42 | X<::> X<'>
|
|---|
| 43 |
|
|---|
| 44 | Packages may themselves contain package separators, as in
|
|---|
| 45 | C<$OUTER::INNER::var>. This implies nothing about the order of
|
|---|
| 46 | name lookups, however. There are no relative packages: all symbols
|
|---|
| 47 | are either local to the current package, or must be fully qualified
|
|---|
| 48 | from the outer package name down. For instance, there is nowhere
|
|---|
| 49 | within package C<OUTER> that C<$INNER::var> refers to
|
|---|
| 50 | C<$OUTER::INNER::var>. C<INNER> refers to a totally
|
|---|
| 51 | separate global package.
|
|---|
| 52 |
|
|---|
| 53 | Only identifiers starting with letters (or underscore) are stored
|
|---|
| 54 | in a package's symbol table. All other symbols are kept in package
|
|---|
| 55 | C<main>, including all punctuation variables, like $_. In addition,
|
|---|
| 56 | when unqualified, the identifiers STDIN, STDOUT, STDERR, ARGV,
|
|---|
| 57 | ARGVOUT, ENV, INC, and SIG are forced to be in package C<main>,
|
|---|
|
|---|