File: perlfaq7.html

package info (click to toggle)
perl-doc-html 5.14.0-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 38,588 kB
  • sloc: xml: 36; makefile: 10
file content (1058 lines) | stat: -rw-r--r-- 101,296 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
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <title>perlfaq7 - 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>perlfaq7</h1>
              </div>
              <div id="perl_version">
                Perl 5 version 14.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-faq.html">FAQs</a> &gt;
      
    
    perlfaq7
  

            </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>perlfaq7</h1>


  <!--    -->
<ul><li><a href="#NAME">NAME</a><li><a href="#DESCRIPTION">DESCRIPTION</a><ul><li><a href="#Can-I-get-a-BNF%2fyacc%2fRE-for-the-Perl-language%3f">Can I get a BNF/yacc/RE for the Perl language?</a><li><a href="#What-are-all-these-%24%40%25%26*-punctuation-signs%2c-and-how-do-I-know-when-to-use-them%3f">What are all these $@%&* punctuation signs, and how do I know when to use them?</a><li><a href="#Do-I-always%2fnever-have-to-quote-my-strings-or-use-semicolons-and-commas%3f">Do I always/never have to quote my strings or use semicolons and commas?</a><li><a href="#How-do-I-skip-some-return-values%3f">How do I skip some return values?</a><li><a href="#How-do-I-temporarily-block-warnings%3f">How do I temporarily block warnings?</a><li><a href="#What's-an-extension%3f">What's an extension?</a><li><a href="#Why-do-Perl-operators-have-different-precedence-than-C-operators%3f">Why do Perl operators have different precedence than C operators?</a><li><a href="#How-do-I-declare%2fcreate-a-structure%3f">How do I declare/create a structure?</a><li><a href="#How-do-I-create-a-module%3f">How do I create a module?</a><li><a href="#How-do-I-adopt-or-take-over-a-module-already-on-CPAN%3f">How do I adopt or take over a module already on CPAN?</a><li><a href="#How-do-I-create-a-class%3f">How do I create a class?
 </a><li><a href="#How-can-I-tell-if-a-variable-is-tainted%3f">How can I tell if a variable is tainted?</a><li><a href="#What's-a-closure%3f">What's a closure?</a><li><a href="#What-is-variable-suicide-and-how-can-I-prevent-it%3f">What is variable suicide and how can I prevent it?</a><li><a href="#How-can-I-pass%2freturn-a-%7bFunction%2c-FileHandle%2c-Array%2c-Hash%2c-Method%2c-Regex%7d%3f">How can I pass/return a {Function, FileHandle, Array, Hash, Method, Regex}?</a><li><a href="#How-do-I-create-a-static-variable%3f">How do I create a static variable?</a><li><a href="#What's-the-difference-between-dynamic-and-lexical-(static)-scoping%3f--Between-local()-and-my()%3f">What's the difference between dynamic and lexical (static) scoping?  Between local() and my()?</a><li><a href="#How-can-I-access-a-dynamic-variable-while-a-similarly-named-lexical-is-in-scope%3f">How can I access a dynamic variable while a similarly named lexical is in scope?</a><li><a href="#What's-the-difference-between-deep-and-shallow-binding%3f">What's the difference between deep and shallow binding?</a><li><a href="#Why-doesn't-%22my(%24foo)-%3d-%3cFILE%3e%3b%22-work-right%3f">Why doesn't "my($foo) = <FILE>;" work right?</a><li><a href="#How-do-I-redefine-a-builtin-function%2c-operator%2c-or-method%3f">How do I redefine a builtin function, operator, or method?</a><li><a href="#What's-the-difference-between-calling-a-function-as-%26foo-and-foo()%3f">What's the difference between calling a function as &foo and foo()?</a><li><a href="#How-do-I-create-a-switch-or-case-statement%3f">How do I create a switch or case statement?</a><li><a href="#How-can-I-catch-accesses-to-undefined-variables%2c-functions%2c-or-methods%3f">How can I catch accesses to undefined variables, functions, or methods?</a><li><a href="#Why-can't-a-method-included-in-this-same-file-be-found%3f">Why can't a method included in this same file be found?</a><li><a href="#How-can-I-find-out-my-current-or-calling-package%3f">How can I find out my current or calling package?</a><li><a href="#How-can-I-comment-out-a-large-block-of-Perl-code%3f">How can I comment out a large block of Perl code?</a><li><a href="#How-do-I-clear-a-package%3f">How do I clear a package?</a><li><a href="#How-can-I-use-a-variable-as-a-variable-name%3f">How can I use a variable as a variable name?</a><li><a href="#What-does-%22bad-interpreter%22-mean%3f">What does "bad interpreter" mean?</a></ul><li><a href="#AUTHOR-AND-COPYRIGHT">AUTHOR AND COPYRIGHT</a></ul><a name="NAME"></a><h1>NAME</h1>
<p>perlfaq7 - General Perl Language Issues</p>
<a name="DESCRIPTION"></a><h1>DESCRIPTION</h1>
<p>This section deals with general Perl language issues that don't
clearly fit into any of the other sections.</p>
<a name="Can-I-get-a-BNF%2fyacc%2fRE-for-the-Perl-language%3f"></a><h2>Can I get a BNF/yacc/RE for the Perl language?</h2>
<p>There is no BNF, but you can paw your way through the yacc grammar in
perly.y in the source distribution if you're particularly brave.  The
grammar relies on very smart tokenizing code, so be prepared to
venture into toke.c as well.</p>
<p>In the words of Chaim Frenkel: "Perl's grammar can not be reduced to BNF.
The work of parsing perl is distributed between yacc, the lexer, smoke
and mirrors."</p>
<a name="What-are-all-these-%24%40%25%26*-punctuation-signs%2c-and-how-do-I-know-when-to-use-them%3f"></a><h2>What are all these $@%&amp;* punctuation signs, and how do I know when to use them?</h2>
<p>They are type specifiers, as detailed in <a href="perldata.html">perldata</a>:</p>
<pre class="verbatim"><ol><li>	$ for scalar values (number, string or reference)</li><li>	@ for arrays</li><li>	% for hashes (associative arrays)</li><li>	&amp; for subroutines (aka functions, procedures, methods)</li><li>	* for all types of that symbol name.  In version 4 you used them like</li><li>	  pointers, but in modern perls you can just use references.</li></ol></pre><p>There are a couple of other symbols that
you're likely to encounter that aren't
really type specifiers:</p>
<pre class="verbatim"><ol><li>	&lt;&gt; are used for inputting a record from a filehandle.</li><li>	\  takes a reference to something.</li></ol></pre><p>Note that &lt;FILE&gt; is <i>neither</i> the type specifier for files
nor the name of the handle.  It is the <code class="inline">&lt;&gt;</code>
 operator applied
to the handle FILE.  It reads one line (well, record--see
<a href="perlvar.html#%24%2f">$/ in perlvar</a>) from the handle FILE in scalar context, or <i>all</i> lines
in list context.  When performing open, close, or any other operation
besides <code class="inline">&lt;&gt;</code>
 on files, or even when talking about the handle, do
<i>not</i> use the brackets.  These are correct: <code class="inline"><a class="l_k" href="functions/eof.html">eof(FH)</a></code>, <code class="inline"><a class="l_k" href="functions/seek.html">seek</a><span class="s">(</span><span class="w">FH</span><span class="cm">,</span> <span class="n">0</span><span class="cm">,</span>
<span class="n">2</span><span class="s">)</span></code>
 and "copying from STDIN to FILE".</p>
<a name="Do-I-always%2fnever-have-to-quote-my-strings-or-use-semicolons-and-commas%3f"></a><h2>Do I always/never have to quote my strings or use semicolons and commas?</h2>
<p>Normally, a bareword doesn't need to be quoted, but in most cases
probably should be (and must be under <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">strict</span></code>
).  But a hash key
consisting of a simple word and the left-hand
operand to the <code class="inline"><span class="cm">=&gt;</span></code>
 operator both
