File: perlmod.html

package info (click to toggle)
perl-doc-html 5.26.0-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 39,400 kB
  • sloc: xml: 36; makefile: 7
file content (976 lines) | stat: -rw-r--r-- 64,768 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <title>perlmod - perldoc.perl.org</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <meta http-equiv="Content-Language" content="en-gb">
  <link rel="search" type="application/opensearchdescription+xml" title="Search perldoc.perl.org" href="/static/search.xml"/>
  <link href="static/css-20100830.css" rel="stylesheet" rev="stylesheet" type="text/css" media="screen">
  <link href="static/exploreperl.css" rel="stylesheet" rev="stylesheet" type="text/css">
</head>

<body onLoad="perldoc.startup();" onPageShow="if (event.persisted) perldoc.startup();">
    <div id="page">
      
      <div id="header">
	<div id="homepage_link">
	  <a href="index.html"></a>
	</div>
	<div id="strapline">
	  Perl Programming Documentation
	</div>
	<div id="download_link" class="download">
	  <a href="http://www.perl.org/get.html">Download Perl</a>
	</div>
	<div id="explore_link" class="download">
	  <a id="explore_anchor" href="#">Explore</a>
	</div>
      </div>
      
      <div id="body">
        <div id="left_column">
          <div class="side_group">
            
	    <div class="side_panel doc_panel">
              <p>Manual</p>
              <ul>
                <li><a href="index-overview.html">Overview</a>
                <li><a href="index-tutorials.html">Tutorials</a>
                <li><a href="index-faq.html">FAQs</a>
                <li><a href="index-history.html">History / Changes</a>
                <li><a href="index-licence.html">License</a>
              </ul>
            </div>
            <div class="side_panel doc_panel">
              <p>Reference</p>
              <ul>
                <li><a href="index-language.html">Language</a>
                <li><a href="index-functions.html">Functions</a>
                <li><a href="perlop.html">Operators</a>
                <li><a href="perlvar.html">Special Variables</a>
                <li><a href="index-pragmas.html">Pragmas</a>
                <li><a href="index-utilities.html">Utilities</a>
                <li><a href="index-internals.html">Internals</a>
                <li><a href="index-platforms.html">Platform Specific</a>
              </ul>
            </div>
            <div class="side_panel doc_panel">
              <p>Modules</p>
              <ul>
		<li>
		
                
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		
                  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		
                  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		
                  
		
                  
		    
		  
		
                  
		
                  
		
		
                    <a href="index-modules-A.html">A</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-B.html">B</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-C.html">C</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-D.html">D</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-E.html">E</a>
                    
                      
                        <li>
                      
                    
                
                    <a href="index-modules-F.html">F</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-G.html">G</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-H.html">H</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-I.html">I</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-L.html">L</a>
                    
                      
                        <li>
                      
                    
                
                    <a href="index-modules-M.html">M</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-N.html">N</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-O.html">O</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-P.html">P</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-S.html">S</a>
                    
                      
                        <li>
                      
                    
                
                    <a href="index-modules-T.html">T</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-U.html">U</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-X.html">X</a>
                    
                
              </ul>
            </div>
            
	      <div class="side_panel doc_panel">
		<p>Tools</p>
		<ul>
		  <li><a href="preferences.html">Preferences</a>
		</ul>
	      </div>
            
          </div>
        </div>
        <div id="centre_column">
          <div id="content_header">
            <div id="title_bar">
              <div id="page_name">
                <h1>perlmod</h1>
              </div>
              <div id="perl_version">
                Perl 5 version 26.0 documentation
              </div>
              <div class="page_links" id="page_links_top">
                <a href="#" onClick="toolbar.goToTop();return false;">Go to top</a>
		
              </div>
	      <div class="page_links" id="page_links_bottom">
		
                  <a href="#" id="page_index_toggle">Show page index</a> &bull;
		
                <a href="#" id="recent_pages_toggle">Show recent pages</a>		
	      </div>
	      <div id="search_form">
		<form action="search.html" method="GET" id="search">
		  <input type="text" name="q" id="search_box" alt="Search">
		</form>
	      </div>
            </div>
            <div id="breadcrumbs">
                
    <a href="index.html">Home</a> &gt;
    
      
        <a href="index-language.html">Language reference</a> &gt;
      
    
    perlmod
  

            </div>
          </div>
          <div id="content_body">
	    <!--[if lt IE 7]>
 <div class="noscript">
   <p>
     <strong>It looks like you're using Internet Explorer 6. This is a very old
     browser which does not offer full support for modern websites.</strong>
   </p>
   <p>
     Unfortunately this means that this website will not work on
     your computer.
   </p>
   <p>
     Don't miss out though! To view the site (and get a better experience from
     many other websites), simply upgrade to
     <a href="http://www.microsoft.com/windows/Internet-explorer/default.aspx">Internet
Explorer 8</a>
     or download an alternative browser such as
     <a href="http://www.mozilla.com/en-US/firefox/firefox.html">Firefox</a>,
     <a href="http://www.apple.com/safari/download/">Safari</a>, or
     <a href="http://www.google.co.uk/chrome">Google Chrome</a>.
   </p>
   <p>
     All of these browsers are free. If you're using a PC at work, you may
     need to contact your IT administrator.
   </p>
 </div>
<![endif]-->
	    <noscript>
	      <div class="noscript">
	      <p>
                <strong>Please note: Many features of this site require JavaScript. You appear to have JavaScript disabled,
	        or are running a non-JavaScript capable web browser.</strong>
	      </p>
	      <p>
		To get the best experience, please enable JavaScript or download a modern web browser such as <a href="http://www.microsoft.com/windows/Internet-explorer/default.aspx">Internet Explorer 8</a>, <a href="http://www.mozilla.com/en-US/firefox/firefox.html">Firefox</a>, <a href="http://www.apple.com/safari/download/">Safari</a>, or <a href="http://www.google.co.uk/chrome">Google Chrome</a>.
              </p>
	      </div>
	    </noscript>

	    <div id="recent_pages" class="hud_container">
	      <div id="recent_pages_header" class="hud_header">
		<div id="recent_pages_close" class="hud_close"><a href="#" onClick="recentPages.hide();return false;"></a></div>
		<div id="recent_pages_title" class="hud_title"><span class="hud_span_top">Recently read</span></div>
		<div id="recent_pages_topright" class="hud_topright"></div>
	      </div>
	      <div id="recent_pages_content" class="hud_content">
	      </div>
	      <div id="recent_pages_footer" class="hud_footer">
		<div id="recent_pages_bottomleft" class="hud_bottomleft"></div>
		<div id="recent_pages_bottom" class="hud_bottom"><span class="hud_span_bottom"></span></div>
		<div id="recent_pages_resize" class="hud_resize"></div>
	      </div>
	    </div>
  
	    <div id="from_search"></div>
            <h1>perlmod</h1>


  <!--    -->
