home *** CD-ROM | disk | FTP | other *** search
/ RISC DISC 2 / RISC_DISC_2.iso / pd_share / utilities / cli / perl / !Perl / Manual / perlmod < prev    next >
Encoding:
Text File  |  1995-04-18  |  18.6 KB  |  481 lines

  1. <!-- $RCSfile$$Revision$$Date$ -->
  2. <!-- $Log$ -->
  3. <HTML>
  4. <TITLE> PERLMOD </TITLE>
  5. <h2>NAME</h2>
  6. perlmod - Perl modules (packages)
  7. <p><h2>DESCRIPTION</h2>
  8. <h3>Packages</h3>
  9. Perl provides a mechanism for alternate namespaces to protect packages
  10. from stomping on each others variables.  By default, a Perl script starts
  11. compiling into the package known as <B>main</B>.  You can switch namespaces
  12. using the <B>package</B> declaration.  The scope of the package declaration is
  13. from the declaration itself to the end of the enclosing block (the same
  14. scope as the local() operator).  Typically it would be the first
  15. declaration in a file to be included by the 
  16. <A HREF="perlfunc.html#perlfunc_205">require</A>
  17.  operator.  You can
  18. switch into a package in more than one place; it merely influences which
  19. symbol table is used by the compiler for the rest of that block.  You can
  20. refer to variables and filehandles in other packages by prefixing the
  21. identifier with the package name and a double colon:
  22. <B>$Package::Variable</B>.  If the package name is null, the <B>main</B> package
  23. as assumed.  That is, <B>$::sail</B> is equivalent to <B>$main::sail</B>.
  24. <p>(The old package delimiter was a single quote, but double colon
  25. is now the preferred delimiter, in part because it's more readable
  26. to humans, and in part because it's more readable to <B>emacs</B> macros.
  27. It also makes C++ programmers feel like they know what's going on.)
  28. <p>Packages may be nested inside other packages: <B>$OUTER::INNER::var</B>.  This
  29. implies nothing about the order of name lookups, however.  All symbols
  30. are either local to the current package, or must be fully qualified
  31. from the outer package name down.  For instance, there is nowhere
  32. within package <B>OUTER</B> that <B>$INNER::var</B> refers to <B>$OUTER::INNER::var</B>.
  33. It would treat package <B>INNER</B> as a totally separate global package.
  34. <p>Only identifiers starting with letters (or underscore) are stored in a
  35. package's symbol table.  All other symbols are kept in package <B>main</B>.
  36. In addition, the identifiers STDIN, STDOUT, STDERR, <B>ARGV</B>,
  37. ARGVOUT, ENV, INC and SIG are forced to be in package <B>main</B>,
  38. even when used for other purposes than their built-in one.  Note also
  39. that, if you have a package called <B>m</B>, <B>s</B> or <B>y</B>, then you can't use
  40. the qualified form of an identifier since it will be interpreted instead
  41. as a pattern match, a substitution, or a translation.
  42. <p>(Variables beginning with underscore used to be forced into package
  43. main, but we decided it was more useful for package writers to be able
  44. to use leading underscore to indicate private variables and method names.)
  45. <p>Eval()ed strings are compiled in the package in which the eval() was
  46. compiled.  (Assignments to <B>$SIG{}</B>, however, assume the signal
  47. handler specified is in the C<main. package.  Qualify the signal handler
  48. name if you wish to have a signal handler in a package.)  For an
  49. example, examine <I>perldb.pl</I> in the Perl library.  It initially switches
  50. to the <B>DB</B> package so that the debugger doesn't interfere with variables
  51. in the script you are trying to debug.  At various points, however, it
  52. temporarily switches back to the <B>main</B> package to evaluate various
  53. expressions in the context of the <B>main</B> package (or wherever you came
  54. from).  See 
  55. <A HREF="perldebug.html">
  56. the perldebug manpage</A>
  57. .
  58. <p><h3>Symbol Tables</h3>
  59. The symbol table for a package happens to be stored in the associative
  60. array of that name appended with two colons.  The main symbol table's
  61. name is thus <B>%main::</B>, or <B>%::</B> for short.  Likewise the nested package
  62. mentioned earlier is named <B>%OUTER::INNER::</B>.
  63. <p>The value in each entry of the associative array is what you are
  64. referring to when you use the <B>*name</B> notation.  In fact, the following
  65. have the same effect, though the first is more efficient because it
  66. does the symbol table lookups at compile time:
  67. <p><pre>
  68.         local(*main::foo) = *main::bar; local($main::{'foo'}) =
  69.         $main::{'bar'};
  70. </pre>
  71. You can use this to print out all the variables in a package, for
  72. instance.  Here is <I>dumpvar.pl</I> from the Perl library:
  73. <p><pre>
  74.        package dumpvar;
  75.        sub main::dumpvar {
  76.            ($package) = @_;
  77.            local(*stab) = eval("*${package}::");
  78.            while (($key,$val) = each(%stab)) {
  79.            local(*entry) = $val;
  80.            if (defined $entry) {
  81.                print "\$$key = '$entry'\n";
  82.            }
  83. </pre>
  84. <pre>
  85.            if (defined @entry) {
  86.                print "\@$key = (\n";
  87.                foreach $num ($[ .. $#entry) {
  88.                    print "  $num\t'",$entry[$num],"'\n";
  89.                }
  90.                print ")\n";
  91.            }
  92. </pre>
  93. <pre>
  94.            if ($key ne "${package}::" && defined %entry) {
  95.                print "\%$key = (\n";
  96.                foreach $key (sort keys(%entry)) {
  97.                    print "  $key\t'",$entry{$key},"'\n";
  98.                }
  99.                print ")\n";
  100.            }
  101.            }
  102.        }
  103. </pre>
  104. Note that even though the subroutine is compiled in package <B>dumpvar</B>,
  105. the name of the subroutine is qualified so that its name is inserted
  106. into package <B>main</B>.
  107. <p>Assignment to a symbol table entry performs an aliasing operation,
  108. i.e.,
  109. <p><pre>
  110.         *dick = *richard;
  111. </pre>
  112. causes variables, subroutines and filehandles accessible via the
  113. identifier <B>richard</B> to also be accessible via the symbol <B>dick</B>.  If
  114. you only want to alias a particular variable or subroutine, you can
  115. assign a reference instead:
  116. <p><pre>
  117.         *dick = \$richard;
  118. </pre>
  119. makes $richard and $dick the same variable, but leaves
  120. @richard and @dick as separate arrays.  Tricky, eh?
  121. <p><h3>Package Constructors and Destructors</h3>
  122. There are two special subroutine definitions that function as package
  123. constructors and destructors.  These are the <B>BEGIN</B> and <B>END</B>
  124. routines.  The <B>sub</B> is optional for these routines.
  125. <p>A <B>BEGIN</B> subroutine is executed as soon as possible, that is, the
  126. moment it is completely defined, even before the rest of the containing
  127. file is parsed.  You may have multiple <B>BEGIN</B> blocks within a
  128. file--they will execute in order of definition.  Because a <B>BEGIN</B>
  129. block executes immediately, it can pull in definitions of subroutines
  130. and such from other files in time to be visible to the rest of the
  131. file.
  132. <p>An <B>END</B> subroutine is executed as late as possible, that is, when the
  133. interpreter is being exited, even if it is exiting as a result of a
  134. die() function.  (But not if it's is being blown out of the water by a
  135. signal--you have to trap that yourself (if you can).)  You may have
  136. multiple <B>END</B> blocks within a file--they wil execute in reverse
  137. order of definition; that is: last in, first out (LIFO).
  138. <p>Note that when you use the 
  139. <A HREF="perlrun.html#perlrun_353">-n</A>
  140.  and 
  141. <A HREF="perlrun.html#perlrun_354">-p</A>
  142.  switches to Perl, <B>BEGIN</B>
  143. and <B>END</B> work just as they do in <B>awk</B>, as a degenerate case.
  144. <p><h3>Perl Classes</h3>
  145. There is no special class syntax in Perl 5, but a package may function
  146. as a class if it provides subroutines that function as methods.  Such a
  147. package may also derive some of its methods from another class package
  148. by listing the other package name in its @ISA array.  For more on
  149. this, see 
  150. <A HREF="perlobj.html">
  151. the perlobj manpage</A>
  152. .
  153. <p><h3>Perl Modules</h3>
  154. In Perl 5, the notion of packages has been extended into the notion of
  155. modules.  A module is a package that is defined in a library file of
  156. the same name, and is designed to be reusable.  It may do this by
  157. providing a mechanism for exporting some of its symbols into the symbol
  158. table of any package using it.  Or it may function as a class
  159. definition and make its semantics available implicitly through method
  160. calls on the class and its objects, without explicit exportation of any
  161. symbols.  Or it can do a little of both.
  162. <p>Perl modules are included by saying
  163. <p><pre>
  164.         use Module;
  165. </pre>
  166. or
  167. <p><pre>
  168.         use Module LIST;
  169. </pre>
  170. This is exactly equivalent to
  171. <p><pre>
  172.         BEGIN { require "Module.pm"; import Module; }
  173. </pre>
  174. or
  175. <p><pre>
  176.         BEGIN { require "Module.pm"; import Module LIST; }
  177. </pre>
  178. All Perl module files have the extension <I>.pm</I>.  
  179. <A HREF="perlfunc.html#perlfunc_263">use</A>
  180.  assumes this so
  181. that you don't have to spell out "<I>Module.pm</I>" in quotes.  This also
  182. helps to differentiate new modules from old <I>.pl</I> and <I>.ph</I> files.
  183. Module names are also capitalized unless they're functioning as pragmas,
  184. "Pragmas" are in effect compiler directives, and are sometimes called
  185. "pragmatic modules" (or even "pragmata" if you're a classicist).
  186. <p>Because the 
  187. <A HREF="perlfunc.html#perlfunc_263">use</A>
  188.  statement implies a <B>BEGIN</B> block, the importation
  189. of semantics happens at the moment the 
  190. <A HREF="perlfunc.html#perlfunc_263">use</A>
  191.  statement is compiled,
  192. before the rest of the file is compiled.  This is how it is able
  193. to function as a pragma mechanism, and also how modules are able to
  194. declare subroutines that are then visible as list operators for
  195. the rest of the current file.  This will not work if you use 
  196. <A HREF="perlfunc.html#perlfunc_205">require</A>
  197.  
  198. instead of 
  199. <A HREF="perlfunc.html#perlfunc_263">use</A>
  200. .  Therefore, if you're planning on the module altering
  201. your namespace, use 
  202. <A HREF="perlfunc.html#perlfunc_263">use</A>
  203. ; otherwise, use 
  204. <A HREF="perlfunc.html#perlfunc_205">require</A>
  205. .  Otherwise you 
  206. can get into this problem:
  207. <p><pre>
  208.         require Cwd;            # make Cwd:: accessible
  209.         $here = Cwd::getcwd();  
  210. </pre>
  211. <pre>
  212.         use Cwd;                        # import names from Cwd:: 
  213.         $here = getcwd();
  214. </pre>
  215. <pre>
  216.         require Cwd;            # make Cwd:: accessible
  217.         $here = getcwd();               # oops! no main::getcwd()
  218. </pre>
  219. Perl packages may be nested inside other package names, so we can have
  220. package names containing <B>::</B>.  But if we used that package name
  221. directly as a filename it would makes for unwieldy or impossible
  222. filenames on some systems.  Therefore, if a module's name is, say,
  223. <B>Text::Soundex</B>, then its definition is actually found in the library
  224. file <I>Text/Soundex.pm</I>.
  225. <p>Perl modules always have a <I>.pm</I> file, but there may also be dynamically
  226. linked executables or autoloaded subroutine definitions associated with
  227. the module.  If so, these will be entirely transparent to the user of
  228. the module.  It is the responsibility of the <I>.pm</I> file to load (or
  229. arrange to autoload) any additional functionality.  The POSIX module
  230. happens to do both dynamic loading and autoloading, but the user can
  231. just say <B>use POSIX</B> to get it all.
  232. <p>For more information on writing extension modules, see 
  233. <A HREF="perlapi.html">
  234. the perlapi manpage</A>
  235.  
  236. and 
  237. <A HREF="perlguts.html">
  238. the perlguts manpage</A>
  239. .
  240. <p><h2>NOTE</h2>
  241. Perl does not enforce private and public parts of its modules as you may
  242. have been used to in other languages like C++, Ada, or Modula-17.  Perl
  243. doesn't have an infatuation with enforced privacy.  It would prefer
  244. that you stayed out of its living room because you weren't invited, not
  245. because it has a shotgun.
  246. <p>The module and its user have a contract, part of which is common law,
  247. and part of which is "written".  Part of the common law contract is
  248. that a module doesn't pollute any namespace it wasn't asked to.  The
  249. written contract for the module (AKA documentation) may make other
  250. provisions.  But then you know when you <B>use RedefineTheWorld</B> that
  251. you're redefining the world and willing to take the consequences.
  252. <p><h2>THE PERL MODULE LIBRARY</h2>
  253. A number of modules are included the the Perl distribution.  These are
  254. described below, and all end in <I>.pm</I>.  You may also discover files in 
  255. the library directory that end in either <I>.pl</I> or <I>.ph</I>.  These are old
  256. libaries supplied so that old programs that use them still run.  The
  257. <I>.pl</I> files will all eventually be converted into standard modules, and
  258. the <I>.ph</I> files made by <B>h2ph</B> will probably end up as extension modules
  259. made by <B>h2xs</B>.  (Some <I>.ph</I> values may already be available through the
  260. POSIX module.)  The <B>pl2pm</B> file in the distribution may help in your
  261. conversion, but it's just a mechanical process, so is far from bullet proof.
  262. <p><h3>Pragmatic Modules</h3>
  263. They work somewhat like pragmas in that they tend to affect the compilation of
  264. your program, and thus will usually only work well when used within a
  265.  
  266. <A HREF="perlfunc.html#perlfunc_263">use</A>
  267. , or 
  268. <A HREF="perlfunc.html#perlfunc_180">no</A>
  269. .  These are locally scoped, so if an inner BLOCK
  270. may countermand any of these by saying
  271. <p><pre>
  272.         no integer;
  273.         no strict 'refs';
  274. </pre>
  275. which lasts until the end of that BLOCK.
  276. <p>The following programs are defined (and have their own documentation).
  277. <p>
  278. <dl>
  279. <dt><B><B>integer</B></B>
  280. <dd>
  281. Perl pragma to compute arithmetic in integer instead of double
  282. <p></dd>
  283. <dt><B><B>less</B></B>
  284. <dd>
  285. Perl pragma to request less of something from the compiler
  286. <p></dd>
  287. <dt><B><B>sigtrap</B></B>
  288. <dd>
  289. Perl pragma to enable stack backtrace on unexpected signals
  290. <p></dd>
  291. <dt><B><B>strict</B></B>
  292. <dd>
  293. Perl pragma to restrict unsafe constructs
  294. <p></dd>
  295. <dt><B><B>subs</B></B>
  296. <dd>
  297. Perl pragma to predeclare sub names
  298. <p></dd>
  299.  
  300. </dl>
  301.  
  302. <h3>Standard Modules</h3>
  303. The following modules are all expacted to behave in a well-defined
  304. manner with respect to namespace pollution because they use the
  305. Exporter module.
  306. See their own documentation for details.
  307. <p>
  308. <dl>
  309. <dt><B><B>Abbrev</B></B>
  310. <dd>
  311. create an abbreviation table from a list
  312. <p></dd>
  313. <dt><B><B>AnyDBM_File</B></B>
  314. <dd>
  315. provide framework for multiple DBMs 
  316. <p></dd>
  317. <dt><B><B>AutoLoader</B></B>
  318. <dd>
  319. load functions only on demand
  320. <p></dd>
  321. <dt><B><B>AutoSplit</B></B>
  322. <dd>
  323. split a package for autoloading
  324. <p></dd>
  325. <dt><B><B>Basename</B></B>
  326. <dd>
  327. parse file anme and path from a specification
  328. <p></dd>
  329. <dt><B><B>Benchmark</B></B>
  330. <dd>
  331. benchmark running times of code 
  332. <p></dd>
  333. <dt><B><B>Carp</B></B>
  334. <dd>
  335. warn or die of errors (from perspective of caller)
  336. <p></dd>
  337. <dt><B><B>CheckTree</B></B>
  338. <dd>
  339. run many filetest checks on a tree
  340. <p></dd>
  341. <dt><B><B>Collate</B></B>
  342. <dd>
  343. compare 8-bit scalar data according to the current locale
  344. <p></dd>
  345. <dt><B><B>Config</B></B>
  346. <dd>
  347. access Perl configuration option
  348. <p></dd>
  349. <dt><B><B>Cwd</B></B>
  350. <dd>
  351. get pathname of current working directory
  352. <p></dd>
  353. <dt><B><B>DynaLoader</B></B>
  354. <dd>
  355. Dynamically load C libraries into Perl code 
  356. <p></dd>
  357. <dt><B><B>English</B></B>
  358. <dd>
  359. use nice English (or <B>awk</B>) names for ugly punctuation variables
  360. <p></dd>
  361. <dt><B><B>Env</B></B>
  362. <dd>
  363. Perl module that imports environment variables
  364. <p></dd>
  365. <dt><B><B>Exporter</B></B>
  366. <dd>
  367. module to control namespace manipulations 
  368. <p></dd>
  369. <dt><B><B>Fcntl</B></B>
  370. <dd>
  371. load the C Fcntl.h defines
  372. <p></dd>
  373. <dt><B><B>FileHandle</B></B>
  374. <dd>
  375. supply object methods for filehandles 
  376. <p></dd>
  377. <dt><B><B>Find</B></B>
  378. <dd>
  379. traverse a file tree
  380. <p></dd>
  381. <dt><B><B>Finddepth</B></B>
  382. <dd>
  383. traverse a directory structure depth-first
  384. <p></dd>
  385. <dt><B><B>Getopt</B></B>
  386. <dd>
  387. basic and extended getopt(3) processing
  388. <p></dd>
  389. <dt><B><B>MakeMaker</B></B>
  390. <dd>
  391. generate a Makefile for Perl extension
  392. <p></dd>
  393. <dt><B><B>Open2</B></B>
  394. <dd>
  395. open a process for both reading and writing
  396. <p></dd>
  397. <dt><B><B>Open3</B></B>
  398. <dd>
  399. open a process for reading, writing, and error handling
  400. <p></dd>
  401. <dt><B><B>POSIX</B></B>
  402. <dd>
  403. Perl interface to IEEE 1003.1 namespace
  404. <p></dd>
  405. <dt><B><B>Ping</B></B>
  406. <dd>
  407. check a host for upness
  408. <p></dd>
  409. <dt><B><B>Socket</B></B>
  410. <dd>
  411. load the C socket.h defines
  412. <p></dd>
  413.  
  414. </dl>
  415.  
  416. <h3>Extension Modules</h3>
  417. Extension modules are written in C (or a mix of Perl and C) and get
  418. dynamically loaded into Perl if and when you need them.  Supported
  419. extension modules include the Socket, Fcntl, and POSIX modules.
  420. <p>The following are popular C extension modules, which while available at
  421. Perl 5.0 release time, do not come not bundled (at least, not completely)
  422. due to their size, volatility, or simply lack of time for adequate testing
  423. and configuration across the multitude of platforms on which Perl was
  424. beta-tested.  You are encouraged to look for them in archie(1L), the Perl
  425. FAQ or Meta-FAQ, the WWW page, and even their authors before randomly
  426. posting asking for their present condition and disposition.  There's no
  427. guarantee that the names or addresses below have not changed since printing,
  428. and in fact, they probably have!
  429. <p>
  430. <dl>
  431. <dt><B><B>Curses</B></B>
  432. <dd>
  433. Written by William Setzer <<I>William_Setzer@ncsu.edu</I>>, while not
  434. included with the standard distribution, this extension module ports to
  435. most systems.  FTP from your nearest Perl archive site, or try
  436. <p></dd>
  437. <pre>
  438.             ftp://ftp.ncsu.edu/pub/math/wsetzer/cursperl5??.tar.gz
  439. </pre>
  440. It is currently in alpha test, so the name and ftp location may
  441. change.
  442. <p><dt><B><B>DBI</B></B>
  443. <dd>
  444. This is the portable database interface written by
  445. <<I>Tim.Bunce@ig.co.uk</I>>.  This supersedes the many perl4 ports for
  446. database extensions.  The official archive for DBperl extensions is
  447. <I>ftp.demon.co.uk:/pub/perl/db</I>.  This archive contains copies of perl4
  448. ports for Ingres, Oracle, Sybase, Informix, Unify, Postgres, and
  449. Interbase, as well as rdb and shql and other non-SQL systems.
  450. <p></dd>
  451. <dt><B><B>DB_File</B></B>
  452. <dd>
  453. Fastest and most restriction-free of the DBM bindings, this extension module 
  454. uses the popular Berkeley DB to tie() into your hashes.  This has a
  455. standardly-distributed man page and dynamic loading extension module, but
  456. you'll have to fetch the Berkeley code yourself.  See DB_File for
  457. where.
  458. <p></dd>
  459. <dt><B><B>Sx</B></B>
  460. <dd>
  461. This extension module is a front to the Athena and Xlib libraries for Perl
  462. GUI progamming, originally written by by Dominic Giampaolo
  463. <<I>dbg@sgi.com</I>>, then and rewritten for Sx by Frédéric
  464. Chauveau <<I>fmc@pasteur.fr</I>>.  It's available for FTP from
  465. <p></dd>
  466. <pre>
  467.         ftp.pasteur.fr:/pub/Perl/Sx.tar.gz
  468. </pre>
  469. <dt><B><B>Tk</B></B>
  470. <dd>
  471. This extension module is an object-oriented Perl5 binding to the popular
  472. tcl/tk X11 package.  However, you need know no TCL to use it!
  473. It was written by Malcolm Beattie <<I>mbeattie@sable.ox.ac.uk</I>>.
  474. If you are unable to locate it using archie(1L) or a similar
  475. tool, you may try retrieving it from <I>/private/Tk-october.tar.gz</I>
  476. from Malcolm's machine listed above.
  477. <p></dd>
  478.  
  479. </dl>
  480.  
  481.