count as though they were quoted:</p>
<pre class="verbatim"><ol><li>	This                    is like this</li><li>	------------            ---------------</li><li>	$foo{line}              $foo{'line'}</li><li>	bar =&gt; stuff            'bar' =&gt; stuff</li></ol></pre><p>The final semicolon in a block is optional, as is the final comma in a
list.  Good style (see <a href="perlstyle.html">perlstyle</a>) says to put them in except for
one-liners:</p>
<pre class="verbatim"><ol><li>	if <span class="s">(</span><span class="i">$whoops</span><span class="s">)</span> <span class="s">{</span> <a class="l_k" href="functions/exit.html">exit</a> <span class="n">1</span> <span class="s">}</span></li><li>	<span class="i">@nums</span> = <span class="s">(</span><span class="n">1</span><span class="cm">,</span> <span class="n">2</span><span class="cm">,</span> <span class="n">3</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>	if <span class="s">(</span><span class="i">$whoops</span><span class="s">)</span> <span class="s">{</span></li><li>		<a class="l_k" href="functions/exit.html">exit</a> <span class="n">1</span><span class="sc">;</span></li><li>	<span class="s">}</span></li><li></li><li>	<span class="i">@lines</span> = <span class="s">(</span></li><li>	<span class="q">&quot;There Beren came from mountains cold&quot;</span><span class="cm">,</span></li><li>	<span class="q">&quot;And lost he wandered under leaves&quot;</span><span class="cm">,</span></li><li>	<span class="s">)</span><span class="sc">;</span></li></ol></pre><a name="How-do-I-skip-some-return-values%3f"></a><h2>How do I skip some return values?</h2>
<p>One way is to treat the return values as a list and index into it:</p>
<pre class="verbatim"><ol><li>	<span class="i">$dir</span> = <span class="s">(</span><a class="l_k" href="functions/getpwnam.html">getpwnam</a><span class="s">(</span><span class="i">$user</span><span class="s">)</span><span class="s">)</span>[<span class="n">7</span>]<span class="sc">;</span></li></ol></pre><p>Another way is to use undef as an element on the left-hand-side:</p>
<pre class="verbatim"><ol><li>	<span class="s">(</span><span class="i">$dev</span><span class="cm">,</span> <span class="i">$ino</span><span class="cm">,</span> <a class="l_k" href="functions/undef.html">undef</a><span class="cm">,</span> <a class="l_k" href="functions/undef.html">undef</a><span class="cm">,</span> <span class="i">$uid</span><span class="cm">,</span> <span class="i">$gid</span><span class="s">)</span> = <a class="l_k" href="functions/stat.html">stat</a><span class="s">(</span><span class="i">$file</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>You can also use a list slice to select only the elements that
you need:</p>
<pre class="verbatim"><ol><li>	<span class="s">(</span><span class="i">$dev</span><span class="cm">,</span> <span class="i">$ino</span><span class="cm">,</span> <span class="i">$uid</span><span class="cm">,</span> <span class="i">$gid</span><span class="s">)</span> = <span class="s">(</span> <a class="l_k" href="functions/stat.html">stat</a><span class="s">(</span><span class="i">$file</span><span class="s">)</span> <span class="s">)</span>[<span class="n">0</span><span class="cm">,</span><span class="n">1</span><span class="cm">,</span><span class="n">4</span><span class="cm">,</span><span class="n">5</span>]<span class="sc">;</span></li></ol></pre><a name="How-do-I-temporarily-block-warnings%3f"></a><h2>How do I temporarily block warnings?</h2>
<p>If you are running Perl 5.6.0 or better, the <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">warnings</span></code>
 pragma
allows fine control of what warning are produced.
See <a href="perllexwarn.html">perllexwarn</a> for more details.</p>
<pre class="verbatim"><ol><li>	<span class="s">{</span></li><li>	<a class="l_k" href="functions/no.html">no</a> <span class="w">warnings</span><span class="sc">;</span>          <span class="c"># temporarily turn off warnings</span></li><li>	<span class="i">$a</span> = <span class="i">$b</span> + <span class="i">$c</span><span class="sc">;</span>         <span class="c"># I know these might be undef</span></li><li>	<span class="s">}</span></li></ol></pre><p>Additionally, you can enable and disable categories of warnings.
You turn off the categories you want to ignore and you can still
get other categories of warnings.  See <a href="perllexwarn.html">perllexwarn</a> for the
complete details, including the category names and hierarchy.</p>
<pre class="verbatim"><ol><li>	<span class="s">{</span></li><li>	<a class="l_k" href="functions/no.html">no</a> <span class="w">warnings</span> <span class="q">&#39;uninitialized&#39;</span><span class="sc">;</span></li><li>	<span class="i">$a</span> = <span class="i">$b</span> + <span class="i">$c</span><span class="sc">;</span></li><li>	<span class="s">}</span></li></ol></pre><p>If you have an older version of Perl, the <code class="inline"><span class="i">$^W</span></code>
 variable (documented
in <a href="perlvar.html">perlvar</a>) controls runtime warnings for a block:</p>
<pre class="verbatim"><ol><li>	<span class="s">{</span></li><li>	<a class="l_k" href="functions/local.html">local</a> <span class="i">$^W</span> = <span class="n">0</span><span class="sc">;</span>        <span class="c"># temporarily turn off warnings</span></li><li>	<span class="i">$a</span> = <span class="i">$b</span> + <span class="i">$c</span><span class="sc">;</span>         <span class="c"># I know these might be undef</span></li><li>	<span class="s">}</span></li></ol></pre><p>Note that like all the punctuation variables, you cannot currently
use my() on <code class="inline"><span class="i">$^W</span></code>
, only local().</p>
<a name="What's-an-extension%3f"></a><h2>What's an extension?</h2>
<p>An extension is a way of calling compiled C code from Perl.  Reading
<a href="perlxstut.html">perlxstut</a> is a good place to learn more about extensions.</p>
<a name="Why-do-Perl-operators-have-different-precedence-than-C-operators%3f"></a><h2>Why do Perl operators have different precedence than C operators?</h2>
<p>Actually, they don't.  All C operators that Perl copies have the same
precedence in Perl as they do in C.  The problem is with operators that C
doesn't have, especially functions that give a list context to everything
on their right, eg. print, chmod, exec, and so on.  Such functions are
called "list operators" and appear as such in the precedence table in
<a href="perlop.html">perlop</a>.</p>
<p>A common mistake is to write:</p>
<pre class="verbatim"><ol><li>	<a class="l_k" href="functions/unlink.html">unlink</a> <span class="i">$file</span> || <a class="l_k" href="functions/die.html">die</a> <span class="q">&quot;snafu&quot;</span><span class="sc">;</span></li></ol></pre><p>This gets interpreted as:</p>
<pre class="verbatim"><ol><li>	<a class="l_k" href="functions/unlink.html">unlink</a> <span class="s">(</span><span class="i">$file</span> || <a class="l_k" href="functions/die.html">die</a> <span class="q">&quot;snafu&quot;</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>To avoid this problem, either put in extra parentheses or use the
super low precedence <code class="inline">or</code>
 operator:</p>
<pre class="verbatim"><ol><li>	<span class="s">(</span><a class="l_k" href="functions/unlink.html">unlink</a> <span class="i">$file</span><span class="s">)</span> || <a class="l_k" href="functions/die.html">die</a> <span class="q">&quot;snafu&quot;</span><span class="sc">;</span></li><li>	<a class="l_k" href="functions/unlink.html">unlink</a> <span class="i">$file</span> or <a class="l_k" href="functions/die.html">die</a> <span class="q">&quot;snafu&quot;</span><span class="sc">;</span></li></ol></pre><p>The "English" operators (<code class="inline">and</code>
, <code class="inline">or</code>
, <code class="inline">xor</code>
, and <code class="inline">not</code>
)
deliberately have precedence lower than that of list operators for
just such situations as the one above.</p>
<p>Another operator with surprising precedence is exponentiation.  It
binds more tightly even than unary minus, making <code class="inline"><span class="n">-2</span>**<span class="n">2</span></code>
 produce a
negative not a positive four.  It is also right-associating, meaning
that <code class="inline"><span class="n">2</span>**<span class="n">3</span>**<span class="n">2</span></code>
 is two raised to the ninth power, not eight squared.</p>
<p>Although it has the same precedence as in C, Perl's <code class="inline">?:</code> operator
produces an lvalue.  This assigns $x to either $a or $b, depending
on the trueness of $maybe:</p>
<pre class="verbatim"><ol><li>	<span class="s">(</span><span class="i">$maybe</span> ? <span class="i">$a</span> <span class="co">:</span> <span class="i">$b</span><span class="s">)</span> = <span class="i">$x</span><span class="sc">;</span></li></ol></pre><a name="How-do-I-declare%2fcreate-a-structure%3f"></a><h2>How do I declare/create a structure?</h2>
<p>In general, you don't "declare" a structure.  Just use a (probably
anonymous) hash reference.  See <a href="perlref.html">perlref</a> and <a href="perldsc.html">perldsc</a> for details.
Here's an example:</p>
<pre class="verbatim"><ol><li>	<span class="i">$person</span> = <span class="s">{</span><span class="s">}</span><span class="sc">;</span>                   <span class="c"># new anonymous hash</span></li><li>	<span class="i">$person</span>-&gt;{<span class="w">AGE</span>}  = <span class="n">24</span><span class="sc">;</span>           <span class="c"># set field AGE to 24</span></li><li>	<span class="i">$person</span>-&gt;{<span class="w">NAME</span>} = <span class="q">&quot;Nat&quot;</span><span class="sc">;</span>        <span class="c"># set field NAME to &quot;Nat&quot;</span></li></ol></pre><p>If you're looking for something a bit more rigorous, try <a href="perltoot.html">perltoot</a>.</p>
<a name="How-do-I-create-a-module%3f"></a><h2>How do I create a module?</h2>
<p>(contributed by brian d foy)</p>
<p><a href="perlmod.html">perlmod</a>, <a href="perlmodlib.html">perlmodlib</a>, <a href="perlmodstyle.html">perlmodstyle</a> explain modules
in all the gory details. <a href="perlnewmod.html">perlnewmod</a> gives a brief
overview of the process along with a couple of suggestions
about style.</p>
<p>If you need to include C code or C library interfaces in
your module, you'll need h2xs.  h2xs will create the module
distribution structure and the initial interface files
you'll need.  <a href="perlxs.html">perlxs</a> and <a href="perlxstut.html">perlxstut</a> explain the details.</p>
<p>If you don't need to use C code, other tools such as
ExtUtils::ModuleMaker and Module::Starter, can help you
create a skeleton module distribution.</p>
<p>You may also want to see Sam Tregar's "Writing Perl Modules
for CPAN" ( <a href="http://apress.com/book/bookDisplay.html?bID=14">http://apress.com/book/bookDisplay.html?bID=14</a> )
which is the best hands-on guide to creating module
distributions.</p>
<a name="How-do-I-adopt-or-take-over-a-module-already-on-CPAN%3f"></a><h2>How do I adopt or take over a module already on CPAN?</h2>
<p>(contributed by brian d foy)</p>
<p>The easiest way to take over a module is to have the current
module maintainer either make you a co-maintainer or transfer
the module to you.</p>
<p>If you can't reach the author for some reason (e.g. email bounces),
the PAUSE admins at modules@perl.org can help. The PAUSE admins
treat each case individually.</p>
<ul>
<li>
<p>Get a login for the Perl Authors Upload Server (PAUSE) if you don't
already have one: <a href="http://pause.perl.org">http://pause.perl.org</a></p>
</li>
<li>
<p>Write to modules@perl.org explaining what you did to contact the
current maintainer. The PAUSE admins will also try to reach the
maintainer.</p>
</li>
<li>
<p>Post a public message in a heavily trafficked site announcing your
intention to take over the module.</p>
</li>
<li>
<p>Wait a bit. The PAUSE admins don't want to act too quickly in case
the current maintainer is on holiday. If there's no response to
private communication or the public post, a PAUSE admin can transfer
it to you.</p>
</li>
</ul>
<a name="How-do-I-create-a-class%3f"></a><h2>How do I create a class?
 </h2>
<p>(contributed by brian d foy)</p>
<p>In Perl, a class is just a package, and methods are just subroutines.
Perl doesn't get more formal than that and lets you set up the package
just the way that you like it (that is, it doesn't set up anything for
you).</p>
<p>The Perl documentation has several tutorials that cover class
creation, including <a href="perlboot.html">perlboot</a> (Barnyard Object Oriented Tutorial),
<a href="perltoot.html">perltoot</a> (Tom's Object Oriented Tutorial), <a href="perlbot.html">perlbot</a> (Bag o'
Object Tricks), and <a href="perlobj.html">perlobj</a>.</p>
<a name="How-can-I-tell-if-a-variable-is-tainted%3f"></a><h2>How can I tell if a variable is tainted?</h2>
<p>You can use the tainted() function of the Scalar::Util module, available
from CPAN (or included with Perl since release 5.8.0).
See also <a href="perlsec.html#Laundering-and-Detecting-Tainted-Data">Laundering and Detecting Tainted Data in perlsec</a>.</p>
<a name="What's-a-closure%3f"></a><h2>What's a closure?</h2>
<p>Closures are documented in <a href="perlref.html">perlref</a>.</p>
<p><i>Closure</i> is a computer science term with a precise but
hard-to-explain meaning. Usually, closures are implemented in Perl as
anonymous subroutines with lasting references to lexical variables
outside their own scopes. These lexicals magically refer to the
variables that were around when the subroutine was defined (deep
binding).</p>
<p>Closures are most often used in programming languages where you can
have the return value of a function be itself a function, as you can
in Perl. Note that some languages provide anonymous functions but are
not capable of providing proper closures: the Python language, for
example.  For more information on closures, check out any textbook on
functional programming.  Scheme is a language that not only supports
but encourages closures.</p>
<p>Here's a classic non-closure function-generating function:</p>
<pre class="verbatim"><ol><li><a name="add_function_generator"></a>	sub <span class="m">add_function_generator</span> <span class="s">{</span></li><li>		<a class="l_k" href="functions/return.html">return</a> <a class="l_k" href="functions/sub.html">sub</a> <span class="s">{</span> <a class="l_k" href="functions/shift.html">shift</a><span class="s">(</span><span class="s">)</span> + <a class="l_k" href="functions/shift.html">shift</a><span class="s">(</span><span class="s">)</span> <span class="s">}</span><span class="sc">;</span></li><li>		<span class="s">}</span></li><li></li><li>	<span class="i">$add_sub</span> = <span class="i">add_function_generator</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>	<span class="i">$sum</span> = <span class="i">$add_sub</span>-&gt;<span class="s">(</span><span class="n">4</span><span class="cm">,</span><span class="n">5</span><span class="s">)</span><span class="sc">;</span>                <span class="c"># $sum is 9 now.</span></li></ol></pre><p>The anonymous subroutine returned by add_function_generator() isn't
technically a closure because it refers to no lexicals outside its own
scope.  Using a closure gives you a <i>function template</i> with some
customization slots left out to be filled later.</p>
<p>Contrast this with the following make_adder() function, in which the
returned anonymous function contains a reference to a lexical variable
outside the scope of that function itself.  Such a reference requires
that Perl return a proper closure, thus locking in for all time the
value that the lexical had when the function was created.</p>
<pre class="verbatim"><ol><li><a name="make_adder"></a>	sub <span class="m">make_adder</span> <span class="s">{</span></li><li>		<a class="l_k" href="functions/my.html">my</a> <span class="i">$addpiece</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>		<a class="l_k" href="functions/return.html">return</a> <a class="l_k" href="functions/sub.html">sub</a> <span class="s">{</span> <a class="l_k" href="functions/shift.html">shift</a><span class="s">(</span><span class="s">)</span> + <span class="i">$addpiece</span> <span class="s">}</span><span class="sc">;</span></li><li>	<span class="s">}</span></li><li></li><li>	<span class="i">$f1</span> = <span class="i">make_adder</span><span class="s">(</span><span class="n">20</span><span class="s">)</span><span class="sc">;</span></li><li>	<span class="i">$f2</span> = <span class="i">make_adder</span><span class="s">(</span><span class="n">555</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Now <code class="inline"><span class="i">&amp;$f1</span><span class="s">(</span><span class="i">$n</span><span class="s">)</span></code>
 is always 20 plus whatever $n you pass in, whereas
<code class="inline"><span class="i">&amp;$f2</span><span class="s">(</span><span class="i">$n</span><span class="s">)</span></code>
 is always 555 plus whatever $n you pass in.  The $addpiece
in the closure sticks around.</p>
<p>Closures are often used for less esoteric purposes.  For example, when
you want to pass in a bit of code into a function:</p>
<pre class="verbatim"><ol><li>	<a class="l_k" href="functions/my.html">my</a> <span class="i">$line</span><span class="sc">;</span></li><li>	<span class="i">timeout</span><span class="s">(</span> <span class="n">30</span><span class="cm">,</span> <a class="l_k" href="functions/sub.html">sub</a> <span class="s">{</span> <span class="i">$line</span> = <span class="q">&lt;STDIN&gt;</span> <span class="s">}</span> <span class="s">)</span><span class="sc">;</span></li></ol></pre><p>If the code to execute had been passed in as a string,
<code class="inline"><span class="q">&#39;$line = &lt;STDIN&gt;&#39;</span></code>
, there would have been no way for the
hypothetical timeout() function to access the lexical variable
$line back in its caller's scope.</p>
<p>Another use for a closure is to make a variable <i>private</i> to a
named subroutine, e.g. a counter that gets initialized at creation
time of the sub and can only be modified from within the sub.
This is sometimes used with a BEGIN block in package files to make
sure a variable doesn't get meddled with during the lifetime of the
package:</p>
<pre class="verbatim"><ol><li>	BEGIN <span class="s">{</span></li><li>		<a class="l_k" href="functions/my.html">my</a> <span class="i">$id</span> = <span class="n">0</span><span class="sc">;</span></li><li><a name="next_id"></a>		sub <span class="m">next_id</span> <span class="s">{</span> ++<span class="i">$id</span> <span class="s">}</span></li><li>	<span class="s">}</span></li></ol></pre><p>This is discussed in more detail in <a href="perlsub.html">perlsub</a>; see the entry on
<i>Persistent Private Variables</i>.</p>
<a name="What-is-variable-suicide-and-how-can-I-prevent-it%3f"></a><h2>What is variable suicide and how can I prevent it?</h2>
<p>This problem was fixed in perl 5.004_05, so preventing it means upgrading
your version of perl. ;)</p>
<p>Variable suicide is when you (temporarily or permanently) lose the value
of a variable.  It is caused by scoping through my() and local()
interacting with either closures or aliased foreach() iterator variables
and subroutine arguments.  It used to be easy to inadvertently lose a
variable's value this way, but now it's much harder.  Take this code:</p>
<pre class="verbatim"><ol><li>	<a class="l_k" href="functions/my.html">my</a> <span class="i">$f</span> = <span class="q">&#39;foo&#39;</span><span class="sc">;</span></li><li><a name="T"></a>	sub <span class="m">T</span> <span class="s">{</span></li><li>		while <span class="s">(</span><span class="i">$i</span>++ &lt; <span class="n">3</span><span class="s">)</span> <span class="s">{</span> <a class="l_k" href="functions/my.html">my</a> <span class="i">$f</span> = <span class="i">$f</span><span class="sc">;</span> <span class="i">$f</span> .= <span class="q">&quot;bar&quot;</span><span class="sc">;</span> <a class="l_k" href="functions/print.html">print</a> <span class="i">$f</span><span class="cm">,</span> <span class="q">&quot;\n&quot;</span> <span class="s">}</span></li><li>		<span class="s">}</span></li><li></li><li>	<span class="i">T</span><span class="sc">;</span></li><li>	<a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;Finally $f\n&quot;</span><span class="sc">;</span></li></ol></pre><p>If you are experiencing variable suicide, that <code class="inline"><a class="l_k" href="functions/my.html">my</a> <span class="i">$f</span></code>
 in the subroutine
doesn't pick up a fresh copy of the <code class="inline"><span class="i">$f</span></code>
 whose value is &lt;foo&gt;. The output
shows that inside the subroutine the value of <code class="inline"><span class="i">$f</span></code>
 leaks through when it
shouldn't, as in this output:</p>
<pre class="verbatim"><ol><li>	<span class="w">foobar</span></li><li>	<span class="w">foobarbar</span></li><li>	<span class="w">foobarbarbar</span></li><li>	<span class="w">Finally</span> <span class="w">foo</span></li></ol></pre><p>The $f that has "bar" added to it three times should be a new <code class="inline"><span class="i">$f</span></code>

<code class="inline"><a class="l_k" href="functions/my.html">my</a> <span class="i">$f</span></code>
 should create a new lexical variable each time through the loop.
The expected output is:</p>
<pre class="verbatim"><ol><li>	<span class="w">foobar</span></li><li>	<span class="w">foobar</span></li><li>	<span class="w">foobar</span></li><li>	<span class="w">Finally</span> <span class="w">foo</span></li></ol></pre><a name="How-can-I-pass%2freturn-a-%7bFunction%2c-FileHandle%2c-Array%2c-Hash%2c-Method%2c-Regex%7d%3f"></a><h2>How can I pass/return a {Function, FileHandle, Array, Hash, Method, Regex}?</h2>
<p>You need to pass references to these objects.  See <a href="perlsub.html#Pass-by-Reference">Pass by Reference in perlsub</a> for this particular question, and <a href="perlref.html">perlref</a> for
information on references.</p>
<ul>
<li><a name="Passing-Variables-and-Functions"></a><b>Passing Variables and Functions</b>
<p>Regular variables and functions are quite easy to pass: just pass in a
reference to an existing or anonymous variable or function:</p>
<pre class="verbatim"><ol><li>	<span class="i">func</span><span class="s">(</span> \<span class="i">$some_scalar</span> <span class="s">)</span><span class="sc">;</span></li><li></li><li>	<span class="i">func</span><span class="s">(</span> \<span class="i">@some_array</span>  <span class="s">)</span><span class="sc">;</span></li><li>	<span class="i">func</span><span class="s">(</span> <span class="s">[</span> <span class="n">1</span> .. <span class="n">10</span> <span class="s">]</span>   <span class="s">)</span><span class="sc">;</span></li><li></li><li>	<span class="i">func</span><span class="s">(</span> \<span class="i">%some_hash</span>   <span class="s">)</span><span class="sc">;</span></li><li>	<span class="i">func</span><span class="s">(</span> <span class="s">{</span> <span class="w">this</span> <span class="cm">=&gt;</span> <span class="n">10</span><span class="cm">,</span> <span class="w">that</span> <span class="cm">=&gt;</span> <span class="n">20</span> <span class="s">}</span>   <span class="s">)</span><span class="sc">;</span></li><li></li><li>	<span class="i">func</span><span class="s">(</span> \<span class="i">&amp;some_func</span>   <span class="s">)</span><span class="sc">;</span></li><li>	<span class="i">func</span><span class="s">(</span> <a class="l_k" href="functions/sub.html">sub</a> <span class="s">{</span> <span class="i">$_</span>[<span class="n">0</span>] ** <span class="i">$_</span>[<span class="n">1</span>] <span class="s">}</span>   <span class="s">)</span><span class="sc">;</span></li></ol></pre></li>
<li><a name="Passing-Filehandles"></a><b>Passing Filehandles</b>
<p>As of Perl 5.6, you can represent filehandles with scalar variables
which you treat as any other scalar.</p>
<pre class="verbatim"><ol><li>	<a class="l_k" href="functions/open.html">open</a> <a class="l_k" href="functions/my.html">my</a> <span class="i">$fh</span><span class="cm">,</span> <span class="i">$filename</span> or <a class="l_k" href="functions/die.html">die</a> <span class="q">&quot;Cannot open $filename! $!&quot;</span><span class="sc">;</span></li><li>	<span class="i">func</span><span class="s">(</span> <span class="i">$fh</span> <span class="s">)</span><span class="sc">;</span></li><li></li><li><a name="func"></a>	sub <span class="m">func</span> <span class="s">{</span></li><li>		<a class="l_k" href="functions/my.html">my</a> <span class="i">$passed_fh</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li></li><li>		<a class="l_k" href="functions/my.html">my</a> <span class="i">$line</span> = <span class="q">&lt;$passed_fh&gt;</span><span class="sc">;</span></li><li>		<span class="s">}</span></li></ol></pre><p>Before Perl 5.6, you had to use the <code class="inline"><span class="i">*FH</span></code>
 or <code class="inline">\<span class="i">*FH</span></code>
 notations.
These are "typeglobs"--see <a href="perldata.html#Typeglobs-and-Filehandles">Typeglobs and Filehandles in perldata</a>
and especially <a href="perlsub.html#Pass-by-Reference">Pass by Reference in perlsub</a> for more information.</p>
</li>
<li><a name="Passing-Regexes"></a><b>Passing Regexes</b>
<p>Here's an example of how to pass in a string and a regular expression
for it to match against. You construct the pattern with the <code class="inline"><a class="l_k" href="functions/qr.html">qr//</a></code>
operator:</p>
<pre class="verbatim"><ol><li><a name="compare"></a>	sub <span class="m">compare($$)</span> <span class="s">{</span></li><li>		<a class="l_k" href="functions/my.html">my</a> <span class="s">(</span><span class="i">$val1</span><span class="cm">,</span> <span class="i">$regex</span><span class="s">)</span> = <span class="i">@_</span><span class="sc">;</span></li><li>		<a class="l_k" href="functions/my.html">my</a> <span class="i">$retval</span> = <span class="i">$val1</span> =~ <span class="q">/$regex/</span><span class="sc">;</span></li><li>	<a class="l_k" href="functions/return.html">return</a> <span class="i">$retval</span><span class="sc">;</span></li><li>	<span class="s">}</span></li><li>	<span class="i">$match</span> = <span class="i">compare</span><span class="s">(</span><span class="q">&quot;old McDonald&quot;</span><span class="cm">,</span> <span class="q">qr/d.*D/i</span><span class="s">)</span><span class="sc">;</span></li></ol></pre></li>
<li><a name="Passing-Methods"></a><b>Passing Methods</b>
<p>To pass an object method into a subroutine, you can do this:</p>
<pre class="verbatim"><ol><li>	call_a_lot(10, $some_obj, "methname")</li><li>	sub call_a_lot {</li><li>		my ($count, $widget, $trick) = @_;</li><li>		for (my $i = 0; $i &lt; $count; $i++) {</li><li>			$widget-&gt;$trick();</li><li>		}</li><li>	}</li></ol></pre><p>Or, you can use a closure to bundle up the object, its
method call, and arguments:</p>
<pre class="verbatim"><ol><li>	<a class="l_k" href="functions/my.html">my</a> <span class="i">$whatnot</span> =  <a class="l_k" href="functions/sub.html">sub</a> <span class="s">{</span> <span class="i">$some_obj</span><span class="i">-&gt;obfuscate</span><span class="s">(</span><span class="i">@args</span><span class="s">)</span> <span class="s">}</span><span class="sc">;</span></li><li>	<span class="i">func</span><span class="s">(</span><span class="i">$whatnot</span><span class="s">)</span><span class="sc">;</span></li><li><a name="func"></a>	sub <span class="m">func</span> <span class="s">{</span></li><li>		<a class="l_k" href="functions/my.html">my</a> <span class="i">$code</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>		<span class="i">&amp;$code</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>	<span class="s">}</span></li></ol></pre><p>You could also investigate the can() method in the UNIVERSAL class
(part of the standard perl distribution).</p>
</li>
</ul>
<a name="How-do-I-create-a-static-variable%3f"></a><h2>How do I create a static variable?</h2>
<p>(contributed by brian d foy)</p>
<p>In Perl 5.10, declare the variable with <code class="inline"><a class="l_k" href="functions/state.html">state</a></code>. The <code class="inline"><a class="l_k" href="functions/state.html">state</a></code>
declaration creates the lexical variable that persists between calls
to the subroutine:</p>
<pre class="verbatim"><ol><li><a name="counter"></a>	sub <span class="m">counter</span> <span class="s">{</span> <span class="w">state</span> <span class="i">$count</span> = <span class="n">1</span><span class="sc">;</span> <span class="i">$counter</span>++ <span class="s">}</span></li></ol></pre><p>You can fake a static variable by using a lexical variable which goes
out of scope. In this example, you define the subroutine <code class="inline"><span class="w">counter</span></code>
, and
it uses the lexical variable <code class="inline"><span class="i">$count</span></code>
. Since you wrap this in a BEGIN
block, <code class="inline"><span class="i">$count</span></code>
 is defined at compile-time, but also goes out of
scope at the end of the BEGIN block. The BEGIN block also ensures that
the subroutine and the value it uses is defined at compile-time so the
subroutine is ready to use just like any other subroutine, and you can
put this code in the same place as other subroutines in the program
text (i.e. at the end of the code, typically). The subroutine
<code class="inline"><span class="w">counter</span></code>
 still has a reference to the data, and is the only way you
can access the value (and each time you do, you increment the value).
The data in chunk of memory defined by <code class="inline"><span class="i">$count</span></code>
 is private to
<code class="inline"><span class="w">counter</span></code>
.</p>
<pre class="verbatim"><ol><li>	BEGIN {</li><li>		my $count = 1;</li><li>		sub counter { $count++ }</li><li>	}</li><li></li><li>	my $start = counter();</li><li></li><li>	.... # code that calls counter();</li><li></li><li>	my $end = counter();</li></ol></pre><p>In the previous example, you created a function-private variable
because only one function remembered its reference. You could define
multiple functions while the variable is in scope, and each function
can share the "private" variable. It's not really "static" because you
can access it outside the function while the lexical variable is in
scope, and even create references to it. In this example,
<code class="inline"><span class="w">increment_count</span></code>
 and <code class="inline"><span class="w">return_count</span></code>
 share the variable. One
function adds to the value and the other simply returns the value.
They can both access <code class="inline"><span class="i">$count</span></code>
, and since it has gone out of scope,
there is no other way to access it.</p>
<pre class="verbatim"><ol><li>	BEGIN <span class="s">{</span></li><li>		<a class="l_k" href="functions/my.html">my</a> <span class="i">$count</span> = <span class="n">1</span><span class="sc">;</span></li><li><a name="increment_count"></a>		sub <span class="m">increment_count</span> <span class="s">{</span> <span class="i">$count</span>++ <span class="s">}</span></li><li><a name="return_count"></a>		sub <span class="m">return_count</span>    <span class="s">{</span> <span class="i">$count</span> <span class="s">}</span></li><li>	<span class="s">}</span></li></ol></pre><p>To declare a file-private variable, you still use a lexical variable.
A file is also a scope, so a lexical variable defined in the file
cannot be seen from any other file.</p>
<p>See <a href="perlsub.html#Persistent-Private-Variables">Persistent Private Variables in perlsub</a> for more information.
The discussion of closures in <a href="perlref.html">perlref</a> may help you even though we
did not use anonymous subroutines in this answer. See
<a href="perlsub.html#Persistent-Private-Variables">Persistent Private Variables in perlsub</a> for details.</p>
<a name="What's-the-difference-between-dynamic-and-lexical-(static)-scoping%3f--Between-local()-and-my()%3f"></a><h2>What's the difference between dynamic and lexical (static) scoping?  Between local() and my()?</h2>
<p><code class="inline"><a class="l_k" href="functions/local.html">local($x)</a></code> saves away the old value of the global variable <code class="inline"><span class="i">$x</span></code>

and assigns a new value for the duration of the subroutine <i>which is
visible in other functions called from that subroutine</i>.  This is done
at run-time, so is called dynamic scoping.  local() always affects global
variables, also called package variables or dynamic variables.</p>
<p><code class="inline"><a class="l_k" href="functions/my.html">my($x)</a></code> creates a new variable that is only visible in the current
subroutine.  This is done at compile-time, so it is called lexical or
static scoping.  my() always affects private variables, also called
lexical variables or (improperly) static(ly scoped) variables.</p>
<p>For instance:</p>
<pre class="verbatim"><ol><li><a name="visible"></a>	sub <span class="m">visible</span> <span class="s">{</span></li><li>		<a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;var has value $var\n&quot;</span><span class="sc">;</span></li><li>		<span class="s">}</span></li><li></li><li><a name="dynamic"></a>	sub <span class="m">dynamic</span> <span class="s">{</span></li><li>		<a class="l_k" href="functions/local.html">local</a> <span class="i">$var</span> = <span class="q">&#39;local&#39;</span><span class="sc">;</span>	<span class="c"># new temporary value for the still-global</span></li><li>		<span class="i">visible</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span>              <span class="c">#   variable called $var</span></li><li>		<span class="s">}</span></li><li></li><li><a name="lexical"></a>	sub <span class="m">lexical</span> <span class="s">{</span></li><li>		<a class="l_k" href="functions/my.html">my</a> <span class="i">$var</span> = <span class="q">&#39;private&#39;</span><span class="sc">;</span>    <span class="c"># new private variable, $var</span></li><li>		<span class="i">visible</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span>              <span class="c"># (invisible outside of sub scope)</span></li><li>		<span class="s">}</span></li><li></li><li>	<span class="i">$var</span> = <span class="q">&#39;global&#39;</span><span class="sc">;</span></li><li></li><li>	<span class="i">visible</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span>      		<span class="c"># prints global</span></li><li>	<span class="i">dynamic</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span>      		<span class="c"># prints local</span></li><li>	<span class="i">lexical</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span>      		<span class="c"># prints global</span></li></ol></pre><p>Notice how at no point does the value "private" get printed.  That's
because $var only has that value within the block of the lexical()
function, and it is hidden from the called subroutine.</p>
<p>In summary, local() doesn't make what you think of as private, local
variables.  It gives a global variable a temporary value.  my() is
what you're looking for if you want private variables.</p>
<p>See <a href="perlsub.html#Private-Variables-via-my()">Private Variables via my() in perlsub</a> and
<a href="perlsub.html#Temporary-Values-via-local()">Temporary Values via local() in perlsub</a> for excruciating details.</p>
<a name="How-can-I-access-a-dynamic-variable-while-a-similarly-named-lexical-is-in-scope%3f"></a><h2>How can I access a dynamic variable while a similarly named lexical is in scope?</h2>
<p>If you know your package, you can just mention it explicitly, as in
$Some_Pack::var. Note that the notation $::var is <b>not</b> the dynamic $var
in the current package, but rather the one in the "main" package, as
though you had written $main::var.</p>
<pre class="verbatim"><ol><li>	<a class="l_k" href="functions/use.html">use</a> <span class="w">vars</span> <span class="q">&#39;$var&#39;</span><span class="sc">;</span></li><li>	<a class="l_k" href="functions/local.html">local</a> <span class="i">$var</span> = <span class="q">&quot;global&quot;</span><span class="sc">;</span></li><li>	<a class="l_k" href="functions/my.html">my</a>    <span class="i">$var</span> = <span class="q">&quot;lexical&quot;</span><span class="sc">;</span></li><li></li><li>	<a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;lexical is $var\n&quot;</span><span class="sc">;</span></li><li>	<a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;global  is $main::var\n&quot;</span><span class="sc">;</span></li></ol></pre><p>Alternatively you can use the compiler directive our() to bring a
dynamic variable into the current lexical scope.</p>
<pre class="verbatim"><ol><li>	<a class="l_k" href="functions/require.html">require</a> <span class="n">5.006</span><span class="sc">;</span> <span class="c"># our() did not exist before 5.6</span></li><li>	<a class="l_k" href="functions/use.html">use</a> <span class="w">vars</span> <span class="q">&#39;$var&#39;</span><span class="sc">;</span></li><li></li><li>	<a class="l_k" href="functions/local.html">local</a> <span class="i">$var</span> = <span class="q">&quot;global&quot;</span><span class="sc">;</span></li><li>	<a class="l_k" href="functions/my.html">my</a> <span class="i">$var</span>    = <span class="q">&quot;lexical&quot;</span><span class="sc">;</span></li><li></li><li>	<a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;lexical is $var\n&quot;</span><span class="sc">;</span></li><li></li><li>	<span class="s">{</span></li><li>		<a class="l_k" href="functions/our.html">our</a> <span class="i">$var</span><span class="sc">;</span></li><li>		<a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;global  is $var\n&quot;</span><span class="sc">;</span></li><li>	<span class="s">}</span></li></ol></pre><a name="What's-the-difference-between-deep-and-shallow-binding%3f"></a><h2>What's the difference between deep and shallow binding?</h2>
<p>In deep binding, lexical variables mentioned in anonymous subroutines
are the same ones that were in scope when the subroutine was created.
In shallow binding, they are whichever variables with the same names
happen to be in scope when the subroutine is called.  Perl always uses
deep binding of lexical variables (i.e., those created with my()).
However, dynamic variables (aka global, local, or package variables)
are effectively shallowly bound.  Consider this just one more reason
not to use them.  See the answer to <a href="#What's-a-closure%3f">What's a closure?</a>.</p>
<a name="Why-doesn't-%22my(%24foo)-%3d-%3cFILE%3e%3b%22-work-right%3f"></a><h2>Why doesn't "my($foo) = &lt;FILE&gt;;" work right?</h2>
<p><code class="inline"><a class="l_k" href="functions/my.html">my()</a></code> and <code class="inline"><a class="l_k" href="functions/local.html">local()</a></code> give list context to the right hand side
of <code class="inline">=</code>
.  The &lt;FH&gt; read operation, like so many of Perl's
functions and operators, can tell which context it was called in and
behaves appropriately.  In general, the scalar() function can help.
This function does nothing to the data itself (contrary to popular myth)
but rather tells its argument to behave in whatever its scalar fashion is.
If that function doesn't have a defined scalar behavior, this of course
doesn't help you (such as with sort()).</p>
<p>To enforce scalar context in this particular case, however, you need
merely omit the parentheses:</p>
<pre class="verbatim"><ol><li>	<a class="l_k" href="functions/local.html">local</a><span class="s">(</span><span class="i">$foo</span><span class="s">)</span> = <span class="q">&lt;FILE&gt;</span><span class="sc">;</span>	    <span class="c"># WRONG</span></li><li>	<a class="l_k" href="functions/local.html">local</a><span class="s">(</span><span class="i">$foo</span><span class="s">)</span> = <a class="l_k" href="functions/scalar.html">scalar</a><span class="s">(</span><span class="q">&lt;FILE&gt;</span><span class="s">)</span><span class="sc">;</span>   <span class="c"># ok</span></li><li>	<a class="l_k" href="functions/local.html">local</a> <span class="i">$foo</span>  = <span class="q">&lt;FILE&gt;</span><span class="sc">;</span>	    <span class="c"># right</span></li></ol></pre><p>You should probably be using lexical variables anyway, although the
issue is the same here:</p>
<pre class="verbatim"><ol><li>	<a class="l_k" href="functions/my.html">my</a><span class="s">(</span><span class="i">$foo</span><span class="s">)</span> = <span class="q">&lt;FILE&gt;</span><span class="sc">;</span>	<span class="c"># WRONG</span></li><li>	<a class="l_k" href="functions/my.html">my</a> <span class="i">$foo</span>  = <span class="q">&lt;FILE&gt;</span><span class="sc">;</span>	<span class="c"># right</span></li></ol></pre><a name="How-do-I-redefine-a-builtin-function%2c-operator%2c-or-method%3f"></a><h2>How do I redefine a builtin function, operator, or method?</h2>
<p>Why do you want to do that? :-)</p>
<p>If you want to override a predefined function, such as open(),
then you'll have to import the new definition from a different
module.  See <a href="perlsub.html#Overriding-Built-in-Functions">Overriding Built-in Functions in perlsub</a>.</p>
<p>If you want to overload a Perl operator, such as <code class="inline">+</code>
 or <code class="inline"><span class="i">**</span></code>
,
then you'll want to use the <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">overload</span></code>
 pragma, documented
in <a href="overload.html">overload</a>.</p>
<p>If you're talking about obscuring method calls in parent classes,
see <a href="perltoot.html#Overridden-Methods">Overridden Methods in perltoot</a>.</p>
<a name="What's-the-difference-between-calling-a-function-as-%26foo-and-foo()%3f"></a><h2>What's the difference between calling a function as &amp;foo and foo()?</h2>
<p>(contributed by brian d foy)</p>
<p>Calling a subroutine as <code class="inline"><span class="i">&amp;foo</span></code>
 with no trailing parentheses ignores
the prototype of <code class="inline"><span class="w">foo</span></code>
 and passes it the current value of the argument
list, <code class="inline"><span class="i">@_</span></code>
. Here's an example; the <code class="inline"><span class="w">bar</span></code>
 subroutine calls <code class="inline"><span class="i">&amp;foo</span></code>
,
which prints its arguments list:</p>
<pre class="verbatim"><ol><li><a name="bar"></a>	sub <span class="m">bar</span> <span class="s">{</span> <span class="i">&amp;foo</span> <span class="s">}</span></li><li></li><li><a name="foo"></a>	sub <span class="m">foo</span> <span class="s">{</span> <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;Args in foo are: @_\n&quot;</span> <span class="s">}</span></li><li></li><li>	<span class="i">bar</span><span class="s">(</span> <span class="q">qw( a b c )</span> <span class="s">)</span><span class="sc">;</span></li></ol></pre><p>When you call <code class="inline"><span class="w">bar</span></code>
 with arguments, you see that <code class="inline"><span class="w">foo</span></code>
 got the same <code class="inline"><span class="i">@_</span></code>
:</p>
<pre class="verbatim"><ol><li>	Args in foo are: a b c</li></ol></pre><p>Calling the subroutine with trailing parentheses, with or without arguments,
does not use the current <code class="inline"><span class="i">@_</span></code>
 and respects the subroutine prototype. Changing
the example to put parentheses after the call to <code class="inline"><span class="w">foo</span></code>
 changes the program:</p>
<pre class="verbatim"><ol><li><a name="bar"></a>	sub <span class="m">bar</span> <span class="s">{</span> <span class="i">&amp;foo</span><span class="s">(</span><span class="s">)</span> <span class="s">}</span></li><li></li><li><a name="foo"></a>	sub <span class="m">foo</span> <span class="s">{</span> <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;Args in foo are: @_\n&quot;</span> <span class="s">}</span></li><li></li><li>	<span class="i">bar</span><span class="s">(</span> <span class="q">qw( a b c )</span> <span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Now the output shows that <code class="inline"><span class="w">foo</span></code>
 doesn't get the <code class="inline"><span class="i">@_</span></code>
 from its caller.</p>
<pre class="verbatim"><ol><li>	Args in foo are:</li></ol></pre><p>The main use of the <code class="inline"><span class="i">@_</span></code>
 pass-through feature is to write subroutines
whose main job it is to call other subroutines for you. For further
details, see <a href="perlsub.html">perlsub</a>.</p>
<a name="How-do-I-create-a-switch-or-case-statement%3f"></a><h2>How do I create a switch or case statement?</h2>
<p>In Perl 5.10, use the <code class="inline">given-when</code>
 construct described in <a href="perlsyn.html">perlsyn</a>:</p>
<pre class="verbatim"><ol><li>	<a class="l_k" href="functions/use.html">use</a> <span class="n">5.010</span><span class="sc">;</span></li><li></li><li>	given <span class="s">(</span> <span class="i">$string</span> <span class="s">)</span> <span class="s">{</span></li><li>		when<span class="s">(</span> <span class="q">&#39;Fred&#39;</span> <span class="s">)</span>        <span class="s">{</span> <a class="l_k" href="functions/say.html">say</a> <span class="q">&quot;I found Fred!&quot;</span> <span class="s">}</span></li><li>		when<span class="s">(</span> <span class="q">&#39;Barney&#39;</span> <span class="s">)</span>      <span class="s">{</span> <a class="l_k" href="functions/say.html">say</a> <span class="q">&quot;I found Barney!&quot;</span> <span class="s">}</span></li><li>		when<span class="s">(</span> <span class="q">/Bamm-?Bamm/</span> <span class="s">)</span>  <span class="s">{</span> <a class="l_k" href="functions/say.html">say</a> <span class="q">&quot;I found Bamm-Bamm!&quot;</span> <span class="s">}</span></li><li>		<span class="i">default</span>               <span class="s">{</span> <a class="l_k" href="functions/say.html">say</a> <span class="q">&quot;I don&#39;t recognize the name!&quot;</span> <span class="s">}</span></li><li>		<span class="s">}</span><span class="sc">;</span></li></ol></pre><p>If one wants to use pure Perl and to be compatible with Perl versions
prior to 5.10, the general answer is to use <code class="inline">if-elsif-else</code>:</p>
<pre class="verbatim"><ol><li>	for <span class="s">(</span><span class="i">$variable_to_test</span><span class="s">)</span> <span class="s">{</span></li><li>		if    <span class="s">(</span><span class="q">/pat1/</span><span class="s">)</span>  <span class="s">{</span> <span class="s">}</span>     <span class="c"># do something</span></li><li>		elsif <span class="s">(</span><span class="q">/pat2/</span><span class="s">)</span>  <span class="s">{</span> <span class="s">}</span>     <span class="c"># do something else</span></li><li>		elsif <span class="s">(</span><span class="q">/pat3/</span><span class="s">)</span>  <span class="s">{</span> <span class="s">}</span>     <span class="c"># do something else</span></li><li>		else            <span class="s">{</span> <span class="s">}</span>     <span class="c"># default</span></li><li>		<span class="s">}</span></li></ol></pre><p>Here's a simple example of a switch based on pattern matching,
lined up in a way to make it look more like a switch statement.
We'll do a multiway conditional based on the type of reference stored
in $whatchamacallit:</p>
<pre class="verbatim"><ol><li>    <span class="j">SWITCH:</span> for <span class="s">(</span><a class="l_k" href="functions/ref.html">ref</a> <span class="i">$whatchamacallit</span><span class="s">)</span> <span class="s">{</span></li><li></li><li>	<span class="q">/^$/</span>		&amp;&amp; <a class="l_k" href="functions/die.html">die</a> <span class="q">&quot;not a reference&quot;</span><span class="sc">;</span></li><li></li><li>	<span class="q">/SCALAR/</span>	&amp;&amp; <a class="l_k" href="functions/do.html">do</a> <span class="s">{</span></li><li>				<span class="i">print_scalar</span><span class="s">(</span><span class="i">$$ref</span><span class="s">)</span><span class="sc">;</span></li><li>				<a class="l_k" href="functions/last.html">last</a> <span class="j">SWITCH</span><span class="sc">;</span></li><li>			<span class="s">}</span><span class="sc">;</span></li><li></li><li>	<span class="q">/ARRAY/</span>		&amp;&amp; <a class="l_k" href="functions/do.html">do</a> <span class="s">{</span></li><li>				<span class="i">print_array</span><span class="s">(</span><span class="i">@$ref</span><span class="s">)</span><span class="sc">;</span></li><li>				<a class="l_k" href="functions/last.html">last</a> <span class="j">SWITCH</span><span class="sc">;</span></li><li>			<span class="s">}</span><span class="sc">;</span></li><li></li><li>	<span class="q">/HASH/</span>		&amp;&amp; <a class="l_k" href="functions/do.html">do</a> <span class="s">{</span></li><li>				<span class="i">print_hash</span><span class="s">(</span><span class="i">%$ref</span><span class="s">)</span><span class="sc">;</span></li><li>				<a class="l_k" href="functions/last.html">last</a> <span class="j">SWITCH</span><span class="sc">;</span></li><li>			<span class="s">}</span><span class="sc">;</span></li><li></li><li>	<span class="q">/CODE/</span>		&amp;&amp; <a class="l_k" href="functions/do.html">do</a> <span class="s">{</span></li><li>				<a class="l_k" href="functions/warn.html">warn</a> <span class="q">&quot;can&#39;t print function ref&quot;</span><span class="sc">;</span></li><li>				<a class="l_k" href="functions/last.html">last</a> <span class="j">SWITCH</span><span class="sc">;</span></li><li>			<span class="s">}</span><span class="sc">;</span></li><li></li><li>	<span class="c"># DEFAULT</span></li><li></li><li>	<a class="l_k" href="functions/warn.html">warn</a> <span class="q">&quot;User defined type skipped&quot;</span><span class="sc">;</span></li><li></li><li>    <span class="s">}</span></li></ol></pre><p>See <a href="perlsyn.html">perlsyn</a> for other examples in this style.</p>
<p>Sometimes you should change the positions of the constant and the variable.
For example, let's say you wanted to test which of many answers you were
given, but in a case-insensitive way that also allows abbreviations.
You can use the following technique if the strings all start with
different characters or if you want to arrange the matches so that
one takes precedence over another, as <code class="inline"><span class="q">&quot;SEND&quot;</span></code>
 has precedence over
<code class="inline"><span class="q">&quot;STOP&quot;</span></code>
 here:</p>
<pre class="verbatim"><ol><li>	<a class="l_k" href="functions/chomp.html">chomp</a><span class="s">(</span><span class="i">$answer</span> = &lt;&gt;<span class="s">)</span><span class="sc">;</span></li><li>	if    <span class="s">(</span><span class="q">&quot;SEND&quot;</span>  =~ <span class="q">/^\Q$answer/i</span><span class="s">)</span> <span class="s">{</span> <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;Action is send\n&quot;</span>  <span class="s">}</span></li><li>	elsif <span class="s">(</span><span class="q">&quot;STOP&quot;</span>  =~ <span class="q">/^\Q$answer/i</span><span class="s">)</span> <span class="s">{</span> <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;Action is stop\n&quot;</span>  <span class="s">}</span></li><li>	elsif <span class="s">(</span><span class="q">&quot;ABORT&quot;</span> =~ <span class="q">/^\Q$answer/i</span><span class="s">)</span> <span class="s">{</span> <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;Action is abort\n&quot;</span> <span class="s">}</span></li><li>	elsif <span class="s">(</span><span class="q">&quot;LIST&quot;</span>  =~ <span class="q">/^\Q$answer/i</span><span class="s">)</span> <span class="s">{</span> <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;Action is list\n&quot;</span>  <span class="s">}</span></li><li>	elsif <span class="s">(</span><span class="q">&quot;EDIT&quot;</span>  =~ <span class="q">/^\Q$answer/i</span><span class="s">)</span> <span class="s">{</span> <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;Action is edit\n&quot;</span>  <span class="s">}</span></li></ol></pre><p>A totally different approach is to create a hash of function references.</p>
<pre class="verbatim"><ol><li>	<a class="l_k" href="functions/my.html">my</a> <span class="i">%commands</span> = <span class="s">(</span></li><li>		<span class="q">&quot;happy&quot;</span> <span class="cm">=&gt;</span> \<span class="i">&amp;joy</span><span class="cm">,</span></li><li>		<span class="q">&quot;sad&quot;</span><span class="cm">,</span>  <span class="cm">=&gt;</span> \<span class="i">&amp;sullen</span><span class="cm">,</span></li><li>		<span class="q">&quot;done&quot;</span>  <span class="cm">=&gt;</span> <a class="l_k" href="functions/sub.html">sub</a> <span class="s">{</span> <a class="l_k" href="functions/die.html">die</a> <span class="q">&quot;See ya!&quot;</span> <span class="s">}</span><span class="cm">,</span></li><li>		<span class="q">&quot;mad&quot;</span>   <span class="cm">=&gt;</span> \<span class="i">&amp;angry</span><span class="cm">,</span></li><li>	<span class="s">)</span><span class="sc">;</span></li><li></li><li>	<a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;How are you? &quot;</span><span class="sc">;</span></li><li>	<a class="l_k" href="functions/chomp.html">chomp</a><span class="s">(</span><span class="i">$string</span> = <span class="q">&lt;STDIN&gt;</span><span class="s">)</span><span class="sc">;</span></li><li>	if <span class="s">(</span><span class="i">$commands</span>{<span class="i">$string</span>}<span class="s">)</span> <span class="s">{</span></li><li>		<span class="i">$commands</span>{<span class="i">$string</span>}-&gt;<span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>	<span class="s">}</span> else <span class="s">{</span></li><li>		<a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;No such command: $string\n&quot;</span><span class="sc">;</span></li><li>	<span class="s">}</span></li></ol></pre><p>Starting from Perl 5.8, a source filter module, <code class="inline"><span class="w">Switch</span></code>
, can also be
used to get switch and case. Its use is now discouraged, because it's
not fully compatible with the native switch of Perl 5.10, and because,
as it's implemented as a source filter, it doesn't always work as intended
when complex syntax is involved.</p>
<a name="How-can-I-catch-accesses-to-undefined-variables%2c-functions%2c-or-methods%3f"></a><h2>How can I catch accesses to undefined variables, functions, or methods?</h2>
<p>The AUTOLOAD method, discussed in <a href="perlsub.html#Autoloading">Autoloading in perlsub</a> and
<a href="perltoot.html#AUTOLOAD%3a-Proxy-Methods">AUTOLOAD: Proxy Methods in perltoot</a>, lets you capture calls to
undefined functions and methods.</p>
<p>When it comes to undefined variables that would trigger a warning
under <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">warnings</span></code>
, you can promote the warning to an error.</p>
<pre class="verbatim"><ol><li>	<a class="l_k" href="functions/use.html">use</a> <span class="w">warnings</span> <span class="w">FATAL</span> <span class="cm">=&gt;</span> <span class="q">qw(uninitialized)</span><span class="sc">;</span></li></ol></pre><a name="Why-can't-a-method-included-in-this-same-file-be-found%3f"></a><h2>Why can't a method included in this same file be found?</h2>
<p>Some possible reasons: your inheritance is getting confused, you've
misspelled the method name, or the object is of the wrong type.  Check
out <a href="perltoot.html">perltoot</a> for details about any of the above cases.  You may
also use <code class="inline"><a class="l_k" href="functions/print.html">print</a> <a class="l_k" href="functions/ref.html">ref</a><span class="s">(</span><span class="i">$object</span><span class="s">)</span></code>
 to find out the class <code class="inline"><span class="i">$object</span></code>
 was
blessed into.</p>
<p>Another possible reason for problems is that you've used the
indirect object syntax (eg, <code class="inline"><span class="w">find</span> <span class="w">Guru</span> <span class="q">&quot;Samy&quot;</span></code>
) on a class name
before Perl has seen that such a package exists.  It's wisest to make
sure your packages are all defined before you start using them, which
will be taken care of if you use the <code class="inline"><a class="l_k" href="functions/use.html">use</a></code> statement instead of
<code class="inline"><a class="l_k" href="functions/require.html">require</a></code>.  If not, make sure to use arrow notation (eg.,
<code class="inline"><span class="w">Guru</span><span class="w">-&gt;find</span><span class="s">(</span><span class="q">&quot;Samy&quot;</span><span class="s">)</span></code>
) instead.  Object notation is explained in
<a href="perlobj.html">perlobj</a>.</p>
<p>Make sure to read about creating modules in <a href="perlmod.html">perlmod</a> and
the perils of indirect objects in <a href="perlobj.html#Method-Invocation">Method Invocation in perlobj</a>.</p>
<a name="How-can-I-find-out-my-current-or-calling-package%3f"></a><h2>How can I find out my current or calling package?</h2>
<p>(contributed by brian d foy)</p>
<p>To find the package you are currently in, use the special literal
<code class="inline"><span class="w">__PACKAGE__</span></code>
, as documented in <a href="perldata.html">perldata</a>. You can only use the
special literals as separate tokens, so you can't interpolate them
into strings like you can with variables:</p>
<pre class="verbatim"><ol><li>	<a class="l_k" href="functions/my.html">my</a> <span class="i">$current_package</span> = <span class="w">__PACKAGE__</span><span class="sc">;</span></li><li>	<a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;I am in package $current_package\n&quot;</span><span class="sc">;</span></li></ol></pre><p>If you want to find the package calling your code, perhaps to give better
diagnostics as <code class="inline"><span class="w">Carp</span></code>
 does, use the <code class="inline"><a class="l_k" href="functions/caller.html">caller</a></code> built-in:</p>
<pre class="verbatim"><ol><li>	sub foo {</li><li>		my @args = ...;</li><li>		my( $package, $filename, $line ) = caller;</li><li></li><li>		print "I was called from package $package\n";</li><li>		);</li></ol></pre><p>By default, your program starts in package <code class="inline"><span class="w">main</span></code>
, so you will
always be in some package.</p>
<p>This is different from finding out the package an object is blessed
into, which might not be the current package. For that, use <code class="inline"><span class="w">blessed</span></code>

from <code class="inline"><span class="w">Scalar::Util</span></code>
, part of the Standard Library since Perl 5.8:</p>
<pre class="verbatim"><ol><li>	<a class="l_k" href="functions/use.html">use</a> <span class="w">Scalar::Util</span> <span class="q">qw(blessed)</span><span class="sc">;</span></li><li>	<a class="l_k" href="functions/my.html">my</a> <span class="i">$object_package</span> = <span class="i">blessed</span><span class="s">(</span> <span class="i">$object</span> <span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Most of the time, you shouldn't care what package an object is blessed
into, however, as long as it claims to inherit from that class:</p>
<pre class="verbatim"><ol><li>	<a class="l_k" href="functions/my.html">my</a> <span class="i">$is_right_class</span> = <a class="l_k" href="functions/eval.html">eval</a> <span class="s">{</span> <span class="i">$object</span><span class="i">-&gt;isa</span><span class="s">(</span> <span class="i">$package</span> <span class="s">)</span> <span class="s">}</span><span class="sc">;</span> <span class="c"># true or false</span></li></ol></pre><p>And, with Perl 5.10 and later, you don't have to check for an
inheritance to see if the object can handle a role. For that, you can
use <code class="inline"><span class="w">DOES</span></code>
, which comes from <code class="inline"><span class="w">UNIVERSAL</span></code>
:</p>
<pre class="verbatim"><ol><li>	<a class="l_k" href="functions/my.html">my</a> <span class="i">$class_does_it</span> = <a class="l_k" href="functions/eval.html">eval</a> <span class="s">{</span> <span class="i">$object</span><span class="i">-&gt;DOES</span><span class="s">(</span> <span class="i">$role</span> <span class="s">)</span> <span class="s">}</span><span class="sc">;</span> <span class="c"># true or false</span></li></ol></pre><p>You can safely replace <code class="inline"><span class="w">isa</span></code>
 with <code class="inline"><span class="w">DOES</span></code>
 (although the converse is not true).</p>
<a name="How-can-I-comment-out-a-large-block-of-Perl-code%3f"></a><h2>How can I comment out a large block of Perl code?</h2>
<p>(contributed by brian d foy)</p>
<p>The quick-and-dirty way to comment out more than one line of Perl is
to surround those lines with Pod directives. You have to put these
directives at the beginning of the line and somewhere where Perl
expects a new statement (so not in the middle of statements like the #
comments). You end the comment with <code class="inline"><span class="pd">=cut</span></code>
, ending the Pod section:</p>
<pre class="verbatim"><ol><li>	=<span class="w">pod</span></li><li></li><li>	<a class="l_k" href="functions/my.html">my</a> <span class="i">$object</span> = <span class="w">NotGonnaHappen</span><span class="w">-&gt;new</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>	<span class="i">ignored_sub</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>	<span class="i">$wont_be_assigned</span> = <span class="n">37</span><span class="sc">;</span></li><li></li><li>	=<span class="w">cut</span></li></ol></pre><p>The quick-and-dirty method only works well when you don't plan to
leave the commented code in the source. If a Pod parser comes along,
you're multiline comment is going to show up in the Pod translation.
A better way hides it from Pod parsers as well.</p>
<p>The <code class="inline"><span class="pd">=begin</span></code>
 directive can mark a section for a particular purpose.
If the Pod parser doesn't want to handle it, it just ignores it. Label
the comments with <code class="inline"><span class="w">comment</span></code>
. End the comment using <code class="inline"><span class="pd">=end</span></code>
 with the
same label. You still need the <code class="inline"><span class="pd">=cut</span></code>
 to go back to Perl code from
the Pod comment:</p>
<pre class="verbatim"><ol><li>	=<span class="w">begin</span> <span class="w">comment</span></li><li></li><li>	<a class="l_k" href="functions/my.html">my</a> <span class="i">$object</span> = <span class="w">NotGonnaHappen</span><span class="w">-&gt;new</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>	<span class="i">ignored_sub</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>	<span class="i">$wont_be_assigned</span> = <span class="n">37</span><span class="sc">;</span></li><li></li><li>	=<span class="w">end</span> <span class="w">comment</span></li><li></li><li>	=<span class="w">cut</span></li></ol></pre><p>For more information on Pod, check out <a href="perlpod.html">perlpod</a> and <a href="perlpodspec.html">perlpodspec</a>.</p>
<a name="How-do-I-clear-a-package%3f"></a><h2>How do I clear a package?</h2>
<p>Use this code, provided by Mark-Jason Dominus:</p>
<pre class="verbatim"><ol><li><a name="scrub_package"></a>	sub <span class="m">scrub_package</span> <span class="s">{</span></li><li>		<a class="l_k" href="functions/no.html">no</a> <span class="w">strict</span> <span class="q">'refs'</span><span class="sc">;</span></li><li>		<a class="l_k" href="functions/my.html">my</a> <span class="i">$pack</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>		<a class="l_k" href="functions/die.html">die</a> <span class="q">&quot;Shouldn't delete main package&quot;</span></li><li>			if <span class="i">$pack</span> eq <span class="q">&quot;&quot;</span> || <span class="i">$pack</span> eq <span class="q">&quot;main&quot;</span><span class="sc">;</span></li><li>		<a class="l_k" href="functions/my.html">my</a> <span class="i">$stash</span> = <span class="i">*</span>{<span class="i">$pack</span> . <span class="q">'::'</span>}{<span class="w">HASH</span>}<span class="sc">;</span></li><li>		<a class="l_k" href="functions/my.html">my</a> <span class="i">$name</span><span class="sc">;</span></li><li>		foreach <span class="i">$name</span> <span class="s">(</span><a class="l_k" href="functions/keys.html">keys</a> <span class="i">%$stash</span><span class="s">)</span> <span class="s">{</span></li><li>			<a class="l_k" href="functions/my.html">my</a> <span class="i">$fullname</span> = <span class="i">$pack</span> . <span class="q">'::'</span> . <span class="i">$name</span><span class="sc">;</span></li><li>			<span class="c"># Get rid of everything with that name.</span></li><li>			<a class="l_k" href="functions/undef.html">undef</a> <span class="i">$$fullname</span><span class="sc">;</span></li><li>			<a class="l_k" href="functions/undef.html">undef</a> <span class="i">@$fullname</span><span class="sc">;</span></li><li>			<a class="l_k" href="functions/undef.html">undef</a> <span class="i">%$fullname</span><span class="sc">;</span></li><li>			<a class="l_k" href="functions/undef.html">undef</a> <span class="i">&amp;$fullname</span><span class="sc">;</span></li><li>			<a class="l_k" href="functions/undef.html">undef</a> <span class="i">*$fullname</span><span class="sc">;</span></li><li>	<span class="s">}</span></li><li>	<span class="s">}</span></li></ol></pre><p>Or, if you're using a recent release of Perl, you can
just use the Symbol::delete_package() function instead.</p>
<a name="How-can-I-use-a-variable-as-a-variable-name%3f"></a><h2>How can I use a variable as a variable name?</h2>
<p>Beginners often think they want to have a variable contain the name
of a variable.</p>
<pre class="verbatim"><ol><li>	<span class="i">$fred</span>    = <span class="n">23</span><span class="sc">;</span></li><li>	<span class="i">$varname</span> = <span class="q">&quot;fred&quot;</span><span class="sc">;</span></li><li>	++<span class="i">$$varname</span><span class="sc">;</span>         <span class="c"># $fred now 24</span></li></ol></pre><p>This works <i>sometimes</i>, but it is a very bad idea for two reasons.</p>
<p>The first reason is that this technique <i>only works on global
variables</i>.  That means that if $fred is a lexical variable created
with my() in the above example, the code wouldn't work at all: you'd
accidentally access the global and skip right over the private lexical
altogether.  Global variables are bad because they can easily collide
accidentally and in general make for non-scalable and confusing code.</p>
<p>Symbolic references are forbidden under the <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">strict</span></code>
 pragma.
They are not true references and consequently are not reference-counted
or garbage-collected.</p>
<p>The other reason why using a variable to hold the name of another
variable is a bad idea is that the question often stems from a lack of
understanding of Perl data structures, particularly hashes.  By using
symbolic references, you are just using the package's symbol-table hash
(like <code class="inline"><span class="i">%main::</span></code>
) instead of a user-defined hash.  The solution is to
use your own hash or a real reference instead.</p>
<pre class="verbatim"><ol><li>	<span class="i">$USER_VARS</span>{<span class="q">&quot;fred&quot;</span>} = <span class="n">23</span><span class="sc">;</span></li><li>	<span class="i">$varname</span> = <span class="q">&quot;fred&quot;</span><span class="sc">;</span></li><li>	<span class="i">$USER_VARS</span>{<span class="i">$varname</span>}++<span class="sc">;</span>  <span class="c"># not $$varname++</span></li></ol></pre><p>There we're using the %USER_VARS hash instead of symbolic references.
Sometimes this comes up in reading strings from the user with variable
references and wanting to expand them to the values of your perl
program's variables.  This is also a bad idea because it conflates the
program-addressable namespace and the user-addressable one.  Instead of
reading a string and expanding it to the actual contents of your program's
own variables:</p>
<pre class="verbatim"><ol><li>	<span class="i">$str</span> = <span class="q">'this has a $fred and $barney in it'</span><span class="sc">;</span></li><li>	<span class="i">$str</span> =~ <span class="q">s/(\$\w+)/$1/eeg</span><span class="sc">;</span>		  <span class="c"># need double eval</span></li></ol></pre><p>it would be better to keep a hash around like %USER_VARS and have
variable references actually refer to entries in that hash:</p>
<pre class="verbatim"><ol><li>	<span class="i">$str</span> =~ <span class="q">s/\$(\w+)/$USER_VARS{$1}/g</span><span class="sc">;</span>   <span class="c"># no /e here at all</span></li></ol></pre><p>That's faster, cleaner, and safer than the previous approach.  Of course,
you don't need to use a dollar sign.  You could use your own scheme to
make it less confusing, like bracketed percent symbols, etc.</p>
<pre class="verbatim"><ol><li>	<span class="i">$str</span> = <span class="q">'this has a %fred% and %barney% in it'</span><span class="sc">;</span></li><li>	<span class="i">$str</span> =~ <span class="q">s/%(\w+)%/$USER_VARS{$1}/g</span><span class="sc">;</span>   <span class="c"># no /e here at all</span></li></ol></pre><p>Another reason that folks sometimes think they want a variable to
contain the name of a variable is that they don't know how to build
proper data structures using hashes.  For example, let's say they
wanted two hashes in their program: %fred and %barney, and that they
wanted to use another scalar variable to refer to those by name.</p>
<pre class="verbatim"><ol><li>	<span class="i">$name</span> = <span class="q">&quot;fred&quot;</span><span class="sc">;</span></li><li>	<span class="i">$$name</span>{<span class="w">WIFE</span>} = <span class="q">&quot;wilma&quot;</span><span class="sc">;</span>     <span class="c"># set %fred</span></li><li></li><li>	<span class="i">$name</span> = <span class="q">&quot;barney&quot;</span><span class="sc">;</span></li><li>	<span class="i">$$name</span>{<span class="w">WIFE</span>} = <span class="q">&quot;betty&quot;</span><span class="sc">;</span>	<span class="c"># set %barney</span></li></ol></pre><p>This is still a symbolic reference, and is still saddled with the
problems enumerated above.  It would be far better to write:</p>
<pre class="verbatim"><ol><li>	<span class="i">$folks</span>{<span class="q">&quot;fred&quot;</span>}{<span class="w">WIFE</span>}   = <span class="q">&quot;wilma&quot;</span><span class="sc">;</span></li><li>	<span class="i">$folks</span>{<span class="q">&quot;barney&quot;</span>}{<span class="w">WIFE</span>} = <span class="q">&quot;betty&quot;</span><span class="sc">;</span></li></ol></pre><p>And just use a multilevel hash to start with.</p>
<p>The only times that you absolutely <i>must</i> use symbolic references are
when you really must refer to the symbol table.  This may be because it's
something that one can't take a real reference to, such as a format name.
Doing so may also be important for method calls, since these always go
through the symbol table for resolution.</p>
<p>In those cases, you would turn off <code class="inline"><span class="w">strict</span> <span class="q">&#39;refs&#39;</span></code>
 temporarily so you
can play around with the symbol table.  For example:</p>
<pre class="verbatim"><ol><li>	<span class="i">@colors</span> = <span class="q">qw(red blue green yellow orange purple violet)</span><span class="sc">;</span></li><li>	for <a class="l_k" href="functions/my.html">my</a> <span class="i">$name</span> <span class="s">(</span><span class="i">@colors</span><span class="s">)</span> <span class="s">{</span></li><li>		<a class="l_k" href="functions/no.html">no</a> <span class="w">strict</span> <span class="q">'refs'</span><span class="sc">;</span>  <span class="c"># renege for the block</span></li><li>		<span class="i">*$name</span> = <a class="l_k" href="functions/sub.html">sub</a> <span class="s">{</span> <span class="q">&quot;&lt;FONT COLOR='$name'&gt;@_&lt;/FONT&gt;&quot;</span> <span class="s">}</span><span class="sc">;</span></li><li>	<span class="s">}</span></li></ol></pre><p>All those functions (red(), blue(), green(), etc.) appear to be separate,
but the real code in the closure actually was compiled only once.</p>
<p>So, sometimes you might want to use symbolic references to manipulate
the symbol table directly.  This doesn't matter for formats, handles, and
subroutines, because they are always global--you can't use my() on them.
For scalars, arrays, and hashes, though--and usually for subroutines--
you probably only want to use hard references.</p>
<a name="What-does-%22bad-interpreter%22-mean%3f"></a><h2>What does "bad interpreter" mean?</h2>
<p>(contributed by brian d foy)</p>
<p>The "bad interpreter" message comes from the shell, not perl.  The
actual message may vary depending on your platform, shell, and locale
settings.</p>
<p>If you see "bad interpreter - no such file or directory", the first
line in your perl script (the "shebang" line) does not contain the
right path to perl (or any other program capable of running scripts).
Sometimes this happens when you move the script from one machine to
another and each machine has a different path to perl--/usr/bin/perl
versus /usr/local/bin/perl for instance. It may also indicate
that the source machine has CRLF line terminators and the
destination machine has LF only: the shell tries to find
/usr/bin/perl&lt;CR&gt;, but can't.</p>
<p>If you see "bad interpreter: Permission denied", you need to make your
script executable.</p>
<p>In either case, you should still be able to run the scripts with perl
explicitly:</p>
<pre class="verbatim"><ol><li>	% perl script.pl</li></ol></pre><p>If you get a message like "perl: command not found", perl is not in
your PATH, which might also mean that the location of perl is not
where you expect it so you need to adjust your shebang line.</p>
<a name="AUTHOR-AND-COPYRIGHT"></a><h1>AUTHOR AND COPYRIGHT</h1>
<p>Copyright (c) 1997-2010 Tom Christiansen, Nathan Torkington, and
other authors as noted. All rights reserved.</p>
<p>This documentation is free; you can redistribute it and/or modify it
under the same terms as Perl itself.</p>
<p>Irrespective of its distribution, all code examples in this file
are hereby placed into the public domain.  You are permitted and
encouraged to use this code in your own programs for fun
or for profit as you see fit.  A simple comment in the code giving
credit would be courteous but is not required.</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="#Can-I-get-a-BNF%2fyacc%2fRE-for-the-Perl-language%3f">Can I get a BNF/yacc/RE for the Perl language?</a><li><a href="#What-are-all-these-%24%40%25%26*-punctuation-signs%2c-and-how-do-I-know-when-to-use-them%3f">What are all these $@%&* punctuation signs, and how do I know when to use them?</a><li><a href="#Do-I-always%2fnever-have-to-quote-my-strings-or-use-semicolons-and-commas%3f">Do I always/never have to quote my strings or use semicolons and commas?</a><li><a href="#How-do-I-skip-some-return-values%3f">How do I skip some return values?</a><li><a href="#How-do-I-temporarily-block-warnings%3f">How do I temporarily block warnings?</a><li><a href="#What's-an-extension%3f">What's an extension?</a><li><a href="#Why-do-Perl-operators-have-different-precedence-than-C-operators%3f">Why do Perl operators have different precedence than C operators?</a><li><a href="#How-do-I-declare%2fcreate-a-structure%3f">How do I declare/create a structure?</a><li><a href="#How-do-I-create-a-module%3f">How do I create a module?</a><li><a href="#How-do-I-adopt-or-take-over-a-module-already-on-CPAN%3f">How do I adopt or take over a module already on CPAN?</a><li><a href="#How-do-I-create-a-class%3f">How do I create a class?
 </a><li><a href="#How-can-I-tell-if-a-variable-is-tainted%3f">How can I tell if a variable is tainted?</a><li><a href="#What's-a-closure%3f">What's a closure?</a><li><a href="#What-is-variable-suicide-and-how-can-I-prevent-it%3f">What is variable suicide and how can I prevent it?</a><li><a href="#How-can-I-pass%2freturn-a-%7bFunction%2c-FileHandle%2c-Array%2c-Hash%2c-Method%2c-Regex%7d%3f">How can I pass/return a {Function, FileHandle, Array, Hash, Method, Regex}?</a><li><a href="#How-do-I-create-a-static-variable%3f">How do I create a static variable?</a><li><a href="#What's-the-difference-between-dynamic-and-lexical-(static)-scoping%3f--Between-local()-and-my()%3f">What's the difference between dynamic and lexical (static) scoping?  Between local() and my()?</a><li><a href="#How-can-I-access-a-dynamic-variable-while-a-similarly-named-lexical-is-in-scope%3f">How can I access a dynamic variable while a similarly named lexical is in scope?</a><li><a href="#What's-the-difference-between-deep-and-shallow-binding%3f">What's the difference between deep and shallow binding?</a><li><a href="#Why-doesn't-%22my(%24foo)-%3d-%3cFILE%3e%3b%22-work-right%3f">Why doesn't "my($foo) = <FILE>;" work right?</a><li><a href="#How-do-I-redefine-a-builtin-function%2c-operator%2c-or-method%3f">How do I redefine a builtin function, operator, or method?</a><li><a href="#What's-the-difference-between-calling-a-function-as-%26foo-and-foo()%3f">What's the difference between calling a function as &foo and foo()?</a><li><a href="#How-do-I-create-a-switch-or-case-statement%3f">How do I create a switch or case statement?</a><li><a href="#How-can-I-catch-accesses-to-undefined-variables%2c-functions%2c-or-methods%3f">How can I catch accesses to undefined variables, functions, or methods?</a><li><a href="#Why-can't-a-method-included-in-this-same-file-be-found%3f">Why can't a method included in this same file be found?</a><li><a href="#How-can-I-find-out-my-current-or-calling-package%3f">How can I find out my current or calling package?</a><li><a href="#How-can-I-comment-out-a-large-block-of-Perl-code%3f">How can I comment out a large block of Perl code?</a><li><a href="#How-do-I-clear-a-package%3f">How do I clear a package?</a><li><a href="#How-can-I-use-a-variable-as-a-variable-name%3f">How can I use a variable as a variable name?</a><li><a href="#What-does-%22bad-interpreter%22-mean%3f">What does "bad interpreter" mean?</a></ul><li><a href="#AUTHOR-AND-COPYRIGHT">AUTHOR AND COPYRIGHT</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="http://perl.jonallen.info">Jon Allen (JJ)</a><br>
	      See the <a href="http://perl.jonallen.info/projects/perldoc">project page</a> for more details
	    </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    = 'perlfaq7';
  perldoc.pageAddress = 'perlfaq7.html';
  perldoc.contentPage = 1;
  explorePerl.render();
  explorePerl.addEvents('explore_anchor');
</script>
    
  </body>
</html>