<ul><li><a href="#NAME">NAME</a><li><a href="#DESCRIPTION">DESCRIPTION</a><ul><li><a href="#Is-this-the-document-you-were-after%3f">Is this the document you were after?</a><li><a href="#Packages">Packages
    </a><li><a href="#Symbol-Tables">Symbol Tables
      </a><li><a href="#BEGIN%2c-UNITCHECK%2c-CHECK%2c-INIT-and-END">BEGIN, UNITCHECK, CHECK, INIT and END
    </a><li><a href="#Perl-Classes">Perl Classes
 </a><li><a href="#Perl-Modules">Perl Modules
</a><li><a href="#Making-your-module-threadsafe">Making your module threadsafe
 
 
    </a></ul><li><a href="#SEE-ALSO">SEE ALSO</a></ul><a name="NAME"></a><h1>NAME</h1>
<p>perlmod - Perl modules (packages and symbol tables)</p>
<a name="DESCRIPTION"></a><h1>DESCRIPTION</h1>
<a name="Is-this-the-document-you-were-after%3f"></a><h2>Is this the document you were after?</h2>
<p>There are other documents which might contain the information that you're
looking for:</p>
<ul>
<li><a name="This-doc"></a><b>This doc</b>
<p>Perl's packages, namespaces, and some info on classes.</p>
</li>
<li><a name="the-perlnewmod-manpage"></a><b><a href="perlnewmod.html">perlnewmod</a></b>
<p>Tutorial on making a new module.</p>
</li>
<li><a name="the-perlmodstyle-manpage"></a><b><a href="perlmodstyle.html">perlmodstyle</a></b>
<p>Best practices for making a new module.</p>
</li>
</ul>
<a name="Packages"></a><h2>Packages
    </h2>
<p>Unlike Perl 4, in which all the variables were dynamic and shared one
global name space, causing maintainability problems, Perl 5 provides two
mechanisms for protecting code from having its variables stomped on by
other code: lexically scoped variables created with <code class="inline"><a class="l_k" href="functions/my.html">my</a></code> or <code class="inline"><a class="l_k" href="functions/state.html">state</a></code> and
namespaced global variables, which are exposed via the <code class="inline"><span class="w">vars</span></code>
 pragma,
or the <code class="inline"><a class="l_k" href="functions/our.html">our</a></code> keyword. Any global variable is considered to
be part of a namespace and can be accessed via a "fully qualified form".
Conversely, any lexically scoped variable is considered to be part of
that lexical-scope, and does not have a "fully qualified form".</p>
<p>In perl namespaces are called "packages" and
the <code class="inline"><a class="l_k" href="functions/package.html">package</a></code> declaration tells the compiler which
namespace to prefix to <code class="inline"><a class="l_k" href="functions/our.html">our</a></code> variables and unqualified dynamic names.
This both protects
against accidental stomping and provides an interface for deliberately
clobbering global dynamic variables declared and used in other scopes or
packages, when that is what you want to do.</p>
<p>The scope of the <code class="inline"><a class="l_k" href="functions/package.html">package</a></code> declaration is from the
declaration itself through the end of the enclosing block, <code class="inline"><a class="l_k" href="functions/eval.html">eval</a></code>,
or file, whichever comes first (the same scope as the my(), our(), state(), and
local() operators, and also the effect
of the experimental "reference aliasing," which may change), or until
the next <code class="inline"><a class="l_k" href="functions/package.html">package</a></code> declaration.  Unqualified dynamic identifiers will be in
this namespace, except for those few identifiers that, if unqualified,
default to the main package instead of the current one as described
below.  A <code class="inline"><a class="l_k" href="functions/package.html">package</a></code> statement affects only dynamic global
symbols, including subroutine names, and variables you've used local()
on, but <i>not</i> lexical variables created with my(), our() or state().</p>
<p>Typically, a <code class="inline"><a class="l_k" href="functions/package.html">package</a></code> statement is the first declaration in a file
included in a program by one of the <code class="inline"><a class="l_k" href="functions/do.html">do</a></code>, <code class="inline"><a class="l_k" href="functions/require.html">require</a></code>, or <code class="inline"><a class="l_k" href="functions/use.html">use</a></code> operators.  You can
switch into a package in more than one place: <code class="inline"><a class="l_k" href="functions/package.html">package</a></code> has no
effect beyond specifying which symbol table the compiler will use for
dynamic symbols for the rest of that block or until the next <code class="inline"><a class="l_k" href="functions/package.html">package</a></code> statement.
You can refer to variables and filehandles in other packages
by prefixing the identifier with the package name and a double
colon: <code class="inline"><span class="i">$Package::Variable</span></code>
.  If the package name is null, the
<code class="inline"><span class="w">main</span></code>
 package is assumed.  That is, <code class="inline"><span class="i">$::sail</span></code>
 is equivalent to
<code class="inline"><span class="i">$main::sail</span></code>
.</p>
<p>The old package delimiter was a single quote, but double colon is now the
preferred delimiter, in part because it's more readable to humans, and
in part because it's more readable to <b>emacs</b> macros.  It also makes C++
programmers feel like they know what's going on--as opposed to using the
single quote as separator, which was there to make Ada programmers feel
like they knew what was going on.  Because the old-fashioned syntax is still
supported for backwards compatibility, if you try to use a string like
<code class="inline"><span class="q">&quot;This is $owner&#39;s house&quot;</span></code>
, you'll be accessing <code class="inline"><span class="i">$owner::s</span></code>
; that is,
the $s variable in package <code class="inline"><span class="w">owner</span></code>
, which is probably not what you meant.
Use braces to disambiguate, as in <code class="inline"><span class="q">&quot;This is ${owner}&#39;s house&quot;</span></code>
.
 </p>
<p>Packages may themselves contain package separators, as in
<code class="inline"><span class="i">$OUTER::INNER::var</span></code>
.  This implies nothing about the order of
name lookups, however.  There are no relative packages: all symbols
are either local to the current package, or must be fully qualified
from the outer package name down.  For instance, there is nowhere
within package <code class="inline"><span class="w">OUTER</span></code>
 that <code class="inline"><span class="i">$INNER::var</span></code>
 refers to
<code class="inline"><span class="i">$OUTER::INNER::var</span></code>
.  <code class="inline"><span class="w">INNER</span></code>
 refers to a totally
