Install Perl 5.8.8
[msysgit.git] / mingw / html / pod / perlmod.html
blob1f1dedb8d72b925ca55cfc84c45285420f683d62
1 <?xml version="1.0" ?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml">
4 <head>
5 <title>perlmod - Perl modules</title>
6 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
7 <link rev="made" href="mailto:" />
8 </head>
10 <body style="background-color: white">
11 <table border="0" width="100%" cellspacing="0" cellpadding="3">
12 <tr><td class="block" style="background-color: #cccccc" valign="middle">
13 <big><strong><span class="block">&nbsp;perlmod - Perl modules</span></strong></big>
14 </td></tr>
15 </table>
17 <p><a name="__index__"></a></p>
18 <!-- INDEX BEGIN -->
20 <ul>
22 <li><a href="#name">NAME</a></li>
23 <li><a href="#description">DESCRIPTION</a></li>
24 <ul>
26 <li><a href="#packages">Packages</a></li>
27 <li><a href="#symbol_tables">Symbol Tables</a></li>
28 <li><a href="#begin__check__init_and_end">BEGIN, CHECK, INIT and END</a></li>
29 <li><a href="#perl_classes">Perl Classes</a></li>
30 <li><a href="#perl_modules">Perl Modules</a></li>
31 <li><a href="#making_your_module_threadsafe">Making your module threadsafe</a></li>
32 </ul>
34 <li><a href="#see_also">SEE ALSO</a></li>
35 </ul>
36 <!-- INDEX END -->
38 <hr />
39 <p>
40 </p>
41 <h1><a name="name">NAME</a></h1>
42 <p>perlmod - Perl modules (packages and symbol tables)</p>
43 <p>
44 </p>
45 <hr />
46 <h1><a name="description">DESCRIPTION</a></h1>
47 <p>
48 </p>
49 <h2><a name="packages_x_package__x_namespace__x_variable__global__x_global_variable__x_global_">Packages
50 </a></h2>
51 <p>Perl provides a mechanism for alternative namespaces to protect
52 packages from stomping on each other's variables. In fact, there's
53 really no such thing as a global variable in Perl. The package
54 statement declares the compilation unit as being in the given
55 namespace. The scope of the package declaration is from the
56 declaration itself through the end of the enclosing block, <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_eval"><code>eval</code></a>,
57 or file, whichever comes first (the same scope as the <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_my"><code>my()</code></a> and
58 <code>local()</code> operators). Unqualified dynamic identifiers will be in
59 this namespace, except for those few identifiers that if unqualified,
60 default to the main package instead of the current one as described
61 below. A package statement affects only dynamic variables--including
62 those you've used <code>local()</code> on--but <em>not</em> lexical variables created
63 with my(). Typically it would be the first declaration in a file
64 included by the <code>do</code>, <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_require"><code>require</code></a>, or <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_use"><code>use</code></a> operators. You can
65 switch into a package in more than one place; it merely influences
66 which symbol table is used by the compiler for the rest of that
67 block. You can refer to variables and filehandles in other packages
68 by prefixing the identifier with the package name and a double
69 colon: <code>$Package::Variable</code>. If the package name is null, the
70 <code>main</code> package is assumed. That is, <code>$::sail</code> is equivalent to
71 <code>$main::sail</code>.</p>
72 <p>The old package delimiter was a single quote, but double colon is now the
73 preferred delimiter, in part because it's more readable to humans, and
74 in part because it's more readable to <strong>emacs</strong> macros. It also makes C++
75 programmers feel like they know what's going on--as opposed to using the
76 single quote as separator, which was there to make Ada programmers feel
77 like they knew what was going on. Because the old-fashioned syntax is still
78 supported for backwards compatibility, if you try to use a string like
79 <code>&quot;This is $owner's house&quot;</code>, you'll be accessing <code>$owner::s</code>; that is,
80 the $s variable in package <code>owner</code>, which is probably not what you meant.
81 Use braces to disambiguate, as in <code>&quot;This is ${owner}'s house&quot;</code>.
82 </p>
83 <p>Packages may themselves contain package separators, as in
84 <code>$OUTER::INNER::var</code>. This implies nothing about the order of
85 name lookups, however. There are no relative packages: all symbols
86 are either local to the current package, or must be fully qualified
87 from the outer package name down. For instance, there is nowhere
88 within package <code>OUTER</code> that <code>$INNER::var</code> refers to
89 <code>$OUTER::INNER::var</code>. <code>INNER</code> refers to a totally
90 separate global package.</p>
91 <p>Only identifiers starting with letters (or underscore) are stored
92 in a package's symbol table. All other symbols are kept in package
93 <code>main</code>, including all punctuation variables, like $_. In addition,
94 when unqualified, the identifiers STDIN, STDOUT, STDERR, ARGV,
95 ARGVOUT, ENV, INC, and SIG are forced to be in package <code>main</code>,
96 even when used for other purposes than their built-in ones. If you
97 have a package called <a href="file://C|\msysgit\mingw\html/pod/perlguts.html#item_m"><code>m</code></a>, <a href="file://C|\msysgit\mingw\html/pod/perlguts.html#item_s"><code>s</code></a>, or <code>y</code>, then you can't use the
98 qualified form of an identifier because it would be instead interpreted
99 as a pattern match, a substitution, or a transliteration.
100 </p>
101 <p>Variables beginning with underscore used to be forced into package
102 main, but we decided it was more useful for package writers to be able
103 to use leading underscore to indicate private variables and method names.
104 However, variables and functions named with a single <code>_</code>, such as
105 $_ and <code>sub _</code>, are still forced into the package <code>main</code>. See also
106 <a href="file://C|\msysgit\mingw\html/pod/perlvar.html#technical_note_on_the_syntax_of_variable_names">Technical Note on the Syntax of Variable Names in the perlvar manpage</a>.</p>
107 <p><a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_eval"><code>eval</code></a>ed strings are compiled in the package in which the <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_eval"><code>eval()</code></a> was
108 compiled. (Assignments to <code>$SIG{}</code>, however, assume the signal
109 handler specified is in the <code>main</code> package. Qualify the signal handler
110 name if you wish to have a signal handler in a package.) For an
111 example, examine <em>perldb.pl</em> in the Perl library. It initially switches
112 to the <code>DB</code> package so that the debugger doesn't interfere with variables
113 in the program you are trying to debug. At various points, however, it
114 temporarily switches back to the <code>main</code> package to evaluate various
115 expressions in the context of the <code>main</code> package (or wherever you came
116 from). See <a href="file://C|\msysgit\mingw\html/pod/perldebug.html">the perldebug manpage</a>.</p>
117 <p>The special symbol <code>__PACKAGE__</code> contains the current package, but cannot
118 (easily) be used to construct variable names.</p>
119 <p>See <a href="file://C|\msysgit\mingw\html/pod/perlsub.html">the perlsub manpage</a> for other scoping issues related to <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_my"><code>my()</code></a> and local(),
120 and <a href="file://C|\msysgit\mingw\html/pod/perlref.html">the perlref manpage</a> regarding closures.</p>
122 </p>
123 <h2><a name="symbol_tables_x_symbol_table__x_stash__x______x__main____x_typeglob__x_glob__x_alias_">Symbol Tables
124 </a></h2>
125 <p>The symbol table for a package happens to be stored in the hash of that
126 name with two colons appended. The main symbol table's name is thus
127 <code>%main::</code>, or <code>%::</code> for short. Likewise the symbol table for the nested
128 package mentioned earlier is named <code>%OUTER::INNER::</code>.</p>
129 <p>The value in each entry of the hash is what you are referring to when you
130 use the <code>*name</code> typeglob notation. In fact, the following have the same
131 effect, though the first is more efficient because it does the symbol
132 table lookups at compile time:</p>
133 <pre>
134 local *main::foo = *main::bar;
135 local $main::{foo} = $main::{bar};</pre>
136 <p>(Be sure to note the <strong>vast</strong> difference between the second line above
137 and <code>local $main::foo = $main::bar</code>. The former is accessing the hash
138 <code>%main::</code>, which is the symbol table of package <code>main</code>. The latter is
139 simply assigning scalar <code>$bar</code> in package <code>main</code> to scalar <code>$foo</code> of
140 the same package.)</p>
141 <p>You can use this to print out all the variables in a package, for
142 instance. The standard but antiquated <em>dumpvar.pl</em> library and
143 the CPAN module Devel::Symdump make use of this.</p>
144 <p>Assignment to a typeglob performs an aliasing operation, i.e.,</p>
145 <pre>
146 *dick = *richard;</pre>
147 <p>causes variables, subroutines, formats, and file and directory handles
148 accessible via the identifier <code>richard</code> also to be accessible via the
149 identifier <code>dick</code>. If you want to alias only a particular variable or
150 subroutine, assign a reference instead:</p>
151 <pre>
152 *dick = \$richard;</pre>
153 <p>Which makes $richard and $dick the same variable, but leaves
154 @richard and @dick as separate arrays. Tricky, eh?</p>
155 <p>There is one subtle difference between the following statements:</p>
156 <pre>
157 *foo = *bar;
158 *foo = \$bar;</pre>
159 <p><code>*foo = *bar</code> makes the typeglobs themselves synonymous while
160 <code>*foo = \$bar</code> makes the SCALAR portions of two distinct typeglobs
161 refer to the same scalar value. This means that the following code:</p>
162 <pre>
163 $bar = 1;
164 *foo = \$bar; # Make $foo an alias for $bar</pre>
165 <pre>
167 local $bar = 2; # Restrict changes to block
168 print $foo; # Prints '1'!
169 }</pre>
170 <p>Would print '1', because <code>$foo</code> holds a reference to the <em>original</em>
171 <code>$bar</code> -- the one that was stuffed away by <code>local()</code> and which will be
172 restored when the block ends. Because variables are accessed through the
173 typeglob, you can use <code>*foo = *bar</code> to create an alias which can be
174 localized. (But be aware that this means you can't have a separate
175 <code>@foo</code> and <code>@bar</code>, etc.)</p>
176 <p>What makes all of this important is that the Exporter module uses glob
177 aliasing as the import/export mechanism. Whether or not you can properly
178 localize a variable that has been exported from a module depends on how
179 it was exported:</p>
180 <pre>
181 @EXPORT = qw($FOO); # Usual form, can't be localized
182 @EXPORT = qw(*FOO); # Can be localized</pre>
183 <p>You can work around the first case by using the fully qualified name
184 (<code>$Package::FOO</code>) where you need a local value, or by overriding it
185 by saying <code>*FOO = *Package::FOO</code> in your script.</p>
186 <p>The <code>*x = \$y</code> mechanism may be used to pass and return cheap references
187 into or from subroutines if you don't want to copy the whole
188 thing. It only works when assigning to dynamic variables, not
189 lexicals.</p>
190 <pre>
191 %some_hash = (); # can't be my()
192 *some_hash = fn( \%another_hash );
193 sub fn {
194 local *hashsym = shift;
195 # now use %hashsym normally, and you
196 # will affect the caller's %another_hash
197 my %nhash = (); # do what you want
198 return \%nhash;
199 }</pre>
200 <p>On return, the reference will overwrite the hash slot in the
201 symbol table specified by the *some_hash typeglob. This
202 is a somewhat tricky way of passing around references cheaply
203 when you don't want to have to remember to dereference variables
204 explicitly.</p>
205 <p>Another use of symbol tables is for making ``constant'' scalars.
206 </p>
207 <pre>
208 *PI = \3.14159265358979;</pre>
209 <p>Now you cannot alter <code>$PI</code>, which is probably a good thing all in all.
210 This isn't the same as a constant subroutine, which is subject to
211 optimization at compile-time. A constant subroutine is one prototyped
212 to take no arguments and to return a constant expression. See
213 <a href="file://C|\msysgit\mingw\html/pod/perlsub.html">the perlsub manpage</a> for details on these. The <code>use constant</code> pragma is a
214 convenient shorthand for these.</p>
215 <p>You can say <code>*foo{PACKAGE}</code> and <code>*foo{NAME}</code> to find out what name and
216 package the *foo symbol table entry comes from. This may be useful
217 in a subroutine that gets passed typeglobs as arguments:</p>
218 <pre>
219 sub identify_typeglob {
220 my $glob = shift;
221 print 'You gave me ', *{$glob}{PACKAGE}, '::', *{$glob}{NAME}, &quot;\n&quot;;
223 identify_typeglob *foo;
224 identify_typeglob *bar::baz;</pre>
225 <p>This prints</p>
226 <pre>
227 You gave me main::foo
228 You gave me bar::baz</pre>
229 <p>The <code>*foo{THING}</code> notation can also be used to obtain references to the
230 individual elements of *foo. See <a href="file://C|\msysgit\mingw\html/pod/perlref.html">the perlref manpage</a>.</p>
231 <p>Subroutine definitions (and declarations, for that matter) need
232 not necessarily be situated in the package whose symbol table they
233 occupy. You can define a subroutine outside its package by
234 explicitly qualifying the name of the subroutine:</p>
235 <pre>
236 package main;
237 sub Some_package::foo { ... } # &amp;foo defined in Some_package</pre>
238 <p>This is just a shorthand for a typeglob assignment at compile time:</p>
239 <pre>
240 BEGIN { *Some_package::foo = sub { ... } }</pre>
241 <p>and is <em>not</em> the same as writing:</p>
242 <pre>
244 package Some_package;
245 sub foo { ... }
246 }</pre>
247 <p>In the first two versions, the body of the subroutine is
248 lexically in the main package, <em>not</em> in Some_package. So
249 something like this:</p>
250 <pre>
251 package main;</pre>
252 <pre>
253 $Some_package::name = &quot;fred&quot;;
254 $main::name = &quot;barney&quot;;</pre>
255 <pre>
256 sub Some_package::foo {
257 print &quot;in &quot;, __PACKAGE__, &quot;: \$name is '$name'\n&quot;;
258 }</pre>
259 <pre>
260 Some_package::foo();</pre>
261 <p>prints:</p>
262 <pre>
263 in main: $name is 'barney'</pre>
264 <p>rather than:</p>
265 <pre>
266 in Some_package: $name is 'fred'</pre>
267 <p>This also has implications for the use of the SUPER:: qualifier
268 (see <a href="file://C|\msysgit\mingw\html/pod/perlobj.html">the perlobj manpage</a>).</p>
270 </p>
271 <h2><a name="begin__check__init_and_end_x_begin__x_check__x_init__x_end_">BEGIN, CHECK, INIT and END
272 </a></h2>
273 <p>Four specially named code blocks are executed at the beginning and at the end
274 of a running Perl program. These are the <code>BEGIN</code>, <code>CHECK</code>, <code>INIT</code>, and
275 <code>END</code> blocks.</p>
276 <p>These code blocks can be prefixed with <code>sub</code> to give the appearance of a
277 subroutine (although this is not considered good style). One should note
278 that these code blocks don't really exist as named subroutines (despite
279 their appearance). The thing that gives this away is the fact that you can
280 have <strong>more than one</strong> of these code blocks in a program, and they will get
281 <strong>all</strong> executed at the appropriate moment. So you can't execute any of
282 these code blocks by name.</p>
283 <p>A <code>BEGIN</code> code block is executed as soon as possible, that is, the moment
284 it is completely defined, even before the rest of the containing file (or
285 string) is parsed. You may have multiple <code>BEGIN</code> blocks within a file (or
286 eval'ed string) -- they will execute in order of definition. Because a <code>BEGIN</code>
287 code block executes immediately, it can pull in definitions of subroutines
288 and such from other files in time to be visible to the rest of the compile
289 and run time. Once a <code>BEGIN</code> has run, it is immediately undefined and any
290 code it used is returned to Perl's memory pool.</p>
291 <p>It should be noted that <code>BEGIN</code> code blocks <strong>are</strong> executed inside string
292 <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_eval"><code>eval()</code></a>'s. The <code>CHECK</code> and <code>INIT</code> code blocks are <strong>not</strong> executed inside
293 a string eval, which e.g. can be a problem in a mod_perl environment.</p>
294 <p>An <code>END</code> code block is executed as late as possible, that is, after
295 perl has finished running the program and just before the interpreter
296 is being exited, even if it is exiting as a result of a <code>die()</code> function.
297 (But not if it's morphing into another program via <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_exec"><code>exec</code></a>, or
298 being blown out of the water by a signal--you have to trap that yourself
299 (if you can).) You may have multiple <code>END</code> blocks within a file--they
300 will execute in reverse order of definition; that is: last in, first
301 out (LIFO). <code>END</code> blocks are not executed when you run perl with the
302 <code>-c</code> switch, or if compilation fails.</p>
303 <p>Note that <code>END</code> code blocks are <strong>not</strong> executed at the end of a string
304 <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_eval"><code>eval()</code></a>: if any <code>END</code> code blocks are created in a string <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_eval"><code>eval()</code></a>,
305 they will be executed just as any other <code>END</code> code block of that package
306 in LIFO order just before the interpreter is being exited.</p>
307 <p>Inside an <code>END</code> code block, <a href="file://C|\msysgit\mingw\html/pod/perlvar.html#item__"><code>$?</code></a> contains the value that the program is
308 going to pass to <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_exit"><code>exit()</code></a>. You can modify <a href="file://C|\msysgit\mingw\html/pod/perlvar.html#item__"><code>$?</code></a> to change the exit
309 value of the program. Beware of changing <a href="file://C|\msysgit\mingw\html/pod/perlvar.html#item__"><code>$?</code></a> by accident (e.g. by
310 running something via <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_system"><code>system</code></a>).
311 </p>
312 <p><code>CHECK</code> and <code>INIT</code> code blocks are useful to catch the transition between
313 the compilation phase and the execution phase of the main program.</p>
314 <p><code>CHECK</code> code blocks are run just after the <strong>initial</strong> Perl compile phase ends
315 and before the run time begins, in LIFO order. <code>CHECK</code> code blocks are used
316 in the Perl compiler suite to save the compiled state of the program.</p>
317 <p><code>INIT</code> blocks are run just before the Perl runtime begins execution, in
318 ``first in, first out'' (FIFO) order. For example, the code generators
319 documented in <a href="file://C|\msysgit\mingw\html/utils/perlcc.html">the perlcc manpage</a> make use of <code>INIT</code> blocks to initialize and
320 resolve pointers to XSUBs.</p>
321 <p>When you use the <strong>-n</strong> and <strong>-p</strong> switches to Perl, <code>BEGIN</code> and
322 <code>END</code> work just as they do in <strong>awk</strong>, as a degenerate case.
323 Both <code>BEGIN</code> and <code>CHECK</code> blocks are run when you use the <strong>-c</strong>
324 switch for a compile-only syntax check, although your main code
325 is not.</p>
326 <p>The <strong>begincheck</strong> program makes it all clear, eventually:</p>
327 <pre>
328 #!/usr/bin/perl</pre>
329 <pre>
330 # begincheck</pre>
331 <pre>
332 print &quot; 8. Ordinary code runs at runtime.\n&quot;;</pre>
333 <pre>
334 END { print &quot;14. So this is the end of the tale.\n&quot; }
335 INIT { print &quot; 5. INIT blocks run FIFO just before runtime.\n&quot; }
336 CHECK { print &quot; 4. So this is the fourth line.\n&quot; }</pre>
337 <pre>
338 print &quot; 9. It runs in order, of course.\n&quot;;</pre>
339 <pre>
340 BEGIN { print &quot; 1. BEGIN blocks run FIFO during compilation.\n&quot; }
341 END { print &quot;13. Read <a href="//C|\msysgit\mingw\html/pod/perlmod.html">perlmod</a> for the rest of the story.\n&quot; }
342 CHECK { print &quot; 3. CHECK blocks run LIFO at compilation's end.\n&quot; }
343 INIT { print &quot; 6. Run this again, using Perl's -c switch.\n&quot; }</pre>
344 <pre>
345 print &quot;10. This is anti-obfuscated code.\n&quot;;</pre>
346 <pre>
347 END { print &quot;12. END blocks run LIFO at quitting time.\n&quot; }
348 BEGIN { print &quot; 2. So this line comes out second.\n&quot; }
349 INIT { print &quot; 7. You'll see the difference right away.\n&quot; }</pre>
350 <pre>
351 print &quot;11. It merely _looks_ like it should be confusing.\n&quot;;</pre>
352 <pre>
353 __END__</pre>
355 </p>
356 <h2><a name="perl_classes_x_class__x__isa_">Perl Classes
357 </a></h2>
358 <p>There is no special class syntax in Perl, but a package may act
359 as a class if it provides subroutines to act as methods. Such a
360 package may also derive some of its methods from another class (package)
361 by listing the other package <code>name(s)</code> in its global @ISA array (which
362 must be a package global, not a lexical).</p>
363 <p>For more on this, see <a href="file://C|\msysgit\mingw\html/pod/perltoot.html">the perltoot manpage</a> and <a href="file://C|\msysgit\mingw\html/pod/perlobj.html">the perlobj manpage</a>.</p>
365 </p>
366 <h2><a name="perl_modules_x_module_">Perl Modules
367 </a></h2>
368 <p>A module is just a set of related functions in a library file, i.e.,
369 a Perl package with the same name as the file. It is specifically
370 designed to be reusable by other modules or programs. It may do this
371 by providing a mechanism for exporting some of its symbols into the
372 symbol table of any package using it, or it may function as a class
373 definition and make its semantics available implicitly through
374 method calls on the class and its objects, without explicitly
375 exporting anything. Or it can do a little of both.</p>
376 <p>For example, to start a traditional, non-OO module called Some::Module,
377 create a file called <em>Some/Module.pm</em> and start with this template:</p>
378 <pre>
379 package Some::Module; # assumes Some/Module.pm</pre>
380 <pre>
381 use strict;
382 use warnings;</pre>
383 <pre>
384 BEGIN {
385 use Exporter ();
386 our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);</pre>
387 <pre>
388 # set the version for version checking
389 $VERSION = 1.00;
390 # if using RCS/CVS, this may be preferred
391 $VERSION = sprintf &quot;%d.%03d&quot;, q$Revision: 1.1 $ =~ /(\d+)/g;</pre>
392 <pre>
393 @ISA = qw(Exporter);
394 @EXPORT = qw(&amp;func1 &amp;func2 &amp;func4);
395 %EXPORT_TAGS = ( ); # eg: TAG =&gt; [ qw!name1 name2! ],</pre>
396 <pre>
397 # your exported package globals go here,
398 # as well as any optionally exported functions
399 @EXPORT_OK = qw($Var1 %Hashit &amp;func3);
401 our @EXPORT_OK;</pre>
402 <pre>
403 # exported package globals go here
404 our $Var1;
405 our %Hashit;</pre>
406 <pre>
407 # non-exported package globals go here
408 our @more;
409 our $stuff;</pre>
410 <pre>
411 # initialize package globals, first exported ones
412 $Var1 = '';
413 %Hashit = ();</pre>
414 <pre>
415 # then the others (which are still accessible as $Some::Module::stuff)
416 $stuff = '';
417 @more = ();</pre>
418 <pre>
419 # all file-scoped lexicals must be created before
420 # the functions below that use them.</pre>
421 <pre>
422 # file-private lexicals go here
423 my $priv_var = '';
424 my %secret_hash = ();</pre>
425 <pre>
426 # here's a file-private function as a closure,
427 # callable as &amp;$priv_func; it cannot be prototyped.
428 my $priv_func = sub {
429 # stuff goes here.
430 };</pre>
431 <pre>
432 # make all your functions, whether exported or not;
433 # remember to put something interesting in the {} stubs
434 sub func1 {} # no prototype
435 sub func2() {} # proto'd void
436 sub func3($$) {} # proto'd to 2 scalars</pre>
437 <pre>
438 # this one isn't exported, but could be called!
439 sub func4(\%) {} # proto'd to 1 hash ref</pre>
440 <pre>
441 END { } # module clean-up code here (global destructor)</pre>
442 <pre>
443 ## YOUR CODE GOES HERE</pre>
444 <pre>
445 1; # don't forget to return a true value from the file</pre>
446 <p>Then go on to declare and use your variables in functions without
447 any qualifications. See <a href="file://C|\msysgit\mingw\html/lib/Exporter.html">the Exporter manpage</a> and the <a href="file://C|\msysgit\mingw\html/pod/perlmodlib.html">the perlmodlib manpage</a> for
448 details on mechanics and style issues in module creation.</p>
449 <p>Perl modules are included into your program by saying</p>
450 <pre>
451 use Module;</pre>
452 <p>or</p>
453 <pre>
454 use Module LIST;</pre>
455 <p>This is exactly equivalent to</p>
456 <pre>
457 BEGIN { require Module; import Module; }</pre>
458 <p>or</p>
459 <pre>
460 BEGIN { require Module; import Module LIST; }</pre>
461 <p>As a special case</p>
462 <pre>
463 use Module ();</pre>
464 <p>is exactly equivalent to</p>
465 <pre>
466 BEGIN { require Module; }</pre>
467 <p>All Perl module files have the extension <em>.pm</em>. The <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_use"><code>use</code></a> operator
468 assumes this so you don't have to spell out ``<em>Module.pm</em>'' in quotes.
469 This also helps to differentiate new modules from old <em>.pl</em> and
470 <em>.ph</em> files. Module names are also capitalized unless they're
471 functioning as pragmas; pragmas are in effect compiler directives,
472 and are sometimes called ``pragmatic modules'' (or even ``pragmata''
473 if you're a classicist).</p>
474 <p>The two statements:</p>
475 <pre>
476 require SomeModule;
477 require &quot;SomeModule.pm&quot;;</pre>
478 <p>differ from each other in two ways. In the first case, any double
479 colons in the module name, such as <code>Some::Module</code>, are translated
480 into your system's directory separator, usually ``/''. The second
481 case does not, and would have to be specified literally. The other
482 difference is that seeing the first <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_require"><code>require</code></a> clues in the compiler
483 that uses of indirect object notation involving ``SomeModule'', as
484 in <code>$ob = purge SomeModule</code>, are method calls, not function calls.
485 (Yes, this really can make a difference.)</p>
486 <p>Because the <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_use"><code>use</code></a> statement implies a <code>BEGIN</code> block, the importing
487 of semantics happens as soon as the <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_use"><code>use</code></a> statement is compiled,
488 before the rest of the file is compiled. This is how it is able
489 to function as a pragma mechanism, and also how modules are able to
490 declare subroutines that are then visible as list or unary operators for
491 the rest of the current file. This will not work if you use <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_require"><code>require</code></a>
492 instead of <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_use"><code>use</code></a>. With <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_require"><code>require</code></a> you can get into this problem:</p>
493 <pre>
494 require Cwd; # make Cwd:: accessible
495 $here = Cwd::getcwd();</pre>
496 <pre>
497 use Cwd; # import names from Cwd::
498 $here = getcwd();</pre>
499 <pre>
500 require Cwd; # make Cwd:: accessible
501 $here = getcwd(); # oops! no main::getcwd()</pre>
502 <p>In general, <code>use Module ()</code> is recommended over <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_require"><code>require Module</code></a>,
503 because it determines module availability at compile time, not in the
504 middle of your program's execution. An exception would be if two modules
505 each tried to <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_use"><code>use</code></a> each other, and each also called a function from
506 that other module. In that case, it's easy to use <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_require"><code>require</code></a> instead.</p>
507 <p>Perl packages may be nested inside other package names, so we can have
508 package names containing <code>::</code>. But if we used that package name
509 directly as a filename it would make for unwieldy or impossible
510 filenames on some systems. Therefore, if a module's name is, say,
511 <code>Text::Soundex</code>, then its definition is actually found in the library
512 file <em>Text/Soundex.pm</em>.</p>
513 <p>Perl modules always have a <em>.pm</em> file, but there may also be
514 dynamically linked executables (often ending in <em>.so</em>) or autoloaded
515 subroutine definitions (often ending in <em>.al</em>) associated with the
516 module. If so, these will be entirely transparent to the user of
517 the module. It is the responsibility of the <em>.pm</em> file to load
518 (or arrange to autoload) any additional functionality. For example,
519 although the POSIX module happens to do both dynamic loading and
520 autoloading, the user can say just <a href="file://C|\msysgit\mingw\html/pod/perlfunc.html#item_use"><code>use POSIX</code></a> to get it all.</p>
522 </p>
523 <h2><a name="making_your_module_threadsafe_x_threadsafe__x_thread_safe__x_module__threadsafe__x_module__thread_safe__x_clone__x_clone_skip__x_thread__x_threads__x_ithread_">Making your module threadsafe
526 </a></h2>
527 <p>Since 5.6.0, Perl has had support for a new type of threads called
528 interpreter threads (ithreads). These threads can be used explicitly
529 and implicitly.</p>
530 <p>Ithreads work by cloning the data tree so that no data is shared
531 between different threads. These threads can be used by using the <code>threads</code>
532 module or by doing <code>fork()</code> on win32 (fake <code>fork()</code> support). When a
533 thread is cloned all Perl data is cloned, however non-Perl data cannot
534 be cloned automatically. Perl after 5.7.2 has support for the <code>CLONE</code>
535 special subroutine. In <code>CLONE</code> you can do whatever
536 you need to do,
537 like for example handle the cloning of non-Perl data, if necessary.
538 <code>CLONE</code> will be called once as a class method for every package that has it
539 defined (or inherits it). It will be called in the context of the new thread,
540 so all modifications are made in the new area. Currently CLONE is called with
541 no parameters other than the invocant package name, but code should not assume
542 that this will remain unchanged, as it is likely that in future extra parameters
543 will be passed in to give more information about the state of cloning.</p>
544 <p>If you want to CLONE all objects you will need to keep track of them per
545 package. This is simply done using a hash and Scalar::Util::weaken().</p>
546 <p>Perl after 5.8.7 has support for the <code>CLONE_SKIP</code> special subroutine.
547 Like <code>CLONE</code>, <code>CLONE_SKIP</code> is called once per package; however, it is
548 called just before cloning starts, and in the context of the parent
549 thread. If it returns a true value, then no objects of that class will
550 be cloned; or rather, they will be copied as unblessed, undef values.
551 This provides a simple mechanism for making a module threadsafe; just add
552 <code>sub CLONE_SKIP { 1 }</code> at the top of the class, and <code>DESTROY()</code> will be
553 now only be called once per object. Of course, if the child thread needs
554 to make use of the objects, then a more sophisticated approach is
555 needed.</p>
556 <p>Like <code>CLONE</code>, <code>CLONE_SKIP</code> is currently called with no parameters other
557 than the invocant package name, although that may change. Similarly, to
558 allow for future expansion, the return value should be a single <code>0</code> or
559 <code>1</code> value.</p>
561 </p>
562 <hr />
563 <h1><a name="see_also">SEE ALSO</a></h1>
564 <p>See <a href="file://C|\msysgit\mingw\html/pod/perlmodlib.html">the perlmodlib manpage</a> for general style issues related to building Perl
565 modules and classes, as well as descriptions of the standard library
566 and CPAN, <a href="file://C|\msysgit\mingw\html/lib/Exporter.html">the Exporter manpage</a> for how Perl's standard import/export mechanism
567 works, <a href="file://C|\msysgit\mingw\html/pod/perltoot.html">the perltoot manpage</a> and <a href="file://C|\msysgit\mingw\html/pod/perltooc.html">the perltooc manpage</a> for an in-depth tutorial on
568 creating classes, <a href="file://C|\msysgit\mingw\html/pod/perlobj.html">the perlobj manpage</a> for a hard-core reference document on
569 objects, <a href="file://C|\msysgit\mingw\html/pod/perlsub.html">the perlsub manpage</a> for an explanation of functions and scoping,
570 and <a href="file://C|\msysgit\mingw\html/pod/perlxstut.html">the perlxstut manpage</a> and <a href="file://C|\msysgit\mingw\html/pod/perlguts.html">the perlguts manpage</a> for more information on writing
571 extension modules.</p>
572 <table border="0" width="100%" cellspacing="0" cellpadding="3">
573 <tr><td class="block" style="background-color: #cccccc" valign="middle">
574 <big><strong><span class="block">&nbsp;perlmod - Perl modules</span></strong></big>
575 </td></tr>
576 </table>
578 </body>
580 </html>