| 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>,
|
|---|
| 58 | even when used for other purposes than their built-in ones. If you
|
|---|
| 59 | have a package called C<m>, C<s>, or C<y>, then you can't use the
|
|---|
| 60 | qualified form of an identifier because it would be instead interpreted
|
|---|
| 61 | as a pattern match, a substitution, or a transliteration.
|
|---|
| 62 | X<variable, punctuation>
|
|---|
| 63 |
|
|---|
| 64 | Variables beginning with underscore used to be forced into package
|
|---|
| 65 | main, but we decided it was more useful for package writers to be able
|
|---|
| 66 | to use leading underscore to indicate private variables and method names.
|
|---|
| 67 | However, variables and functions named with a single C<_>, such as
|
|---|
| 68 | $_ and C<sub _>, are still forced into the package C<main>. See also
|
|---|
| 69 | L<perlvar/"Technical Note on the Syntax of Variable Names">.
|
|---|
| 70 |
|
|---|
| 71 | C<eval>ed strings are compiled in the package in which the eval() was
|
|---|
| 72 | compiled. (Assignments to C<$SIG{}>, however, assume the signal
|
|---|
| 73 | handler specified is in the C<main> package. Qualify the signal handler
|
|---|
| 74 | name if you wish to have a signal handler in a package.) For an
|
|---|
| 75 | example, examine F<perldb.pl> in the Perl library. It initially switches
|
|---|
| 76 | to the C<DB> package so that the debugger doesn't interfere with variables
|
|---|
| 77 | in the program you are trying to debug. At various points, however, it
|
|---|
| 78 | temporarily switches back to the C<main> package to evaluate various
|
|---|
| 79 | expressions in the context of the C<main> package (or wherever you came
|
|---|
| 80 | from). See L<perldebug>.
|
|---|
| 81 |
|
|---|
| 82 | The special symbol C<__PACKAGE__> contains the current package, but cannot
|
|---|
| 83 | (easily) be used to construct variable names.
|
|---|
| 84 |
|
|---|
| 85 | See L<perlsub> for other scoping issues related to my() and local(),
|
|---|
| 86 | and L<perlref> regarding closures.
|
|---|
| 87 |
|
|---|
| 88 | =head2 Symbol Tables
|
|---|
| 89 | X<symbol table> X<stash> X<%::> X<%main::> X<typeglob> X<glob> X<alias>
|
|---|
| 90 |
|
|---|
| 91 | The symbol table for a package happens to be stored in the hash of that
|
|---|
| 92 | name with two colons appended. The main symbol table's name is thus
|
|---|
| 93 | C<%main::>, or C<%::> for short. Likewise the symbol table for the nested
|
|---|
| 94 | package mentioned earlier is named C<%OUTER::INNER::>.
|
|---|
| 95 |
|
|---|
| 96 | The value in each entry of the hash is what you are referring to when you
|
|---|
| 97 | use the C<*name> typeglob notation. In fact, the following have the same
|
|---|
| 98 | effect, though the first is more efficient because it does the symbol
|
|---|
| 99 | table lookups at compile time:
|
|---|
| 100 |
|
|---|
| 101 | local *main::foo = *main::bar;
|
|---|
| 102 | local $main::{foo} = $main::{bar};
|
|---|
| 103 |
|
|---|
| 104 | (Be sure to note the B<vast> difference between the second line above
|
|---|
| 105 | and C<local $main::foo = $main::bar>. The former is accessing the hash
|
|---|
| 106 | C<%main::>, which is the symbol table of package C<main>. The latter is
|
|---|
| 107 | simply assigning scalar C<$bar> in package C<main> to scalar C<$foo> of
|
|---|
| 108 | the same package.)
|
|---|
| 109 |
|
|---|
| 110 | You can use this to print out all the variables in a package, for
|
|---|
| 111 | instance. The standard but antiquated F<dumpvar.pl> library and
|
|---|
| 112 | the CPAN module Devel::Symdump make use of this.
|
|---|
| 113 |
|
|---|
| 114 | Assignment to a typeglob performs an aliasing operation, i.e.,
|
|---|
| 115 |
|
|---|
| 116 | *dick = *richard;
|
|---|
| 117 |
|
|---|
| 118 | causes variables, subroutines, formats, and file and directory handles
|
|---|
| 119 | accessible via the identifier C<richard> also to be accessible via the
|
|---|
| 120 | identifier C<dick>. If you want to alias only a particular variable or
|
|---|
| 121 | subroutine, assign a reference instead:
|
|---|
| 122 |
|
|---|
| 123 | *dick = \$richard;
|
|---|
| 124 |
|
|---|
| 125 | Which makes $richard and $dick the same variable, but leaves
|
|---|
| 126 | @richard and @dick as separate arrays. Tricky, eh?
|
|---|
| 127 |
|
|---|
| 128 | There is one subtle difference between the following statements:
|
|---|
| 129 |
|
|---|
| 130 | *foo = *bar;
|
|---|
| 131 | *foo = \$bar;
|
|---|
| 132 |
|
|---|
| 133 | C<*foo = *bar> makes the typeglobs themselves synonymous while
|
|---|
| 134 | C<*foo = \$bar> makes the SCALAR portions of two distinct typeglobs
|
|---|
| 135 | refer to the same scalar value. This means that the following code:
|
|---|
| 136 |
|
|---|
| 137 | $bar = 1;
|
|---|
| 138 | *foo = \$bar; # Make $foo an alias for $bar
|
|---|
| 139 |
|
|---|
| 140 | {
|
|---|
|
|---|