separate global package. The custom of treating package names as a
hierarchy is very strong, but the language in no way enforces it.</p>
<p>Only identifiers starting with letters (or underscore) are stored
in a package's symbol table.  All other symbols are kept in package
<code class="inline"><span class="w">main</span></code>
, including all punctuation variables, like $_.  In addition,
when unqualified, the identifiers STDIN, STDOUT, STDERR, ARGV,
ARGVOUT, ENV, INC, and SIG are forced to be in package <code class="inline"><span class="w">main</span></code>
,
even when used for other purposes than their built-in ones.  If you
have a package called <code class="inline"><a class="l_k" href="functions/m.html">m</a></code>, <code class="inline"><a class="l_k" href="functions/s.html">s</a></code>, or <code class="inline"><a class="l_k" href="functions/y.html">y</a></code>, then you can't use the
qualified form of an identifier because it would be instead interpreted
as a pattern match, a substitution, or a transliteration.
</p>
<p>Variables beginning with underscore used to be forced into package
main, but we decided it was more useful for package writers to be able
to use leading underscore to indicate private variables and method names.
However, variables and functions named with a single <code class="inline"><span class="w">_</span></code>
, such as
$_ and <code class="inline"><a name="_"></a>sub <span class="m">_</span></code>
, are still forced into the package <code class="inline"><span class="w">main</span></code>
.  See also
<a href="perlvar.html#The-Syntax-of-Variable-Names">The Syntax of Variable Names in perlvar</a>.</p>
<p><code class="inline"><a class="l_k" href="functions/eval.html">eval</a></code>ed strings are compiled in the package in which the eval() was
compiled.  (Assignments to <code class="inline"><span class="i">$SIG</span>{}</code>
, however, assume the signal
handler specified is in the <code class="inline"><span class="w">main</span></code>
 package.  Qualify the signal handler
name if you wish to have a signal handler in a package.)  For an
example, examine <i>perldb.pl</i> in the Perl library.  It initially switches
to the <code class="inline"><span class="w">DB</span></code>
 package so that the debugger doesn't interfere with variables
in the program you are trying to debug.  At various points, however, it
temporarily switches back to the <code class="inline"><span class="w">main</span></code>
 package to evaluate various
expressions in the context of the <code class="inline"><span class="w">main</span></code>
 package (or wherever you came
from).  See <a href="perldebug.html">perldebug</a>.</p>
<p>The special symbol <code class="inline"><span class="w">__PACKAGE__</span></code>
 contains the current package, but cannot
