source: trunk/essentials/dev-lang/perl/pod/perlmod.pod@ 3280

Last change on this file since 3280 was 3181, checked in by bird, 19 years ago

perl 5.8.8

File size: 23.8 KB
Line 
1=head1 NAME
2
3perlmod - Perl modules (packages and symbol tables)
4
5=head1 DESCRIPTION
6
7=head2 Packages
8X<package> X<namespace> X<variable, global> X<global variable> X<global>
9
10Perl provides a mechanism for alternative namespaces to protect
11packages from stomping on each other's variables. In fact, there's
12really no such thing as a global variable in Perl. The package
13statement declares the compilation unit as being in the given
14namespace. The scope of the package declaration is from the
15declaration itself through the end of the enclosing block, C<eval>,
16or file, whichever comes first (the same scope as the my() and
17local() operators). Unqualified dynamic identifiers will be in
18this namespace, except for those few identifiers that if unqualified,
19default to the main package instead of the current one as described
20below. A package statement affects only dynamic variables--including
21those you've used local() on--but I<not> lexical variables created
22with my(). Typically it would be the first declaration in a file
23included by the C<do>, C<require>, or C<use> operators. You can
24switch into a package in more than one place; it merely influences
25which symbol table is used by the compiler for the rest of that
26block. You can refer to variables and filehandles in other packages
27by prefixing the identifier with the package name and a double
28colon: C<$Package::Variable>. If the package name is null, the
29C<main> package is assumed. That is, C<$::sail> is equivalent to
30C<$main::sail>.
31
32The old package delimiter was a single quote, but double colon is now the
33preferred delimiter, in part because it's more readable to humans, and
34in part because it's more readable to B<emacs> macros. It also makes C++
35programmers feel like they know what's going on--as opposed to using the
36single quote as separator, which was there to make Ada programmers feel
37like they knew what was going on. Because the old-fashioned syntax is still
38supported for backwards compatibility, if you try to use a string like
39C<"This is $owner's house">, you'll be accessing C<$owner::s>; that is,
40the $s variable in package C<owner>, which is probably not what you meant.
41Use braces to disambiguate, as in C<"This is ${owner}'s house">.
42X<::> X<'>
43
44Packages may themselves contain package separators, as in
45C<$OUTER::INNER::var>. This implies nothing about the order of
46name lookups, however. There are no relative packages: all symbols
47are either local to the current package, or must be fully qualified
48from the outer package name down. For instance, there is nowhere
49within package C<OUTER> that C<$INNER::var> refers to
50C<$OUTER::INNER::var>. C<INNER> refers to a totally
51separate global package.
52
53Only identifiers starting with letters (or underscore) are stored
54in a package's symbol table. All other symbols are kept in package
55C<main>, including all punctuation variables, like $_. In addition,
56when unqualified, the identifiers STDIN, STDOUT, STDERR, ARGV,
57ARGVOUT, ENV, INC, and SIG are forced to be in package C<main>,
58even when used for other purposes than their built-in ones. If you
59have a package called C<m>, C<s>, or C<y>, then you can't use the
60qualified form of an identifier because it would be instead interpreted
61as a pattern match, a substitution, or a transliteration.
62X<variable, punctuation>
63
64Variables beginning with underscore used to be forced into package
65main, but we decided it was more useful for package writers to be able
66to use leading underscore to indicate private variables and method names.
67However, variables and functions named with a single C<_>, such as
68$_ and C<sub _>, are still forced into the package C<main>. See also
69L<perlvar/"Technical Note on the Syntax of Variable Names">.
70
71C<eval>ed strings are compiled in the package in which the eval() was
72compiled. (Assignments to C<$SIG{}>, however, assume the signal
73handler specified is in the C<main> package. Qualify the signal handler
74name if you wish to have a signal handler in a package.) For an
75example, examine F<perldb.pl> in the Perl library. It initially switches
76to the C<DB> package so that the debugger doesn't interfere with variables
77in the program you are trying to debug. At various points, however, it
78temporarily switches back to the C<main> package to evaluate various
79expressions in the context of the C<main> package (or wherever you came
80from). See L<perldebug>.
81
82The special symbol C<__PACKAGE__> contains the current package, but cannot
83(easily) be used to construct variable names.
84
85See L<perlsub> for other scoping issues related to my() and local(),
86and L<perlref> regarding closures.
87
88=head2 Symbol Tables
89X<symbol table> X<stash> X<%::> X<%main::> X<typeglob> X<glob> X<alias>
90
91The symbol table for a package happens to be stored in the hash of that
92name with two colons appended. The main symbol table's name is thus
93C<%main::>, or C<%::> for short. Likewise the symbol table for the nested
94package mentioned earlier is named C<%OUTER::INNER::>.
95
96The value in each entry of the hash is what you are referring to when you
97use the C<*name> typeglob notation. In fact, the following have the same
98effect, though the first is more efficient because it does the symbol
99table 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
105and C<local $main::foo = $main::bar>. The former is accessing the hash
106C<%main::>, which is the symbol table of package C<main>. The latter is
107simply assigning scalar C<$bar> in package C<main> to scalar C<$foo> of
108the same package.)
109
110You can use this to print out all the variables in a package, for
111instance. The standard but antiquated F<dumpvar.pl> library and
112the CPAN module Devel::Symdump make use of this.
113
114Assignment to a typeglob performs an aliasing operation, i.e.,
115
116 *dick = *richard;
117
118causes variables, subroutines, formats, and file and directory handles
119accessible via the identifier C<richard> also to be accessible via the
120identifier C<dick>. If you want to alias only a particular variable or
121subroutine, assign a reference instead:
122
123 *dick = \$richard;
124
125Which makes $richard and $dick the same variable, but leaves
126@richard and @dick as separate arrays. Tricky, eh?
127
128There is one subtle difference between the following statements:
129
130 *foo = *bar;
131 *foo = \$bar;
132
133C<*foo = *bar> makes the typeglobs themselves synonymous while
134C<*foo = \$bar> makes the SCALAR portions of two distinct typeglobs
135refer 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 {