home *** CD-ROM | disk | FTP | other *** search
/ BURKS 2 / BURKS_AUG97.ISO / SLAKWARE / D13 / PERL2.TGZ / perl2.tar / usr / lib / perl5 / pod / perlmod.pod < prev    next >
Text File  |  1996-06-28  |  34KB  |  1,070 lines

  1. =head1 NAME
  2.  
  3. perlmod - Perl modules (packages)
  4.  
  5. =head1 DESCRIPTION
  6.  
  7. =head2 Packages
  8.  
  9. Perl provides a mechanism for alternative namespaces to protect packages
  10. from stomping on each others variables.  In fact, apart from certain
  11. magical variables, there's really no such thing as a global variable in
  12. Perl.  The package statement declares the compilation unit as being in the
  13. given namespace.  The scope of the package declaration is from the
  14. declaration itself through the end of the enclosing block (the same scope
  15. as the local() operator).  All further unqualified dynamic identifiers
  16. will be in this namespace.  A package statement only affects dynamic
  17. variables--including those you've used local() on--but I<not> lexical
  18. variables created with my().  Typically it would be the first declaration
  19. in a file to be included by the C<require> or C<use> operator.  You can
  20. switch into a package in more than one place; it merely influences which
  21. symbol table is used by the compiler for the rest of that block.  You can
  22. refer to variables and filehandles in other packages by prefixing the
  23. identifier with the package name and a double colon:
  24. C<$Package::Variable>.  If the package name is null, the C<main> package
  25. as assumed.  That is, C<$::sail> is equivalent to C<$main::sail>.
  26.  
  27. (The old package delimiter was a single quote, but double colon
  28. is now the preferred delimiter, in part because it's more readable
  29. to humans, and in part because it's more readable to B<emacs> macros.
  30. It also makes C++ programmers feel like they know what's going on.)
  31.  
  32. Packages may be nested inside other packages: C<$OUTER::INNER::var>.  This
  33. implies nothing about the order of name lookups, however.  All symbols
  34. are either local to the current package, or must be fully qualified
  35. from the outer package name down.  For instance, there is nowhere
  36. within package C<OUTER> that C<$INNER::var> refers to C<$OUTER::INNER::var>.
  37. It would treat package C<INNER> as a totally separate global package.
  38.  
  39. Only identifiers starting with letters (or underscore) are stored in a
  40. package's symbol table.  All other symbols are kept in package C<main>,
  41. including all of the punctuation variables like $_.  In addition, the
  42. identifiers STDIN, STDOUT, STDERR, ARGV, ARGVOUT, ENV, INC and SIG are
  43. forced to be in package C<main>, even when used for other purposes than
  44. their built-in one.  Note also that, if you have a package called C<m>,
  45. C<s> or C<y>, then you can't use the qualified form of an identifier
  46. because it will be interpreted instead as a pattern match, a substitution,
  47. or a translation.
  48.  
  49. (Variables beginning with underscore used to be forced into package
  50. main, but we decided it was more useful for package writers to be able
  51. to use leading underscore to indicate private variables and method names.
  52. $_ is still global though.)
  53.  
  54. Eval()ed strings are compiled in the package in which the eval() was
  55. compiled.  (Assignments to C<$SIG{}>, however, assume the signal
  56. handler specified is in the C<main> package.  Qualify the signal handler
  57. name if you wish to have a signal handler in a package.)  For an
  58. example, examine F<perldb.pl> in the Perl library.  It initially switches
  59. to the C<DB> package so that the debugger doesn't interfere with variables
  60. in the script you are trying to debug.  At various points, however, it
  61. temporarily switches back to the C<main> package to evaluate various
  62. expressions in the context of the C<main> package (or wherever you came
  63. from).  See L<perldebug>.
  64.  
  65. See L<perlsub> for other scoping issues related to my() and local(), 
  66. or L<perlref> regarding closures.
  67.  
  68. =head2 Symbol Tables
  69.  
  70. The symbol table for a package happens to be stored in the associative
  71. array of that name appended with two colons.  The main symbol table's
  72. name is thus C<%main::>, or C<%::> for short.  Likewise the nested package
  73. mentioned earlier is named C<%OUTER::INNER::>.
  74.  
  75. The value in each entry of the associative array is what you are referring
  76. to when you use the C<*name> typeglob notation.  In fact, the following
  77. have the same effect, though the first is more efficient because it does
  78. the symbol table lookups at compile time:
  79.  
  80.     local(*main::foo) = *main::bar; local($main::{'foo'}) =
  81.     $main::{'bar'};
  82.  
  83. You can use this to print out all the variables in a package, for
  84. instance.  Here is F<dumpvar.pl> from the Perl library:
  85.  
  86.    package dumpvar;
  87.    sub main::dumpvar {
  88.        ($package) = @_;
  89.        local(*stab) = eval("*${package}::");
  90.        while (($key,$val) = each(%stab)) {
  91.        local(*entry) = $val;
  92.        if (defined $entry) {
  93.            print "\$$key = '$entry'\n";
  94.        }
  95.  
  96.        if (defined @entry) {
  97.            print "\@$key = (\n";
  98.            foreach $num ($[ .. $#entry) {
  99.            print "  $num\t'",$entry[$num],"'\n";
  100.            }
  101.            print ")\n";
  102.        }
  103.  
  104.        if ($key ne "${package}::" && defined %entry) {
  105.            print "\%$key = (\n";
  106.            foreach $key (sort keys(%entry)) {
  107.            print "  $key\t'",$entry{$key},"'\n";
  108.            }
  109.            print ")\n";
  110.        }
  111.        }
  112.    }
  113.  
  114. Note that even though the subroutine is compiled in package C<dumpvar>,
  115. the name of the subroutine is qualified so that its name is inserted
  116. into package C<main>.
  117.  
  118. Assignment to a typeglob performs an aliasing operation, i.e.,
  119.  
  120.     *dick = *richard;
  121.  
  122. causes variables, subroutines and file handles accessible via the
  123. identifier C<richard> to also be accessible via the symbol C<dick>.  If
  124. you only want to alias a particular variable or subroutine, you can
  125. assign a reference instead:
  126.  
  127.     *dick = \$richard;
  128.  
  129. makes $richard and $dick the same variable, but leaves
  130. @richard and @dick as separate arrays.  Tricky, eh?
  131.  
  132. This mechanism may be used to pass and return cheap references
  133. into or from subroutines if you won't want to copy the whole
  134. thing.
  135.  
  136.     %some_hash = ();
  137.     *some_hash = fn( \%another_hash );
  138.     sub fn {
  139.     local *hashsym = shift;
  140.     # now use %hashsym normally, and you
  141.     # will affect the caller's %another_hash
  142.     my %nhash = (); # do what you want
  143.     return \%nhash; 
  144.     }
  145.  
  146. On return, the reference wil overwrite the hash slot in the
  147. symbol table specified by the *some_hash typeglob.  This
  148. is a somewhat tricky way of passing around refernces cheaply
  149. when you won't want to have to remember to dereference variables
  150. explicitly.
  151.  
  152. Another use of symbol tables is for making "constant"  scalars.
  153.  
  154.     *PI = \3.14159265358979;
  155.  
  156. Now you cannot alter $PI, which is probably a good thing all in all.
  157.  
  158. =head2 Package Constructors and Destructors
  159.  
  160. There are two special subroutine definitions that function as package
  161. constructors and destructors.  These are the C<BEGIN> and C<END>
  162. routines.  The C<sub> is optional for these routines.
  163.  
  164. A C<BEGIN> subroutine is executed as soon as possible, that is, the
  165. moment it is completely defined, even before the rest of the containing
  166. file is parsed.  You may have multiple C<BEGIN> blocks within a
  167. file--they will execute in order of definition.  Because a C<BEGIN>
  168. block executes immediately, it can pull in definitions of subroutines
  169. and such from other files in time to be visible to the rest of the
  170. file.
  171.  
  172. An C<END> subroutine is executed as late as possible, that is, when the
  173. interpreter is being exited, even if it is exiting as a result of a
  174. die() function.  (But not if it's is being blown out of the water by a
  175. signal--you have to trap that yourself (if you can).)  You may have
  176. multiple C<END> blocks within a file--they will execute in reverse
  177. order of definition; that is: last in, first out (LIFO).
  178.  
  179. Note that when you use the B<-n> and B<-p> switches to Perl, C<BEGIN>
  180. and C<END> work just as they do in B<awk>, as a degenerate case.
  181.  
  182. =head2 Perl Classes
  183.  
  184. There is no special class syntax in Perl, but a package may function
  185. as a class if it provides subroutines that function as methods.  Such a
  186. package may also derive some of its methods from another class package
  187. by listing the other package name in its @ISA array.  
  188.  
  189. For more on this, see L<perlobj>.
  190.  
  191. =head2 Perl Modules
  192.  
  193. A module is just a package that is defined in a library file of
  194. the same name, and is designed to be reusable.  It may do this by
  195. providing a mechanism for exporting some of its symbols into the symbol
  196. table of any package using it.  Or it may function as a class
  197. defin