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

Last change on this file since 3368 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>,