(easily) be used to construct variable names. After <code class="inline"><a class="l_k" href="functions/my.html">my($foo)</a></code> has hidden
package variable <code class="inline"><span class="i">$foo</span></code>
, it can still be accessed, without knowing what
package you are in, as <code class="inline"><span class="i">$</span>{<span class="w">__PACKAGE__</span>.<span class="q">&#39;::foo&#39;</span>}</code>
.</p>
<p>See <a href="perlsub.html">perlsub</a> for other scoping issues related to my() and local(),
and <a href="perlref.html">perlref</a> regarding closures.</p>
<a name="Symbol-Tables"></a><h2>Symbol Tables
      </h2>
<p>The symbol table for a package happens to be stored in the hash of that
name with two colons appended.  The main symbol table's name is thus
<code class="inline"><span class="i">%main::</span></code>
, or <code class="inline"><span class="i">%::</span></code>
 for short.  Likewise the symbol table for the nested
package mentioned earlier is named <code class="inline"><span class="i">%OUTER::INNER::</span></code>
.</p>
<p>The value in each entry of the hash is what you are referring to when you
use the <code class="inline"><span class="i">*name</span></code>
 typeglob notation.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/local.html">local</a> <span class="i">*main::foo</span>    = <span class="i">*main::bar</span><span class="sc">;</span></li></ol></pre><p>You can use this to print out all the variables in a package, for
instance.  The standard but antiquated <i>dumpvar.pl</i> library and
the CPAN module Devel::Symdump make use of this.</p>
<p>The results of creating new symbol table entries directly or modifying any
entries that are not already typeglobs are undefined and subject to change
between releases of perl.</p>
<p>Assignment to a typeglob performs an aliasing operation, i.e.,</p>
<pre class="verbatim"><ol><li>    <span class="i">*dick</span> = <span class="i">*richard</span><span class="sc">;</span></li></ol></pre><p>causes variables, subroutines, formats, and file and directory handles
accessible via the identifier <code class="inline"><span class="w">richard</span></code>
 also to be accessible via the
identifier <code class="inline"><span class="w">dick</span></code>
.  If you want to alias only a particular variable or
subroutine, assign a reference instead:</p>
<pre class="verbatim"><ol><li>    <span class="i">*dick</span> = \<span class="i">$richard</span><span class="sc">;</span></li></ol></pre><p>Which makes $richard and $dick the same variable, but leaves
@richard and @dick as separate arrays.  Tricky, eh?</p>
<p>There is one subtle difference between the following statements:</p>
<pre class="verbatim"><ol><li>    <span class="i">*foo</span> = <span class="i">*bar</span><span class="sc">;</span></li><li>    <span class="i">*foo</span> = \<span class="i">$bar</span><span class="sc">;</span></li></ol></pre><p><code class="inline"><span class="i">*foo</span> = <span class="i">*bar</span></code>
 makes the typeglobs themselves synonymous while
<code class="inline"><span class="i">*foo</span> = \<span class="i">$bar</span></code>
 makes the SCALAR portions of two distinct typeglobs
refer to the same scalar value. This means that the following code:</p>
<pre class="verbatim"><ol><li>    <span class="i">$bar</span> = <span class="n">1</span><span class="sc">;</span></li><li>    <span class="i">*foo</span> = \<span class="i">$bar</span><span class="sc">;</span>       <span class="c"># Make $foo an alias for $bar</span></li><li></li><li>    <span class="s">{</span></li><li>        <a class="l_k" href="functions/local.html">local</a> <span class="i">$bar</span> = <span class="n">2</span><span class="sc">;</span> <span class="c"># Restrict changes to block</span></li><li>        <a class="l_k" href="functions/print.html">print</a> <span class="i">$foo</span><span class="sc">;</span>     <span class="c"># Prints &#39;1&#39;!</span></li><li>    <span class="s">}</span></li></ol></pre><p>Would print '1', because <code class="inline"><span class="i">$foo</span></code>
 holds a reference to the <i>original</i>
<code class="inline"><span class="i">$bar</span></code>
. The one that was stuffed away by <code class="inline"><a class="l_k" href="functions/local.html">local()</a></code> and which will be
restored when the block ends. Because variables are accessed through the
typeglob, you can use <code class="inline"><span class="i">*foo</span> = <span class="i">*bar</span></code>
 to create an alias which can be
localized. (But be aware that this means you can't have a separate
<code class="inline"><span class="i">@foo</span></code>
 and <code class="inline"><span class="i">@bar</span></code>
, etc.)</p>
<p>What makes all of this important is that the Exporter module uses glob
aliasing as the import/export mechanism. Whether or not you can properly
localize a variable that has been exported from a module depends on how
it was exported:</p>
<pre class="verbatim"><ol><li>    <span class="i">@EXPORT</span> = <span class="q">qw($FOO)</span><span class="sc">;</span> <span class="c"># Usual form, can&#39;t be localized</span></li><li>    <span class="i">@EXPORT</span> = <span class="q">qw(*FOO)</span><span class="sc">;</span> <span class="c"># Can be localized</span></li></ol></pre><p>You can work around the first case by using the fully qualified name
(<code class="inline"><span class="i">$Package::FOO</span></code>
) where you need a local value, or by overriding it
by saying <code class="inline"><span class="i">*FOO</span> = <span class="i">*Package::FOO</span></code>
 in your script.</p>
<p>The <code class="inline"><span class="i">*x</span> = \<span class="i">$y</span></code>
 mechanism may be used to pass and return cheap references
into or from subroutines if you don't want to copy the whole
thing.  It only works when assigning to dynamic variables, not
lexicals.</p>
<pre class="verbatim"><ol><li>    <span class="i">%some_hash</span> = <span class="s">(</span><span class="s">)</span><span class="sc">;</span>			<span class="c"># can&#39;t be my()</span></li><li>    <span class="i">*some_hash</span> = <span class="i">fn</span><span class="s">(</span> \<span class="i">%another_hash</span> <span class="s">)</span><span class="sc">;</span></li><li><a name="fn"></a>    sub <span class="m">fn</span> <span class="s">{</span></li><li>	<a class="l_k" href="functions/local.html">local</a> <span class="i">*hashsym</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>	<span class="c"># now use %hashsym normally, and you</span></li><li>	<span class="c"># will affect the caller&#39;s %another_hash</span></li><li>	<a class="l_k" href="functions/my.html">my</a> <span class="i">%nhash</span> = <span class="s">(</span><span class="s">)</span><span class="sc">;</span> <span class="c"># do what you want</span></li><li>	<a class="l_k" href="functions/return.html">return</a> \<span class="i">%nhash</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>On return, the reference will overwrite the hash slot in the
symbol table specified by the *some_hash typeglob.  This
is a somewhat tricky way of passing around references cheaply
when you don't want to have to remember to dereference variables
explicitly.</p>
<p>Another use of symbol tables is for making "constant" scalars.
 </p>
<pre class="verbatim"><ol><li>    <span class="i">*PI</span> = \<span class="n">3.14159265358979</span><span class="sc">;</span></li></ol></pre><p>Now you cannot alter <code class="inline"><span class="i">$PI</span></code>
, which is probably a good thing all in all.
This isn't the same as a constant subroutine, which is subject to
optimization at compile-time.  A constant subroutine is one prototyped
to take no arguments and to return a constant expression.  See
<a href="perlsub.html">perlsub</a> for details on these.  The <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">constant</span></code>
 pragma is a
convenient shorthand for these.</p>
<p>You can say <code class="inline"><span class="i">*foo</span>{<span class="w">PACKAGE</span>}</code>
 and <code class="inline"><span class="i">*foo</span>{<span class="w">NAME</span>}</code>
 to find out what name and
package the *foo symbol table entry comes from.  This may be useful
in a subroutine that gets passed typeglobs as arguments:</p>
<pre class="verbatim"><ol><li><a name="identify_typeglob"></a>    sub <span class="m">identify_typeglob</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/my.html">my</a> <span class="i">$glob</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>        <a class="l_k" href="functions/print.html">print</a> <span class="q">&#39;You gave me &#39;</span><span class="cm">,</span> <span class="i">*</span>{<span class="i">$glob</span>}{<span class="w">PACKAGE</span>}<span class="cm">,</span></li><li>            <span class="q">&#39;::&#39;</span><span class="cm">,</span> <span class="i">*</span>{<span class="i">$glob</span>}{<span class="w">NAME</span>}<span class="cm">,</span> <span class="q">&quot;\n&quot;</span><span class="sc">;</span></li><li>    <span class="s">}</span></li><li>    <span class="i">identify_typeglob</span> <span class="i">*foo</span><span class="sc">;</span></li><li>    <span class="i">identify_typeglob</span> <span class="i">*bar::baz</span><span class="sc">;</span></li></ol></pre><p>This prints</p>
<pre class="verbatim"><ol><li>    <span class="w">You</span> <span class="w">gave</span> <span class="w">me</span> <span class="w">main::foo</span></li><li>    <span class="w">You</span> <span class="w">gave</span> <span class="w">me</span> <span class="w">bar::baz</span></li></ol></pre><p>The <code class="inline"><span class="i">*foo</span>{<span class="w">THING</span>}</code>
 notation can also be used to obtain references to the
individual elements of *foo.  See <a href="perlref.html">perlref</a>.</p>
<p>Subroutine definitions (and declarations, for that matter) need
not necessarily be situated in the package whose symbol table they
occupy.  You can define a subroutine outside its package by
explicitly qualifying the name of the subroutine:</p>
<pre class="verbatim"><ol><li><a name="package-main"></a>    package <span class="i">main</span><span class="sc">;</span></li><li><a name="Some_package::foo"></a>    sub <span class="m">Some_package::foo</span> <span class="s">{</span> ... <span class="s">}</span>   <span class="c"># &amp;foo defined in Some_package</span></li></ol></pre><p>This is just a shorthand for a typeglob assignment at compile time:</p>
<pre class="verbatim"><ol><li>    BEGIN <span class="s">{</span> <span class="i">*Some_package::foo</span> = <a class="l_k" href="functions/sub.html">sub</a> <span class="s">{</span> ... <span class="s">}</span> <span class="s">}</span></li></ol></pre><p>and is <i>not</i> the same as writing:</p>
<pre class="verbatim"><ol><li>    <span class="s">{</span></li><li><a name="package-Some_package"></a>	package <span class="i">Some_package</span><span class="sc">;</span></li><li><a name="foo"></a>	sub <span class="m">foo</span> <span class="s">{</span> ... <span class="s">}</span></li><li><a name="package-main"></a>    <span class="s">}</span></li></ol></pre><p>In the first two versions, the body of the subroutine is
lexically in the main package, <i>not</i> in Some_package. So
something like this:</p>
<pre class="verbatim"><ol><li><a name="package-main"></a>    package <span class="i">main</span><span class="sc">;</span></li><li></li><li>    <span class="i">$Some_package::name</span> = <span class="q">&quot;fred&quot;</span><span class="sc">;</span></li><li>    <span class="i">$main::name</span> = <span class="q">&quot;barney&quot;</span><span class="sc">;</span></li><li></li><li><a name="Some_package::foo"></a>    sub <span class="m">Some_package::foo</span> <span class="s">{</span></li><li>	<a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;in &quot;</span><span class="cm">,</span> <span class="w">__PACKAGE__</span><span class="cm">,</span> <span class="q">&quot;: \$name is &#39;$name&#39;\n&quot;</span><span class="sc">;</span></li><li>    <span class="s">}</span></li><li></li><li>    <span class="i">Some_package::foo</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>prints:</p>
<pre class="verbatim"><ol><li>    in main: $name is 'barney'</li></ol></pre><p>rather than:</p>
<pre class="verbatim"><ol><li>    in Some_package: $name is 'fred'</li></ol></pre><p>This also has implications for the use of the SUPER:: qualifier
(see <a href="perlobj.html">perlobj</a>).</p>
<a name="BEGIN%2c-UNITCHECK%2c-CHECK%2c-INIT-and-END"></a><h2>BEGIN, UNITCHECK, CHECK, INIT and END
    </h2>
<p>Five specially named code blocks are executed at the beginning and at
the end of a running Perl program.  These are the <code class="inline">BEGIN</code>
,
<code class="inline">UNITCHECK</code>
, <code class="inline">CHECK</code>
, <code class="inline">INIT</code>
, and <code class="inline">END</code>
 blocks.</p>
<p>These code blocks can be prefixed with <code class="inline"><a class="l_k" href="functions/sub.html">sub</a></code> to give the appearance of a
subroutine (although this is not considered good style).  One should note
that these code blocks don't really exist as named subroutines (despite
their appearance). The thing that gives this away is the fact that you can
have <b>more than one</b> of these code blocks in a program, and they will get
<b>all</b> executed at the appropriate moment.  So you can't execute any of
these code blocks by name.</p>
<p>A <code class="inline">BEGIN</code>
 code block is executed as soon as possible, that is, the moment
it is completely defined, even before the rest of the containing file (or
string) is parsed.  You may have multiple <code class="inline">BEGIN</code>
 blocks within a file (or
eval'ed string); they will execute in order of definition.  Because a <code class="inline">BEGIN</code>

code block executes immediately, it can pull in definitions of subroutines
and such from other files in time to be visible to the rest of the compile
and run time.  Once a <code class="inline">BEGIN</code>
 has run, it is immediately undefined and any
code it used is returned to Perl's memory pool.</p>
<p>An <code class="inline">END</code>
 code block is executed as late as possible, that is, after
perl has finished running the program and just before the interpreter
is being exited, even if it is exiting as a result of a die() function.
(But not if it's morphing into another program via <code class="inline"><a class="l_k" href="functions/exec.html">exec</a></code>, or
being blown out of the water by a signal--you have to trap that yourself
(if you can).)  You may have multiple <code class="inline">END</code>
 blocks within a file--they
will execute in reverse order of definition; that is: last in, first
out (LIFO).  <code class="inline">END</code>
 blocks are not executed when you run perl with the
<code class="inline">-c</code>
 switch, or if compilation fails.</p>
<p>Note that <code class="inline">END</code>
 code blocks are <b>not</b> executed at the end of a string
<code class="inline"><a class="l_k" href="functions/eval.html">eval()</a></code>: if any <code class="inline">END</code>
 code blocks are created in a string <code class="inline"><a class="l_k" href="functions/eval.html">eval()</a></code>,
they will be executed just as any other <code class="inline">END</code>
 code block of that package
in LIFO order just before the interpreter is being exited.</p>
<p>Inside an <code class="inline">END</code>
 code block, <code class="inline"><span class="i">$?</span></code>
 contains the value that the program is
going to pass to <code class="inline"><a class="l_k" href="functions/exit.html">exit()</a></code>.  You can modify <code class="inline"><span class="i">$?</span></code>
 to change the exit
value of the program.  Beware of changing <code class="inline"><span class="i">$?</span></code>
 by accident (e.g. by
running something via <code class="inline"><a class="l_k" href="functions/system.html">system</a></code>).
</p>
<p>Inside of a <code class="inline">END</code>
 block, the value of <code class="inline"><span class="i">$</span>{<span class="w">^GLOBAL_PHASE</span>}</code>
 will be
<code class="inline"><span class="q">&quot;END&quot;</span></code>
.</p>
<p><code class="inline">UNITCHECK</code>
, <code class="inline">CHECK</code>
 and <code class="inline">INIT</code>
 code blocks are useful to catch the
transition between the compilation phase and the execution phase of
the main program.</p>
<p><code class="inline">UNITCHECK</code>
 blocks are run just after the unit which defined them has
been compiled.  The main program file and each module it loads are
compilation units, as are string <code class="inline"><a class="l_k" href="functions/eval.html">eval</a></code>s, run-time code compiled using the
<code class="inline">(?{ })</code> construct in a regex, calls to <code class="inline"><a class="l_k" href="functions/do.html">do</a> <span class="w">FILE</span></code>
, <code class="inline"><a class="l_k" href="functions/require.html">require</a> <span class="w">FILE</span></code>
,
and code after the <code class="inline">-e</code>
 switch on the command line.</p>
<p><code class="inline">BEGIN</code>
 and <code class="inline">UNITCHECK</code>
 blocks are not directly related to the phase of
the interpreter.  They can be created and executed during any phase.</p>
<p><code class="inline">CHECK</code>
 code blocks are run just after the <b>initial</b> Perl compile phase ends
and before the run time begins, in LIFO order.  <code class="inline">CHECK</code>
 code blocks are used
in the Perl compiler suite to save the compiled state of the program.</p>
<p>Inside of a <code class="inline">CHECK</code>
 block, the value of <code class="inline"><span class="i">$</span>{<span class="w">^GLOBAL_PHASE</span>}</code>
 will be
<code class="inline"><span class="q">&quot;CHECK&quot;</span></code>
.</p>
<p><code class="inline">INIT</code>
 blocks are run just before the Perl runtime begins execution, in
"first in, first out" (FIFO) order.</p>
<p>Inside of an <code class="inline">INIT</code>
 block, the value of <code class="inline"><span class="i">$</span>{<span class="w">^GLOBAL_PHASE</span>}</code>
 will be <code class="inline"><span class="q">&quot;INIT&quot;</span></code>
.</p>
<p>The <code class="inline">CHECK</code>
 and <code class="inline">INIT</code>
 blocks in code compiled by <code class="inline"><a class="l_k" href="functions/require.html">require</a></code>, string <code class="inline"><a class="l_k" href="functions/do.html">do</a></code>,
or string <code class="inline"><a class="l_k" href="functions/eval.html">eval</a></code> will not be executed if they occur after the end of the
main compilation phase; that can be a problem in mod_perl and other persistent
environments which use those functions to load code at runtime.</p>
<p>When you use the <b>-n</b> and <b>-p</b> switches to Perl, <code class="inline">BEGIN</code>
 and
<code class="inline">END</code>
 work just as they do in <b>awk</b>, as a degenerate case.
Both <code class="inline">BEGIN</code>
 and <code class="inline">CHECK</code>
 blocks are run when you use the <b>-c</b>
switch for a compile-only syntax check, although your main code
is not.</p>
<p>The <b>begincheck</b> program makes it all clear, eventually:</p>
<pre class="verbatim"><ol><li>  <span class="c">#!/usr/bin/perl</span></li><li></li><li>  <span class="c"># begincheck</span></li><li></li><li>  <a class="l_k" href="functions/print.html">print</a>         <span class="q">&quot;10. Ordinary code runs at runtime.\n&quot;</span><span class="sc">;</span></li><li></li><li>  <a class="l_k" href="functions/END.html">END</a> <span class="s">{</span> <a class="l_k" href="functions/print.html">print</a>   <span class="q">&quot;16.   So this is the end of the tale.\n&quot;</span> <span class="s">}</span></li><li>  <a class="l_k" href="functions/INIT.html">INIT</a> <span class="s">{</span> <a class="l_k" href="functions/print.html">print</a>  <span class="q">&quot; 7. INIT blocks run FIFO just before runtime.\n&quot;</span> <span class="s">}</span></li><li>  <a class="l_k" href="functions/UNITCHECK.html">UNITCHECK</a> <span class="s">{</span></li><li>    <a class="l_k" href="functions/print.html">print</a>       <span class="q">&quot; 4.   And therefore before any CHECK blocks.\n&quot;</span></li><li>  <span class="s">}</span></li><li>  <a class="l_k" href="functions/CHECK.html">CHECK</a> <span class="s">{</span> <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot; 6.   So this is the sixth line.\n&quot;</span> <span class="s">}</span></li><li></li><li>  <a class="l_k" href="functions/print.html">print</a>         <span class="q">&quot;11.   It runs in order, of course.\n&quot;</span><span class="sc">;</span></li><li></li><li>  <a class="l_k" href="functions/BEGIN.html">BEGIN</a> <span class="s">{</span> <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot; 1. BEGIN blocks run FIFO during compilation.\n&quot;</span> <span class="s">}</span></li><li>  <a class="l_k" href="functions/END.html">END</a> <span class="s">{</span> <a class="l_k" href="functions/print.html">print</a>   <span class="q">&quot;15.   Read perlmod for the rest of the story.\n&quot;</span> <span class="s">}</span></li><li>  <a class="l_k" href="functions/CHECK.html">CHECK</a> <span class="s">{</span> <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot; 5. CHECK blocks run LIFO after all compilation.\n&quot;</span> <span class="s">}</span></li><li>  <a class="l_k" href="functions/INIT.html">INIT</a> <span class="s">{</span> <a class="l_k" href="functions/print.html">print</a>  <span class="q">&quot; 8.   Run this again, using Perl&#39;s -c switch.\n&quot;</span> <span class="s">}</span></li><li></li><li>  <a class="l_k" href="functions/print.html">print</a>         <span class="q">&quot;12.   This is anti-obfuscated code.\n&quot;</span><span class="sc">;</span></li><li></li><li>  <a class="l_k" href="functions/END.html">END</a> <span class="s">{</span> <a class="l_k" href="functions/print.html">print</a>   <span class="q">&quot;14. END blocks run LIFO at quitting time.\n&quot;</span> <span class="s">}</span></li><li>  <a class="l_k" href="functions/BEGIN.html">BEGIN</a> <span class="s">{</span> <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot; 2.   So this line comes out second.\n&quot;</span> <span class="s">}</span></li><li>  <a class="l_k" href="functions/UNITCHECK.html">UNITCHECK</a> <span class="s">{</span></li><li>   <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot; 3. UNITCHECK blocks run LIFO after each file is compiled.\n&quot;</span></li><li>  <span class="s">}</span></li><li>  <a class="l_k" href="functions/INIT.html">INIT</a> <span class="s">{</span> <a class="l_k" href="functions/print.html">print</a>  <span class="q">&quot; 9.   You&#39;ll see the difference right away.\n&quot;</span> <span class="s">}</span></li><li></li><li>  <a class="l_k" href="functions/print.html">print</a>         <span class="q">&quot;13.   It only _looks_ like it should be confusing.\n&quot;</span><span class="sc">;</span></li><li></li><li><a name="__END__"></a>  __END__</li></ol></pre><a name="Perl-Classes"></a><h2>Perl Classes
 </h2>
<p>There is no special class syntax in Perl, but a package may act
as a class if it provides subroutines to act as methods.  Such a
package may also derive some of its methods from another class (package)
by listing the other package name(s) in its global @ISA array (which
must be a package global, not a lexical).</p>
<p>For more on this, see <a href="perlootut.html">perlootut</a> and <a href="perlobj.html">perlobj</a>.</p>
<a name="Perl-Modules"></a><h2>Perl Modules
</h2>
<p>A module is just a set of related functions in a library file, i.e.,
a Perl package with the same name as the file.  It is specifically
designed to be reusable by other modules or programs.  It may do this
by providing a mechanism for exporting some of its symbols into the
symbol table of any package using it, or it may function as a class
definition and make its semantics available implicitly through
method calls on the class and its objects, without explicitly
exporting anything.  Or it can do a little of both.</p>
<p>For example, to start a traditional, non-OO module called Some::Module,
create a file called <i>Some/Module.pm</i> and start with this template:</p>
<pre class="verbatim"><ol><li><a name="package-Some::Module"></a>    package <span class="i">Some::Module</span><span class="sc">;</span>  <span class="c"># assumes Some/Module.pm</span></li><li></li><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">strict</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">warnings</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="functions/BEGIN.html">BEGIN</a> <span class="s">{</span></li><li>        <a class="l_k" href="functions/require.html">require</a> <span class="w">Exporter</span><span class="sc">;</span></li><li></li><li>        <span class="c"># set the version for version checking</span></li><li>        <a class="l_k" href="functions/our.html">our</a> <span class="i">$VERSION</span>     = <span class="n">1.00</span><span class="sc">;</span></li><li></li><li>        <span class="c"># Inherit from Exporter to export functions and variables</span></li><li>        <a class="l_k" href="functions/our.html">our</a> <span class="i">@ISA</span>         = <span class="q">qw(Exporter)</span><span class="sc">;</span></li><li></li><li>        <span class="c"># Functions and variables which are exported by default</span></li><li>        <a class="l_k" href="functions/our.html">our</a> <span class="i">@EXPORT</span>      = <span class="q">qw(func1 func2)</span><span class="sc">;</span></li><li></li><li>        <span class="c"># Functions and variables which can be optionally exported</span></li><li>        <a class="l_k" href="functions/our.html">our</a> <span class="i">@EXPORT_OK</span>   = <span class="q">qw($Var1 %Hashit func3)</span><span class="sc">;</span></li><li>    <span class="s">}</span></li><li></li><li>    <span class="c"># exported package globals go here</span></li><li>    <a class="l_k" href="functions/our.html">our</a> <span class="i">$Var1</span>    = <span class="q">&#39;&#39;</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/our.html">our</a> <span class="i">%Hashit</span>  = <span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>    <span class="c"># non-exported package globals go here</span></li><li>    <span class="c"># (they are still accessible as $Some::Module::stuff)</span></li><li>    <a class="l_k" href="functions/our.html">our</a> <span class="i">@more</span>    = <span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/our.html">our</a> <span class="i">$stuff</span>   = <span class="q">&#39;&#39;</span><span class="sc">;</span></li><li></li><li>    <span class="c"># file-private lexicals go here, before any functions which use them</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$priv_var</span>    = <span class="q">&#39;&#39;</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">%secret_hash</span> = <span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>    <span class="c"># here&#39;s a file-private function as a closure,</span></li><li>    <span class="c"># callable as $priv_func-&gt;();</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$priv_func</span> = <a class="l_k" href="functions/sub.html">sub</a> <span class="s">{</span></li><li>        ...</li><li>    <span class="s">}</span><span class="sc">;</span></li><li></li><li>    <span class="c"># make all your functions, whether exported or not;</span></li><li>    <span class="c"># remember to put something interesting in the {} stubs</span></li><li><a name="func1"></a>    sub <span class="m">func1</span>      <span class="s">{</span> ... <span class="s">}</span></li><li><a name="func2"></a>    sub <span class="m">func2</span>      <span class="s">{</span> ... <span class="s">}</span></li><li></li><li>    <span class="c"># this one isn&#39;t exported, but could be called directly</span></li><li>    <span class="c"># as Some::Module::func3()</span></li><li><a name="func3"></a>    sub <span class="m">func3</span>      <span class="s">{</span> ... <span class="s">}</span></li><li></li><li>    <a class="l_k" href="functions/END.html">END</a> <span class="s">{</span> ... <span class="s">}</span>       <span class="c"># module clean-up code here (global destructor)</span></li><li></li><li>    <span class="n">1</span><span class="sc">;</span>  <span class="c"># don&#39;t forget to return a true value from the file</span></li></ol></pre><p>Then go on to declare and use your variables in functions without
any qualifications.  See <a href="Exporter.html">Exporter</a> and the <a href="perlmodlib.html">perlmodlib</a> for
details on mechanics and style issues in module creation.</p>
<p>Perl modules are included into your program by saying</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">Module</span><span class="sc">;</span></li></ol></pre><p>or</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">Module</span> <span class="w">LIST</span><span class="sc">;</span></li></ol></pre><p>This is exactly equivalent to</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/BEGIN.html">BEGIN</a> <span class="s">{</span> <a class="l_k" href="functions/require.html">require</a> <span class="q">&#39;Module.pm&#39;</span><span class="sc">;</span> <span class="q">&#39;Module&#39;</span><span class="i">-&gt;import</span><span class="sc">;</span> <span class="s">}</span></li></ol></pre><p>or</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/BEGIN.html">BEGIN</a> <span class="s">{</span> <a class="l_k" href="functions/require.html">require</a> <span class="q">&#39;Module.pm&#39;</span><span class="sc">;</span> <span class="q">&#39;Module&#39;</span><span class="i">-&gt;import</span><span class="s">(</span> <span class="w">LIST</span> <span class="s">)</span><span class="sc">;</span> <span class="s">}</span></li></ol></pre><p>As a special case</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">Module</span> <span class="s">(</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>is exactly equivalent to</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/BEGIN.html">BEGIN</a> <span class="s">{</span> <a class="l_k" href="functions/require.html">require</a> <span class="q">&#39;Module.pm&#39;</span><span class="sc">;</span> <span class="s">}</span></li></ol></pre><p>All Perl module files have the extension <i>.pm</i>.  The <code class="inline"><a class="l_k" href="functions/use.html">use</a></code> operator
assumes this so you don't have to spell out "<i>Module.pm</i>" in quotes.
This also helps to differentiate new modules from old <i>.pl</i> and
<i>.ph</i> files.  Module names are also capitalized unless they're
functioning as pragmas; pragmas are in effect compiler directives,
and are sometimes called "pragmatic modules" (or even "pragmata"
if you're a classicist).</p>
<p>The two statements:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/require.html">require</a> <span class="w">SomeModule</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/require.html">require</a> <span class="q">&quot;SomeModule.pm&quot;</span><span class="sc">;</span></li></ol></pre><p>differ from each other in two ways.  In the first case, any double
colons in the module name, such as <code class="inline"><span class="w">Some::Module</span></code>
, are translated
into your system's directory separator, usually "/".   The second
case does not, and would have to be specified literally.  The other
difference is that seeing the first <code class="inline"><a class="l_k" href="functions/require.html">require</a></code> clues in the compiler
that uses of indirect object notation involving "SomeModule", as
in <code class="inline"><span class="i">$ob</span> = <span class="w">purge</span> <span class="w">SomeModule</span></code>
, are method calls, not function calls.
(Yes, this really can make a difference.)</p>
<p>Because the <code class="inline"><a class="l_k" href="functions/use.html">use</a></code> statement implies a <code class="inline">BEGIN</code>
 block, the importing
of semantics happens as soon as the <code class="inline"><a class="l_k" href="functions/use.html">use</a></code> statement is compiled,
before the rest of the file is compiled.  This is how it is able
to function as a pragma mechanism, and also how modules are able to
declare subroutines that are then visible as list or unary operators for
the rest of the current file.  This will not work if you use <code class="inline"><a class="l_k" href="functions/require.html">require</a></code>
instead of <code class="inline"><a class="l_k" href="functions/use.html">use</a></code>.  With <code class="inline"><a class="l_k" href="functions/require.html">require</a></code> you can get into this problem:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/require.html">require</a> <span class="w">Cwd</span><span class="sc">;</span>		<span class="c"># make Cwd:: accessible</span></li><li>    <span class="i">$here</span> = <span class="i">Cwd::getcwd</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">Cwd</span><span class="sc">;</span>			<span class="c"># import names from Cwd::</span></li><li>    <span class="i">$here</span> = <span class="i">getcwd</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="functions/require.html">require</a> <span class="w">Cwd</span><span class="sc">;</span>	    	<span class="c"># make Cwd:: accessible</span></li><li>    <span class="i">$here</span> = <span class="i">getcwd</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span> 		<span class="c"># oops! no main::getcwd()</span></li></ol></pre><p>In general, <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">Module</span> <span class="s">(</span><span class="s">)</span></code>
 is recommended over <code class="inline"><a class="l_k" href="functions/require.html">require</a> <span class="w">Module</span></code>
,
because it determines module availability at compile time, not in the
middle of your program's execution.  An exception would be if two modules
each tried to <code class="inline"><a class="l_k" href="functions/use.html">use</a></code> each other, and each also called a function from
that other module.  In that case, it's easy to use <code class="inline"><a class="l_k" href="functions/require.html">require</a></code> instead.</p>
<p>Perl packages may be nested inside other package names, so we can have
package names containing <code class="inline"><span class="w">::</span></code>
.  But if we used that package name
directly as a filename it would make for unwieldy or impossible
filenames on some systems.  Therefore, if a module's name is, say,
<code class="inline"><span class="w">Text::Soundex</span></code>
, then its definition is actually found in the library
file <i>Text/Soundex.pm</i>.</p>
<p>Perl modules always have a <i>.pm</i> file, but there may also be
dynamically linked executables (often ending in <i>.so</i>) or autoloaded
subroutine definitions (often ending in <i>.al</i>) associated with the
module.  If so, these will be entirely transparent to the user of
the module.  It is the responsibility of the <i>.pm</i> file to load
(or arrange to autoload) any additional functionality.  For example,
although the POSIX module happens to do both dynamic loading and
autoloading, the user can say just <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">POSIX</span></code>
 to get it all.</p>
<a name="Making-your-module-threadsafe"></a><h2>Making your module threadsafe
 
 
    </h2>
<p>Perl supports a type of threads called interpreter threads (ithreads).
These threads can be used explicitly and implicitly.</p>
<p>Ithreads work by cloning the data tree so that no data is shared
between different threads. These threads can be used by using the <code class="inline"><span class="w">threads</span></code>

module or by doing fork() on win32 (fake fork() support). When a
thread is cloned all Perl data is cloned, however non-Perl data cannot
be cloned automatically.  Perl after 5.8.0 has support for the <code class="inline"><span class="w">CLONE</span></code>

special subroutine.  In <code class="inline"><span class="w">CLONE</span></code>
 you can do whatever
you need to do,
like for example handle the cloning of non-Perl data, if necessary.
<code class="inline"><span class="w">CLONE</span></code>
 will be called once as a class method for every package that has it
defined (or inherits it).  It will be called in the context of the new thread,
so all modifications are made in the new area.  Currently CLONE is called with
no parameters other than the invocant package name, but code should not assume
that this will remain unchanged, as it is likely that in future extra parameters
will be passed in to give more information about the state of cloning.</p>
<p>If you want to CLONE all objects you will need to keep track of them per
package. This is simply done using a hash and Scalar::Util::weaken().</p>
<p>Perl after 5.8.7 has support for the <code class="inline"><span class="w">CLONE_SKIP</span></code>
 special subroutine.
Like <code class="inline"><span class="w">CLONE</span></code>
, <code class="inline"><span class="w">CLONE_SKIP</span></code>
 is called once per package; however, it is
called just before cloning starts, and in the context of the parent
thread. If it returns a true value, then no objects of that class will
be cloned; or rather, they will be copied as unblessed, undef values.
For example: if in the parent there are two references to a single blessed
hash, then in the child there will be two references to a single undefined
scalar value instead.
This provides a simple mechanism for making a module threadsafe; just add
<code class="inline"><a name="CLONE_SKIP"></a>sub <span class="m">CLONE_SKIP</span> <span class="s">{</span> <span class="n">1</span> <span class="s">}</span></code>
 at the top of the class, and <code class="inline">DESTROY<span class="s">(</span><span class="s">)</span></code>
 will
now only be called once per object. Of course, if the child thread needs
to make use of the objects, then a more sophisticated approach is
needed.</p>
<p>Like <code class="inline"><span class="w">CLONE</span></code>
, <code class="inline"><span class="w">CLONE_SKIP</span></code>
 is currently called with no parameters other
than the invocant package name, although that may change. Similarly, to
allow for future expansion, the return value should be a single <code class="inline"><span class="n">0</span></code>
 or
<code class="inline"><span class="n">1</span></code>
 value.</p>
<a name="SEE-ALSO"></a><h1>SEE ALSO</h1>
<p>See <a href="perlmodlib.html">perlmodlib</a> for general style issues related to building Perl
modules and classes, as well as descriptions of the standard library
and CPAN, <a href="Exporter.html">Exporter</a> for how Perl's standard import/export mechanism
works, <a href="perlootut.html">perlootut</a> and <a href="perlobj.html">perlobj</a> for in-depth information on
creating classes, <a href="perlobj.html">perlobj</a> for a hard-core reference document on
objects, <a href="perlsub.html">perlsub</a> for an explanation of functions and scoping,
and <a href="perlxstut.html">perlxstut</a> and <a href="perlguts.html">perlguts</a> for more information on writing
extension modules.</p>




  <div id="page_index" class="hud_container">
    <div id="page_index_header" class="hud_header">
      <div id="page_index_close" class="hud_close"><a href="#" onClick="pageIndex.hide();return false;"></a></div>
      <div id="page_index_title" class="hud_title"><span class="hud_span_top">Page index</span></div>
      <div id="page_index_topright" class="hud_topright"></div>
    </div>
    <div id="page_index_content" class="hud_content">
      <ul><li><a href="#NAME">NAME</a><li><a href="#DESCRIPTION">DESCRIPTION</a><ul><li><a href="#Is-this-the-document-you-were-after%3f">Is this the document you were after?</a><li><a href="#Packages">Packages
    </a><li><a href="#Symbol-Tables">Symbol Tables
      </a><li><a href="#BEGIN%2c-UNITCHECK%2c-CHECK%2c-INIT-and-END">BEGIN, UNITCHECK, CHECK, INIT and END
    </a><li><a href="#Perl-Classes">Perl Classes
 </a><li><a href="#Perl-Modules">Perl Modules
</a><li><a href="#Making-your-module-threadsafe">Making your module threadsafe
 
 
    </a></ul><li><a href="#SEE-ALSO">SEE ALSO</a></ul>
    </div>
    <div id="page_index_footer" class="hud_footer">
      <div id="page_index_bottomleft" class="hud_bottomleft"></div>
      <div id="page_index_bottom" class="hud_bottom"><span class="hud_span_bottom"></span></div>
      <div id="page_index_resize" class="hud_resize"></div>
    </div>
  </div>


	    &nbsp;
          </div>
          <div id="content_footer">
          </div>
        </div>
        <div class="clear"></div>
      </div>
      
    <div id="footer">
      <div id="footer_content">
        <div id="footer_strapline">
          perldoc.perl.org - Official documentation for the Perl programming language
        </div>
        <div id="footer_links">
          <div id="address">
            <p class="name">Contact details</p>
            <p class="address">
	      Site maintained by <a href="mailto:jj@jonallen.info">Jon Allen (JJ)</a><br>
	    </p>
            <p class="contact">
              Documentation maintained by the <a href="http://lists.cpan.org/showlist.cgi?name=perl5-porters">Perl 5 Porters</a>
            </p>
          </div>
          <ul class="f1">
            <li>Manual
              <ul class="f2">
                <li><a href="index-overview.html">Overview</a>
                <li><a href="index-tutorials.html">Tutorials</a>
                <li><a href="index-faq.html">FAQs</a>
                <li><a href="index-history.html">Changes</a>
              </ul>
            <li>Reference
              <ul class="f2">
                <li><a href="index-language.html">Language</a>
                <li><a href="index-functions.html">Functions</a>
                <li><a href="perlop.html">Operators</a>
                <li><a href="perlvar.html">Variables</a>
              </ul>
            <li>Modules
              <ul class="f2">
                <li><a href="index-modules-A.html">Modules</a>
                <li><a href="index-pragmas.html">Pragmas</a>
                <li><a href="index-utilities.html">Utilities</a>
              </ul>
            <li>Misc
              <ul class="f2">
                <li><a href="index-licence.html">License</a>
                <li><a href="index-internals.html">Internals</a>
                <li><a href="index-platforms.html">Platforms</a>
              </ul>          </ul>
          <div class="clear"></div>
        </div>
      </div>
      <div id="footer_end">
      </div>
    </div>
      
    </div>
      <script language="JavaScript" type="text/javascript" src="static/exploreperl.js"></script>
      <script language="JavaScript" src="static/combined-20100403.js" type="text/javascript"></script>
<script language="JavaScript" type="text/javascript">
  perldoc.setPath(0);
  perldoc.pageName    = 'perlmod';
  perldoc.pageAddress = 'perlmod.html';
  perldoc.contentPage = 1;
  explorePerl.render();
  explorePerl.addEvents('explore_anchor');
</script>
    
  </body>
</html>