Commit | Line | Data |
---|---|---|
a0d0e21e LW |
1 | =head1 NAME |
2 | ||
3 | perlmod - Perl modules (packages) | |
4 | ||
5 | =head1 DESCRIPTION | |
6 | ||
7 | =head2 Packages | |
8 | ||
748a9306 LW |
9 | Perl provides a mechanism for alternative namespaces to protect packages |
10 | from stomping on each others variables. In fact, apart from certain magical | |
11 | variables, there's really no such thing as a global variable in Perl. | |
12 | By default, a Perl script starts | |
a0d0e21e LW |
13 | compiling into the package known as C<main>. You can switch namespaces |
14 | using the C<package> declaration. The scope of the package declaration is | |
15 | from the declaration itself to the end of the enclosing block (the same | |
16 | scope as the local() operator). Typically it would be the first | |
17 | declaration in a file to be included by the C<require> 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 | C<$Package::Variable>. If the package name is null, the C<main> package | |
23 | as assumed. That is, C<$::sail> is equivalent to C<$main::sail>. | |
24 | ||
25 | (The old package delimiter was a single quote, but double colon | |
26 | is now the preferred delimiter, in part because it's more readable | |
27 | to humans, and in part because it's more readable to B<emacs> macros. | |
28 | It also makes C++ programmers feel like they know what's going on.) | |
29 | ||
30 | Packages may be nested inside other packages: C<$OUTER::INNER::var>. This | |
31 | implies nothing about the order of name lookups, however. All symbols | |
32 | are either local to the current package, or must be fully qualified | |
33 | from the outer package name down. For instance, there is nowhere | |
34 | within package C<OUTER> that C<$INNER::var> refers to C<$OUTER::INNER::var>. | |
35 | It would treat package C<INNER> as a totally separate global package. | |
36 | ||
37 | Only identifiers starting with letters (or underscore) are stored in a | |
38 | package's symbol table. All other symbols are kept in package C<main>. | |
748a9306 | 39 | In addition, the identifiers STDIN, STDOUT, STDERR, ARGV, |
a0d0e21e LW |
40 | ARGVOUT, ENV, INC and SIG are forced to be in package C<main>, |
41 | even when used for other purposes than their built-in one. Note also | |
42 | that, if you have a package called C<m>, C<s> or C<y>, then you can't use | |
748a9306 | 43 | the qualified form of an identifier because it will be interpreted instead |
a0d0e21e LW |
44 | as a pattern match, a substitution, or a translation. |
45 | ||
46 | (Variables beginning with underscore used to be forced into package | |
47 | main, but we decided it was more useful for package writers to be able | |
48 | to use leading underscore to indicate private variables and method names.) | |
49 | ||
50 | Eval()ed strings are compiled in the package in which the eval() was | |
51 | compiled. (Assignments to C<$SIG{}>, however, assume the signal | |
748a9306 | 52 | handler specified is in the C<main> package. Qualify the signal handler |
a0d0e21e LW |
53 | name if you wish to have a signal handler in a package.) For an |
54 | example, examine F<perldb.pl> in the Perl library. It initially switches | |
55 | to the C<DB> package so that the debugger doesn't interfere with variables | |
56 | in the script you are trying to debug. At various points, however, it | |
57 | temporarily switches back to the C<main> package to evaluate various | |
58 | expressions in the context of the C<main> package (or wherever you came | |
59 | from). See L<perldebug>. | |
60 | ||
61 | =head2 Symbol Tables | |
62 | ||
63 | The symbol table for a package happens to be stored in the associative | |
64 | array of that name appended with two colons. The main symbol table's | |
65 | name is thus C<%main::>, or C<%::> for short. Likewise the nested package | |
66 | mentioned earlier is named C<%OUTER::INNER::>. | |
67 | ||
68 | The value in each entry of the associative array is what you are | |
4633a7c4 | 69 | referring to when you use the C<*name> typeglob notation. In fact, the following |
a0d0e21e LW |
70 | have the same effect, though the first is more efficient because it |
71 | does the symbol table lookups at compile time: | |
72 | ||
73 | local(*main::foo) = *main::bar; local($main::{'foo'}) = | |
74 | $main::{'bar'}; | |
75 | ||
76 | You can use this to print out all the variables in a package, for | |
77 | instance. Here is F<dumpvar.pl> from the Perl library: | |
78 | ||
79 | package dumpvar; | |
80 | sub main::dumpvar { | |
81 | ($package) = @_; | |
82 | local(*stab) = eval("*${package}::"); | |
83 | while (($key,$val) = each(%stab)) { | |
84 | local(*entry) = $val; | |
85 | if (defined $entry) { | |
86 | print "\$$key = '$entry'\n"; | |
87 | } | |
88 | ||
89 | if (defined @entry) { | |
90 | print "\@$key = (\n"; | |
91 | foreach $num ($[ .. $#entry) { | |
92 | print " $num\t'",$entry[$num],"'\n"; | |
93 | } | |
94 | print ")\n"; | |
95 | } | |
96 | ||
97 | if ($key ne "${package}::" && defined %entry) { | |
98 | print "\%$key = (\n"; | |
99 | foreach $key (sort keys(%entry)) { | |
100 | print " $key\t'",$entry{$key},"'\n"; | |
101 | } | |
102 | print ")\n"; | |
103 | } | |
104 | } | |
105 | } | |
106 | ||
107 | Note that even though the subroutine is compiled in package C<dumpvar>, | |
108 | the name of the subroutine is qualified so that its name is inserted | |
109 | into package C<main>. | |
110 | ||
4633a7c4 | 111 | Assignment to a typeglob performs an aliasing operation, |
a0d0e21e LW |
112 | i.e., |
113 | ||
114 | *dick = *richard; | |
115 | ||
748a9306 | 116 | causes variables, subroutines and file handles accessible via the |
a0d0e21e LW |
117 | identifier C<richard> to also be accessible via the symbol C<dick>. If |
118 | you only want to alias a particular variable or subroutine, you can | |
119 | assign a reference instead: | |
120 | ||
121 | *dick = \$richard; | |
122 | ||
123 | makes $richard and $dick the same variable, but leaves | |
124 | @richard and @dick as separate arrays. Tricky, eh? | |
125 | ||
126 | =head2 Package Constructors and Destructors | |
127 | ||
128 | There are two special subroutine definitions that function as package | |
129 | constructors and destructors. These are the C<BEGIN> and C<END> | |
130 | routines. The C<sub> is optional for these routines. | |
131 | ||
132 | A C<BEGIN> subroutine is executed as soon as possible, that is, the | |
133 | moment it is completely defined, even before the rest of the containing | |
134 | file is parsed. You may have multiple C<BEGIN> blocks within a | |
135 | file--they will execute in order of definition. Because a C<BEGIN> | |
136 | block executes immediately, it can pull in definitions of subroutines | |
137 | and such from other files in time to be visible to the rest of the | |
138 | file. | |
139 | ||
140 | An C<END> subroutine is executed as late as possible, that is, when the | |
141 | interpreter is being exited, even if it is exiting as a result of a | |
142 | die() function. (But not if it's is being blown out of the water by a | |
143 | signal--you have to trap that yourself (if you can).) You may have | |
748a9306 | 144 | multiple C<END> blocks within a file--they will execute in reverse |
a0d0e21e LW |
145 | order of definition; that is: last in, first out (LIFO). |
146 | ||
147 | Note that when you use the B<-n> and B<-p> switches to Perl, C<BEGIN> | |
148 | and C<END> work just as they do in B<awk>, as a degenerate case. | |
149 | ||
150 | =head2 Perl Classes | |
151 | ||
4633a7c4 | 152 | There is no special class syntax in Perl, but a package may function |
a0d0e21e LW |
153 | as a class if it provides subroutines that function as methods. Such a |
154 | package may also derive some of its methods from another class package | |
4633a7c4 LW |
155 | by listing the other package name in its @ISA array. |
156 | ||
157 | For more on this, see L<perlobj>. | |
a0d0e21e LW |
158 | |
159 | =head2 Perl Modules | |
160 | ||
4633a7c4 | 161 | A module is a just package that is defined in a library file of |
a0d0e21e LW |
162 | the same name, and is designed to be reusable. It may do this by |
163 | providing a mechanism for exporting some of its symbols into the symbol | |
164 | table of any package using it. Or it may function as a class | |
165 | definition and make its semantics available implicitly through method | |
166 | calls on the class and its objects, without explicit exportation of any | |
167 | symbols. Or it can do a little of both. | |
168 | ||
4633a7c4 LW |
169 | For example, to start a normal module called Fred, create |
170 | a file called Fred.pm and put this at the start of it: | |
171 | ||
172 | package Fred; | |
173 | require Exporter; | |
174 | @ISA = qw(Exporter); | |
175 | @EXPORT = qw(func1 func2); | |
176 | @EXPORT_OK = qw($sally @listabob %harry func3); | |
177 | ||
178 | Then go on to declare and use your variables in functions | |
179 | without any qualifications. | |
180 | See L<Exporter> and the I<Perl Modules File> for details on | |
181 | mechanics and style issues in module creation. | |
182 | ||
183 | Perl modules are included into your program by saying | |
a0d0e21e LW |
184 | |
185 | use Module; | |
186 | ||
187 | or | |
188 | ||
189 | use Module LIST; | |
190 | ||
191 | This is exactly equivalent to | |
192 | ||
193 | BEGIN { require "Module.pm"; import Module; } | |
194 | ||
195 | or | |
196 | ||
197 | BEGIN { require "Module.pm"; import Module LIST; } | |
198 | ||
199 | All Perl module files have the extension F<.pm>. C<use> assumes this so | |
200 | that you don't have to spell out "F<Module.pm>" in quotes. This also | |
201 | helps to differentiate new modules from old F<.pl> and F<.ph> files. | |
202 | Module names are also capitalized unless they're functioning as pragmas, | |
203 | "Pragmas" are in effect compiler directives, and are sometimes called | |
204 | "pragmatic modules" (or even "pragmata" if you're a classicist). | |
205 | ||
206 | Because the C<use> statement implies a C<BEGIN> block, the importation | |
207 | of semantics happens at the moment the C<use> statement is compiled, | |
208 | before the rest of the file is compiled. This is how it is able | |
209 | to function as a pragma mechanism, and also how modules are able to | |
210 | declare subroutines that are then visible as list operators for | |
211 | the rest of the current file. This will not work if you use C<require> | |
212 | instead of C<use>. Therefore, if you're planning on the module altering | |
213 | your namespace, use C<use>; otherwise, use C<require>. Otherwise you | |
214 | can get into this problem: | |
215 | ||
216 | require Cwd; # make Cwd:: accessible | |
217 | $here = Cwd::getcwd(); | |
218 | ||
219 | use Cwd; # import names from Cwd:: | |
220 | $here = getcwd(); | |
221 | ||
222 | require Cwd; # make Cwd:: accessible | |
223 | $here = getcwd(); # oops! no main::getcwd() | |
224 | ||
225 | Perl packages may be nested inside other package names, so we can have | |
226 | package names containing C<::>. But if we used that package name | |
227 | directly as a filename it would makes for unwieldy or impossible | |
228 | filenames on some systems. Therefore, if a module's name is, say, | |
229 | C<Text::Soundex>, then its definition is actually found in the library | |
230 | file F<Text/Soundex.pm>. | |
231 | ||
232 | Perl modules always have a F<.pm> file, but there may also be dynamically | |
233 | linked executables or autoloaded subroutine definitions associated with | |
234 | the module. If so, these will be entirely transparent to the user of | |
235 | the module. It is the responsibility of the F<.pm> file to load (or | |
236 | arrange to autoload) any additional functionality. The POSIX module | |
237 | happens to do both dynamic loading and autoloading, but the user can | |
238 | just say C<use POSIX> to get it all. | |
239 | ||
8e07c86e | 240 | For more information on writing extension modules, see L<perlxs> |
a0d0e21e LW |
241 | and L<perlguts>. |
242 | ||
243 | =head1 NOTE | |
244 | ||
245 | Perl does not enforce private and public parts of its modules as you may | |
246 | have been used to in other languages like C++, Ada, or Modula-17. Perl | |
247 | doesn't have an infatuation with enforced privacy. It would prefer | |
248 | that you stayed out of its living room because you weren't invited, not | |
249 | because it has a shotgun. | |
250 | ||
251 | The module and its user have a contract, part of which is common law, | |
252 | and part of which is "written". Part of the common law contract is | |
253 | that a module doesn't pollute any namespace it wasn't asked to. The | |
254 | written contract for the module (AKA documentation) may make other | |
255 | provisions. But then you know when you C<use RedefineTheWorld> that | |
256 | you're redefining the world and willing to take the consequences. | |
257 | ||
258 | =head1 THE PERL MODULE LIBRARY | |
259 | ||
260 | A number of modules are included the the Perl distribution. These are | |
261 | described below, and all end in F<.pm>. You may also discover files in | |
262 | the library directory that end in either F<.pl> or F<.ph>. These are old | |
748a9306 | 263 | libraries supplied so that old programs that use them still run. The |
a0d0e21e LW |
264 | F<.pl> files will all eventually be converted into standard modules, and |
265 | the F<.ph> files made by B<h2ph> will probably end up as extension modules | |
266 | made by B<h2xs>. (Some F<.ph> values may already be available through the | |
267 | POSIX module.) The B<pl2pm> file in the distribution may help in your | |
268 | conversion, but it's just a mechanical process, so is far from bullet proof. | |
269 | ||
270 | =head2 Pragmatic Modules | |
271 | ||
272 | They work somewhat like pragmas in that they tend to affect the compilation of | |
273 | your program, and thus will usually only work well when used within a | |
748a9306 | 274 | C<use>, or C<no>. These are locally scoped, so an inner BLOCK |
a0d0e21e LW |
275 | may countermand any of these by saying |
276 | ||
277 | no integer; | |
278 | no strict 'refs'; | |
279 | ||
280 | which lasts until the end of that BLOCK. | |
281 | ||
282 | The following programs are defined (and have their own documentation). | |
283 | ||
284 | =over 12 | |
285 | ||
4633a7c4 LW |
286 | =item C<diagnostics> |
287 | ||
288 | Pragma to produce enhanced diagnostics | |
289 | ||
a0d0e21e LW |
290 | =item C<integer> |
291 | ||
4633a7c4 | 292 | Pragma to compute arithmetic in integer instead of double |
a0d0e21e LW |
293 | |
294 | =item C<less> | |
295 | ||
4633a7c4 | 296 | Pragma to request less of something from the compiler |
a0d0e21e LW |
297 | |
298 | =item C<sigtrap> | |
299 | ||
4633a7c4 | 300 | Pragma to enable stack backtrace on unexpected signals |
a0d0e21e LW |
301 | |
302 | =item C<strict> | |
303 | ||
4633a7c4 | 304 | Pragma to restrict unsafe constructs |
a0d0e21e LW |
305 | |
306 | =item C<subs> | |
307 | ||
4633a7c4 | 308 | Pragma to predeclare sub names |
a0d0e21e LW |
309 | |
310 | =back | |
311 | ||
312 | =head2 Standard Modules | |
313 | ||
4633a7c4 | 314 | Standard, bundled modules are all expected to behave in a well-defined |
a0d0e21e | 315 | manner with respect to namespace pollution because they use the |
4633a7c4 | 316 | Exporter module. See their own documentation for details. |
a0d0e21e | 317 | |
4633a7c4 | 318 | To find out all the modules installed on your system, do this: |
a0d0e21e | 319 | |
4633a7c4 | 320 | find `perl -e 'print "@INC"'` -name '*.pm' -print |
a0d0e21e | 321 | |
4633a7c4 LW |
322 | They should all have their own documentation installed and accessible via |
323 | your system man(1) command. If that fails, try the I<perldoc> program. | |
a0d0e21e | 324 | |
4633a7c4 | 325 | =head2 Extension Modules |
a0d0e21e | 326 | |
4633a7c4 LW |
327 | Extension modules are written in C (or a mix of Perl and C) and get |
328 | dynamically loaded into Perl if and when you need them. Supported | |
329 | extension modules include the Socket, Fcntl, and POSIX modules. | |
a0d0e21e | 330 | |
4633a7c4 LW |
331 | Many popular C extension modules |
332 | do not come bundled (at least, not completely) | |
333 | due to their size, volatility, or simply lack of time for adequate testing | |
334 | and configuration across the multitude of platforms on which Perl was | |
335 | beta-tested. You are encouraged to look for them in archie(1L), the Perl | |
336 | FAQ or Meta-FAQ, the WWW page, and even with their authors before randomly | |
337 | posting asking for their present condition and disposition. | |
a0d0e21e | 338 | |
4633a7c4 | 339 | =head2 CPAN |
a0d0e21e | 340 | |
4633a7c4 LW |
341 | CPAN stands for the Comprehensive Perl Archive Network. This is a globally |
342 | replicated collection of all known Perl materials, including hundreds | |
343 | of unbunded modules. Here are the major categories of modules: | |
a0d0e21e | 344 | |
4633a7c4 | 345 | =over |
a0d0e21e | 346 | |
4633a7c4 LW |
347 | =item * |
348 | Language Extensions and Documentation Tools | |
a0d0e21e | 349 | |
4633a7c4 LW |
350 | =item * |
351 | Development Support | |
a0d0e21e | 352 | |
4633a7c4 LW |
353 | =item * |
354 | Operating System Interfaces | |
a0d0e21e | 355 | |
4633a7c4 LW |
356 | =item * |
357 | Networking, Device Control (modems) and InterProcess Communication | |
a0d0e21e | 358 | |
4633a7c4 LW |
359 | =item * |
360 | Data Types and Data Type Utilities | |
a0d0e21e | 361 | |
4633a7c4 LW |
362 | =item * |
363 | Database Interfaces | |
a0d0e21e | 364 | |
4633a7c4 LW |
365 | =item * |
366 | User Interfaces | |
a0d0e21e | 367 | |
4633a7c4 LW |
368 | =item * |
369 | Interfaces to / Emulations of Other Programming Languages | |
a0d0e21e | 370 | |
4633a7c4 LW |
371 | =item * |
372 | File Names, File Systems and File Locking (see also File Handles) | |
a0d0e21e | 373 | |
4633a7c4 LW |
374 | =item * |
375 | String Processing, Language Text Processing, Parsing and Searching | |
a0d0e21e | 376 | |
4633a7c4 LW |
377 | =item * |
378 | Option, Argument, Parameter and Configuration File Processing | |
a0d0e21e | 379 | |
4633a7c4 LW |
380 | =item * |
381 | Internationalization and Locale | |
a0d0e21e | 382 | |
4633a7c4 LW |
383 | =item * |
384 | Authentication, Security and Encryption | |
a0d0e21e | 385 | |
4633a7c4 LW |
386 | =item * |
387 | World Wide Web, HTML, HTTP, CGI, MIME | |
a0d0e21e | 388 | |
4633a7c4 LW |
389 | =item * |
390 | Server and Daemon Utilities | |
a0d0e21e | 391 | |
4633a7c4 LW |
392 | =item * |
393 | Archiving and Compression | |
a0d0e21e | 394 | |
4633a7c4 LW |
395 | =item * |
396 | Images, Pixmap and Bitmap Manipulation, Drawing and Graphing | |
a0d0e21e | 397 | |
4633a7c4 LW |
398 | =item * |
399 | Mail and Usenet News | |
a0d0e21e | 400 | |
4633a7c4 LW |
401 | =item * |
402 | Control Flow Utilities (callbacks and exceptions etc) | |
a0d0e21e | 403 | |
4633a7c4 LW |
404 | =item * |
405 | File Handle and Input/Output Stream Utilities | |
a0d0e21e | 406 | |
4633a7c4 LW |
407 | =item * |
408 | Miscellaneous Modules | |
a0d0e21e | 409 | |
4633a7c4 | 410 | =back |
a0d0e21e | 411 | |
4633a7c4 LW |
412 | Some of the reguster CPAN sites as of this writing include the following. |
413 | You should try to choose one close to you: | |
a0d0e21e | 414 | |
4633a7c4 | 415 | =over |
a0d0e21e | 416 | |
4633a7c4 LW |
417 | =item * |
418 | ftp://ftp.sterling.com/programming/languages/perl/ | |
a0d0e21e | 419 | |
4633a7c4 LW |
420 | =item * |
421 | ftp://ftp.sedl.org/pub/mirrors/CPAN/ | |
a0d0e21e | 422 | |
4633a7c4 LW |
423 | =item * |
424 | ftp://ftp.uoknor.edu/mirrors/CPAN/ | |
a0d0e21e | 425 | |
4633a7c4 LW |
426 | =item * |
427 | ftp://ftp.delphi.com/pub/mirrors/packages/perl/CPAN/ | |
a0d0e21e | 428 | |
4633a7c4 LW |
429 | =item * |
430 | ftp://uiarchive.cso.uiuc.edu/pub/lang/perl/CPAN/ | |
a0d0e21e | 431 | |
4633a7c4 LW |
432 | =item * |
433 | ftp://ftp.cis.ufl.edu/pub/perl/CPAN/ | |
a0d0e21e | 434 | |
4633a7c4 LW |
435 | =item * |
436 | ftp://ftp.switch.ch/mirror/CPAN/ | |
a0d0e21e | 437 | |
4633a7c4 LW |
438 | =item * |
439 | ftp://ftp.sunet.se/pub/lang/perl/CPAN/ | |
a0d0e21e | 440 | |
4633a7c4 LW |
441 | =item * |
442 | ftp://ftp.ci.uminho.pt/pub/lang/perl/ | |
a0d0e21e | 443 | |
4633a7c4 LW |
444 | =item * |
445 | ftp://ftp.cs.ruu.nl/pub/PERL/CPAN/ | |
a0d0e21e | 446 | |
4633a7c4 LW |
447 | =item * |
448 | ftp://ftp.demon.co.uk/pub/mirrors/perl/CPAN/ | |
a0d0e21e | 449 | |
4633a7c4 LW |
450 | =item * |
451 | ftp://ftp.rz.ruhr-uni-bochum.de/pub/programming/languages/perl/CPAN/ | |
a0d0e21e | 452 | |
4633a7c4 LW |
453 | =item * |
454 | ftp://ftp.leo.org/pub/comp/programming/languages/perl/CPAN/ | |
a0d0e21e | 455 | |
4633a7c4 LW |
456 | =item * |
457 | ftp://ftp.pasteur.fr/pub/computing/unix/perl/CPAN/ | |
a0d0e21e | 458 | |
4633a7c4 LW |
459 | =item * |
460 | ftp://ftp.ibp.fr/pub/perl/CPAN/ | |
a0d0e21e | 461 | |
4633a7c4 LW |
462 | =item * |
463 | ftp://ftp.funet.fi/pub/languages/perl/CPAN/ | |
a0d0e21e | 464 | |
4633a7c4 LW |
465 | =item * |
466 | ftp://ftp.tekotago.ac.nz/pub/perl/CPAN/ | |
a0d0e21e | 467 | |
4633a7c4 LW |
468 | =item * |
469 | ftp://ftp.mame.mu.oz.au/pub/perl/CPAN/ | |
a0d0e21e | 470 | |
4633a7c4 LW |
471 | =item * |
472 | ftp://coombs.anu.edu.au/pub/perl/ | |
a0d0e21e | 473 | |
4633a7c4 LW |
474 | =item * |
475 | ftp://dongpo.math.ncu.edu.tw/perl/CPAN/ | |
a0d0e21e | 476 | |
4633a7c4 LW |
477 | =item * |
478 | ftp://ftp.lab.kdd.co.jp/lang/perl/CPAN/ | |
a0d0e21e | 479 | |
4633a7c4 LW |
480 | =item * |
481 | ftp://ftp.is.co.za/programming/perl/CPAN/ | |
a0d0e21e LW |
482 | |
483 | =back | |
4633a7c4 LW |
484 | |
485 | For an up-to-date listing of CPAN sites, | |
486 | see http://www.perl.com/perl/ or ftp://ftp.perl.com/perl/. |