perlfaq7.html 108 KB
Newer Older
1 2 3
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
4 5
<head>
  <title>perlfaq7 - perldoc.perl.org</title>
6 7
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <meta http-equiv="Content-Language" content="en-gb">
8 9 10
  <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">
11 12
</head>

13 14 15 16 17 18 19
<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>
20 21 22 23 24 25 26 27 28
	<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>
29
      </div>
30 31 32 33
      
      <div id="body">
        <div id="left_column">
          <div class="side_group">
34 35
            
	    <div class="side_panel doc_panel">
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
              <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>
54
                <li><a href="index-internals.html">Internals</a>
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
                <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>
277 278 279 280 281 282 283 284
            
	      <div class="side_panel doc_panel">
		<p>Tools</p>
		<ul>
		  <li><a href="preferences.html">Preferences</a>
		</ul>
	      </div>
            
285 286 287 288 289 290 291 292 293
          </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">
294
                Perl 5 version 26.0 documentation
295
              </div>
296
              <div class="page_links" id="page_links_top">
297 298
                <a href="#" onClick="toolbar.goToTop();return false;">Go to top</a>
		
299 300
              </div>
	      <div class="page_links" id="page_links_bottom">
301
		
302
                  <a href="#" id="page_index_toggle">Show page index</a> &bull;
303
		
304 305 306 307 308 309 310
                <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>
311 312 313 314 315 316 317 318 319 320 321
            </div>
            <div id="breadcrumbs">
                
    <a href="index.html">Home</a> &gt;
    
      
        <a href="index-faq.html">FAQs</a> &gt;
      
    
    perlfaq7
  
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
            </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>
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378

	    <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>
  
379 380 381 382 383
	    <div id="from_search"></div>
            <h1>perlfaq7</h1>


  <!--    -->
384
<ul><li><a href="#NAME">NAME</a><li><a href="#VERSION">VERSION</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?
385
 </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-%3c%24fh%3e%3b%22-work-right%3f">Why doesn't "my($foo) = <$fh>;" 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><li><a href="#Do-I-need-to-recompile-XS-modules-when-there-is-a-change-in-the-C-library%3f">Do I need to recompile XS modules when there is a change in the C library?</a></ul><li><a href="#AUTHOR-AND-COPYRIGHT">AUTHOR AND COPYRIGHT</a></ul><a name="NAME"></a><h1>NAME</h1>
386
<p>perlfaq7 - General Perl Language Issues</p>
387
<a name="VERSION"></a><h1>VERSION</h1>
388
<p>version 5.021011</p>
389
<a name="DESCRIPTION"></a><h1>DESCRIPTION</h1>
390
<p>This section deals with general Perl language issues that don't
391 392 393
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
394
perly.y in the source distribution if you're particularly brave. The
395 396
grammar relies on very smart tokenizing code, so be prepared to
venture into toke.c as well.</p>
397
<p>In the words of Chaim Frenkel: "Perl's grammar can not be reduced to BNF.
398
The work of parsing perl is distributed between yacc, the lexer, smoke
399
and mirrors."</p>
400 401
<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>
402
<pre class="verbatim"><ol><li>    <span class="i">$ for</span> <a class="l_k" href="functions/scalar.html">scalar</a> <a class="l_k" href="functions/values.html">values</a> <span class="s">(</span><span class="w">number</span><span class="cm">,</span> <span class="w">string</span> <a class="l_k" href="functions/or.html">or</a> <span class="w">reference</span><span class="s">)</span></li><li>    <span class="i">@ for</span> <span class="w">arrays</span></li><li>    % <a class="l_k" href="functions/for.html">for</a> <span class="w">hashes</span> <span class="s">(</span><span class="w">associative</span> <span class="w">arrays</span><span class="s">)</span></li><li>    &amp; <a class="l_k" href="functions/for.html">for</a> <span class="w">subroutines</span> <span class="s">(</span><span class="w">aka</span> <span class="w">functions</span><span class="cm">,</span> <span class="w">procedures</span><span class="cm">,</span> <span class="w">methods</span><span class="s">)</span></li><li>    * <a class="l_k" href="functions/for.html">for</a> <span class="w">all</span> <span class="w">types</span> <span class="w">of</span> <span class="w">that</span> <span class="w">symbol</span> <span class="w">name</span>. <span class="w">In</span> <span class="w">version</span> <span class="n">4</span> <span class="w">you</span> <span class="w">used</span> <span class="w">them</span> <span class="w">like</span></li><li>      <span class="w">pointers</span><span class="cm">,</span> <span class="w">but</span> <span class="w">in</span> <span class="w">modern</span> <span class="w">perls</span> <span class="w">you</span> <span class="w">can</span> <span class="w">just</span> <a class="l_k" href="functions/use.html">use</a> <span class="w">references</span>.</li></ol></pre><p>There are a couple of other symbols that
403
you're likely to encounter that aren't
404
really type specifiers:</p>
405 406
<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>
407
 operator applied
408
to the handle FILE. It reads one line (well, record--see
409
<a href="perlvar.html#%24%2f">$/ in perlvar</a>) from the handle FILE in scalar context, or <i>all</i> lines
410
in list context. When performing open, close, or any other operation
411 412
besides <code class="inline">&lt;&gt;</code>
 on files, or even when talking about the handle, do
413
<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>
414
<span class="n">2</span><span class="s">)</span></code>
415
 and "copying from STDIN to FILE".</p>
416
<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>
417
<p>Normally, a bareword doesn't need to be quoted, but in most cases
418
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>
419
). But a hash key
420 421
consisting of a simple word and the left-hand
operand to the <code class="inline"><span class="cm">=&gt;</span></code>
422 423
 operator both
count as though they were quoted:</p>
424 425
<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
426
one-liners:</p>
427
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/if.html">if</a> <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>    <a class="l_k" href="functions/my.html">my</a> <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>    <a class="l_k" href="functions/if.html">if</a> <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>    <a class="l_k" href="functions/my.html">my</a> <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>
428
<p>One way is to treat the return values as a list and index into it:</p>
429 430
<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
431
you need:</p>
432
<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>
433
<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>
434
 pragma
435
allows fine control of what warnings are produced.
436
See <a href="perllexwarn.html">perllexwarn</a> for more details.</p>
437
<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">$x</span> = <span class="i">$y</span> + <span class="i">$z</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.
438
You turn off the categories you want to ignore and you can still
439
get other categories of warnings. See <a href="perllexwarn.html">perllexwarn</a> for the
440
complete details, including the category names and hierarchy.</p>
441
<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">$x</span> = <span class="i">$y</span> + <span class="i">$z</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>
442 443
 variable (documented
in <a href="perlvar.html">perlvar</a>) controls runtime warnings for a block:</p>
444
<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">$x</span> = <span class="i">$y</span> + <span class="i">$z</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
445 446
use my() on <code class="inline"><span class="i">$^W</span></code>
, only local().</p>
447
<a name="What's-an-extension%3f"></a><h2>What's an extension?</h2>
448
<p>An extension is a way of calling compiled C code from Perl. Reading
449 450
<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>
451 452
<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
453
doesn't have, especially functions that give a list context to everything
454
on their right, eg. print, chmod, exec, and so on. Such functions are
455
called "list operators" and appear as such in the precedence table in
456 457
<a href="perlop.html">perlop</a>.</p>
<p>A common mistake is to write:</p>
458 459
<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
460 461
super low precedence <code class="inline">or</code>
 operator:</p>
462
<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>
463 464 465 466 467 468
, <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>
469
<p>Another operator with surprising precedence is exponentiation. It
470
binds more tightly even than unary minus, making <code class="inline"><span class="n">-2</span>**<span class="n">2</span></code>
471
 produce a
472
negative four and not a positive one. It is also right-associating, meaning
473 474
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>
475
<p>Although it has the same precedence as in C, Perl's <code class="inline">?:</code> operator
476
produces an lvalue. This assigns $x to either $if_true or $if_false, depending
477
on the trueness of $maybe:</p>
478 479 480
<pre class="verbatim"><ol><li>    <span class="s">(</span><span class="i">$maybe</span> ? <span class="i">$if_true</span> <span class="co">:</span> <span class="i">$if_false</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.
481
Here's an example:</p>
482
<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="perlootut.html">perlootut</a>.</p>
483
<a name="How-do-I-create-a-module%3f"></a><h2>How do I create a module?</h2>
484 485 486 487 488 489 490 491
<p><a href="perlnewmod.html">perlnewmod</a> is a good place to start, ignore the bits
about uploading to CPAN if you don't want to make your
module publicly available.</p>
<p><a href="http://search.cpan.org/perldoc/ExtUtils::ModuleMaker">ExtUtils::ModuleMaker</a> and <a href="http://search.cpan.org/perldoc/Module::Starter">Module::Starter</a> are also
good places to start. Many CPAN authors now use <a href="http://search.cpan.org/perldoc/Dist::Zilla">Dist::Zilla</a>
to automate as much as possible.</p>
<p>Detailed documentation about modules can be found at:
<a href="perlmod.html">perlmod</a>, <a href="perlmodlib.html">perlmodlib</a>, <a href="perlmodstyle.html">perlmodstyle</a>.</p>
492 493
<p>If you need to include C code or C library interfaces
use h2xs. h2xs will create the module distribution structure
494 495
and the initial interface files.
<a href="perlxs.html">perlxs</a> and <a href="perlxstut.html">perlxstut</a> explain the details.</p>
496
<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>
497 498 499 500
<p>Ask the current maintainer to make you a co-maintainer or
transfer the module to you.</p>
<p>If you can not reach the author for some reason contact
the PAUSE admins at modules@perl.org who may be able to help,
501
but each case is treated separately.</p>
502 503
<ul>
<li>
504
<p>Get a login for the Perl Authors Upload Server (PAUSE) if you don't
505 506 507 508 509 510 511 512 513 514 515 516
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>
517 518
<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
519 520 521 522
private communication or the public post, a PAUSE admin can transfer
it to you.</p>
</li>
</ul>
523 524 525 526
<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.
527 528
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
529
you).</p>
530
<p>See also <a href="perlootut.html">perlootut</a>, a tutorial that covers class creation, and <a href="perlobj.html">perlobj</a>.</p>
531 532 533
<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).
534 535
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>
536 537
<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
538 539 540
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
541
variables that were around when the subroutine was defined (deep
542 543 544 545 546
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
547 548
example. For more information on closures, check out any textbook on
functional programming. Scheme is a language that not only supports
549
but encourages closures.</p>
550
<p>Here's a classic non-closure function-generating function:</p>
551
<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>    <a class="l_k" href="functions/my.html">my</a> <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>    <a class="l_k" href="functions/my.html">my</a> <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
552
technically a closure because it refers to no lexicals outside its own
553
scope. Using a closure gives you a <i>function template</i> with some
554
customization slots left out to be filled later.</p>
555 556
<p>Contrast this with the following make_adder() function, in which the
returned anonymous function contains a reference to a lexical variable
557
outside the scope of that function itself. Such a reference requires
558 559
that Perl return a proper closure, thus locking in for all time the
value that the lexical had when the function was created.</p>
560
<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>    <a class="l_k" href="functions/my.html">my</a> <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>    <a class="l_k" href="functions/my.html">my</a> <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">$f1</span>-&gt;<span class="s">(</span><span class="i">$n</span><span class="s">)</span></code>
561
 is always 20 plus whatever $n you pass in, whereas
562 563
<code class="inline"><span class="i">$f2</span>-&gt;<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
564
in the closure sticks around.</p>
565
<p>Closures are often used for less esoteric purposes. For example, when
566
you want to pass in a bit of code into a function:</p>
567
<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,
568 569 570
<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
571
$line back in its caller's scope.</p>
572 573 574 575
<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
576
sure a variable doesn't get meddled with during the lifetime of the
577
package:</p>
578
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/BEGIN.html">BEGIN</a> <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
579
<i>Persistent Private Variables</i>.</p>
580 581 582 583
<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
584
of a variable. It is caused by scoping through my() and local()
585
interacting with either closures or aliased foreach() iterator variables
586 587 588
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>        <a class="l_k" href="functions/while.html">while</a> <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>
589
 in the subroutine
590
doesn't pick up a fresh copy of the <code class="inline"><span class="i">$f</span></code>
591 592 593 594 595 596
 whose value is <code class="inline"><span class="q">&#39;foo&#39;</span></code>
. 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>
597 598 599 600

<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>
601 602
<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
603
information on references.</p>
604 605 606 607
<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>
608
<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>
609 610 611
<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>
612
<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> <a class="l_k" href="functions/or.html">or</a> <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>
613 614
 or <code class="inline">\<span class="i">*FH</span></code>
 notations.
615 616
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>
617 618
</li>
<li><a name="Passing-Regexes"></a><b>Passing Regexes</b>
619 620 621
<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>
622
<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>
623 624
<li><a name="Passing-Methods"></a><b>Passing Methods</b>
<p>To pass an object method into a subroutine, you can do this:</p>
625
<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
626
method call, and arguments:</p>
627
<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
628 629 630 631 632
(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>
633 634 635
<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>
636
<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">$count</span>++ <span class="s">}</span></li></ol></pre><p>You can fake a static variable by using a lexical variable which goes
637
out of scope. In this example, you define the subroutine <code class="inline"><span class="w">counter</span></code>
638 639 640 641 642 643 644 645 646 647
, 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
648
<code class="inline"><span class="w">counter</span></code>
649 650 651 652
 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
653
<code class="inline"><span class="w">counter</span></code>
654
.</p>
655
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/BEGIN.html">BEGIN</a> <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="counter"></a>        sub <span class="m">counter</span> <span class="s">{</span> <span class="i">$count</span>++ <span class="s">}</span></li><li>    <span class="s">}</span></li><li></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$start</span> = <span class="i">counter</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>    .... <span class="c"># code that calls counter();</span></li><li></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$end</span> = <span class="i">counter</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>In the previous example, you created a function-private variable
656 657
because only one function remembered its reference. You could define
multiple functions while the variable is in scope, and each function
658
can share the "private" variable. It's not really "static" because you
659 660
can access it outside the function while the lexical variable is in
scope, and even create references to it. In this example,
661 662
<code class="inline"><span class="w">increment_count</span></code>
 and <code class="inline"><span class="w">return_count</span></code>
663 664 665 666 667
 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>
668
<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.
669 670
A file is also a scope, so a lexical variable defined in the file
cannot be seen from any other file.</p>
671
<p>See <a href="perlsub.html#Persistent-Private-Variables">Persistent Private Variables in perlsub</a> for more information.
672 673
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
674
<a href="perlsub.html#Persistent-Private-Variables">Persistent Private Variables in perlsub</a> for details.</p>
675
<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>
676 677 678
<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
679 680
visible in other functions called from that subroutine</i>. This is done
at run-time, so is called dynamic scoping. local() always affects global
681 682
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
683 684
subroutine. This is done at compile-time, so it is called lexical or
static scoping. my() always affects private variables, also called
685 686
lexical variables or (improperly) static(ly scoped) variables.</p>
<p>For instance:</p>
687
<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
688
because $var only has that value within the block of the lexical()
689 690
function, and it is hidden from the called subroutine.</p>
<p>In summary, local() doesn't make what you think of as private, local
691
variables. It gives a global variable a temporary value. my() is
692 693 694
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>
695 696 697
<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
698
in the current package, but rather the one in the "main" package, as
699
though you had written $main::var.</p>
700
<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
701
dynamic variable into the current lexical scope.</p>
702
<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>
703 704 705
<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
706
happen to be in scope when the subroutine is called. Perl always uses
707 708
deep binding of lexical variables (i.e., those created with my()).
However, dynamic variables (aka global, local, or package variables)
709 710 711
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-%3c%24fh%3e%3b%22-work-right%3f"></a><h2>Why doesn't "my($foo) = &lt;$fh&gt;;" work right?</h2>
712 713
<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>
714
. The &lt;$fh&gt; read operation, like so many of Perl's
715
functions and operators, can tell which context it was called in and
716
behaves appropriately. In general, the scalar() function can help.
717 718
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.
719 720
If that function doesn't have a defined scalar behavior, this of course
doesn't help you (such as with sort()).</p>
721 722
<p>To enforce scalar context in this particular case, however, you need
merely omit the parentheses:</p>
723
<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;$fh&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;$fh&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;$fh&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
724
issue is the same here:</p>
725
<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;$fh&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;$fh&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>
726 727
<p>Why do you want to do that? :-)</p>
<p>If you want to override a predefined function, such as open(),
728
then you'll have to import the new definition from a different
729
module. See <a href="perlsub.html#Overriding-Built-in-Functions">Overriding Built-in Functions in perlsub</a>.</p>
730 731 732
<p>If you want to overload a Perl operator, such as <code class="inline">+</code>
 or <code class="inline"><span class="i">**</span></code>
,
733
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>
734 735
 pragma, documented
in <a href="overload.html">overload</a>.</p>
736
<p>If you're talking about obscuring method calls in parent classes,
737
see <a href="perlootut.html#Overriding-methods-and-method-resolution">Overriding methods and method resolution in perlootut</a>.</p>
738
<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>
739 740 741 742
<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>
743
 and passes it the current value of the argument
744
list, <code class="inline"><span class="i">@_</span></code>
745
. Here's an example; the <code class="inline"><span class="w">bar</span></code>
746 747
 subroutine calls <code class="inline"><span class="i">&amp;foo</span></code>
,
748
which prints its arguments list:</p>
749
<pre class="verbatim"><ol><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="sc">;</span> <span class="s">}</span></li><li></li><li><a name="bar"></a>    sub <span class="m">bar</span> <span class="s">{</span> <span class="i">&amp;foo</span><span class="sc">;</span> <span class="s">}</span></li><li></li><li>    <span class="i">bar</span><span class="s">(</span> <span class="q">&quot;a&quot;</span><span class="cm">,</span> <span class="q">&quot;b&quot;</span><span class="cm">,</span> <span class="q">&quot;c&quot;</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>
750 751 752
 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>
753
<pre class="verbatim"><ol><li>    <span class="w">Args</span> <span class="w">in</span> <span class="w">foo</span> <span class="w">are</span><span class="co">:</span> <span class="w">a</span> <span class="w">b</span> <span class="w">c</span></li></ol></pre><p>Calling the subroutine with trailing parentheses, with or without arguments,
754
does not use the current <code class="inline"><span class="i">@_</span></code>
755 756
. Changing the example to put parentheses after
the call to <code class="inline"><span class="w">foo</span></code>
757
 changes the program:</p>
758
<pre class="verbatim"><ol><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="sc">;</span> <span class="s">}</span></li><li></li><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="sc">;</span> <span class="s">}</span></li><li></li><li>    <span class="i">bar</span><span class="s">(</span> <span class="q">&quot;a&quot;</span><span class="cm">,</span> <span class="q">&quot;b&quot;</span><span class="cm">,</span> <span class="q">&quot;c&quot;</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>
759
 doesn't get the <code class="inline"><span class="i">@_</span></code>
760
 from its caller.</p>
761 762 763 764 765
<pre class="verbatim"><ol><li>    <span class="w">Args</span> <span class="w">in</span> <span class="w">foo</span> <span class="w">are</span><span class="co">:</span></li></ol></pre><p>However, using <code class="inline"><span class="i">&amp;</span></code>
 in the call still overrides the prototype of <code class="inline"><span class="w">foo</span></code>
 if
present:</p>
<pre class="verbatim"><ol><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 infoo are: @_\n&quot;</span><span class="sc">;</span> <span class="s">}</span></li><li></li><li><a name="bar_1"></a>    sub <span class="m">bar_1</span> <span class="s">{</span> <span class="i">&amp;foo</span><span class="sc">;</span> <span class="s">}</span></li><li><a name="bar_2"></a>    sub <span class="m">bar_2</span> <span class="s">{</span> <span class="i">&amp;foo</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span> <span class="s">}</span></li><li><a name="bar_3"></a>    sub <span class="m">bar_3</span> <span class="s">{</span> <span class="i">foo</span><span class="s">(</span> <span class="i">$_</span>[<span class="n">0</span>]<span class="cm">,</span> <span class="i">$_</span>[<span class="n">1</span>]<span class="cm">,</span> <span class="i">$_</span>[<span class="n">2</span>] <span class="s">)</span><span class="sc">;</span> <span class="s">}</span></li><li>    <span class="c"># sub bar_4 { foo(); }</span></li><li>    <span class="c"># bar_4 doesn&#39;t compile: &quot;Not enough arguments for main::foo at ...&quot;</span></li><li></li><li>    <span class="i">bar_1</span><span class="s">(</span> <span class="q">&quot;a&quot;</span><span class="cm">,</span> <span class="q">&quot;b&quot;</span><span class="cm">,</span> <span class="q">&quot;c&quot;</span> <span class="s">)</span><span class="sc">;</span></li><li>    <span class="c"># Args in foo are: a b c</span></li><li></li><li>    <span class="i">bar_2</span><span class="s">(</span> <span class="q">&quot;a&quot;</span><span class="cm">,</span> <span class="q">&quot;b&quot;</span><span class="cm">,</span> <span class="q">&quot;c&quot;</span> <span class="s">)</span><span class="sc">;</span></li><li>    <span class="c"># Args in foo are:</span></li><li></li><li>    <span class="i">bar_3</span><span class="s">(</span> <span class="q">&quot;a&quot;</span><span class="cm">,</span> <span class="q">&quot;b&quot;</span><span class="cm">,</span> <span class="q">&quot;c&quot;</span> <span class="s">)</span><span class="sc">;</span></li><li>    <span class="c"># Args in foo are: a b c</span></li></ol></pre><p>The main use of the <code class="inline"><span class="i">@_</span></code>
766 767 768
 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>
769
<a name="How-do-I-create-a-switch-or-case-statement%3f"></a><h2>How do I create a switch or case statement?</h2>
770 771
<p>In Perl 5.10, use the <code class="inline">given-when</code>
 construct described in <a href="perlsyn.html">perlsyn</a>:</p>
772
<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>    <a class="l_k" href="functions/given.html">given</a> <span class="s">(</span> <span class="i">$string</span> <span class="s">)</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/when.html">when</a><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>        <a class="l_k" href="functions/when.html">when</a><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>        <a class="l_k" href="functions/when.html">when</a><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
773
prior to 5.10, the general answer is to use <code class="inline">if-elsif-else</code>:</p>
774
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/for.html">for</a> <span class="s">(</span><span class="i">$variable_to_test</span><span class="s">)</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/if.html">if</a>    <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>        <a class="l_k" href="functions/elsif.html">elsif</a> <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>        <a class="l_k" href="functions/elsif.html">elsif</a> <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>        <a class="l_k" href="functions/else.html">else</a>            <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,
775
lined up in a way to make it look more like a switch statement.
776
We'll do a multiway conditional based on the type of reference stored
777
in $whatchamacallit:</p>
778
<pre class="verbatim"><ol><li>    <span class="j">SWITCH:</span> <a class="l_k" href="functions/for.html">for</a> <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>
779
<p>Sometimes you should change the positions of the constant and the variable.
780
For example, let's say you wanted to test which of many answers you were
781 782 783 784 785 786 787
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>
788 789
<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>
790
, can also be
791
used to get switch and case. Its use is now discouraged, because it's
792
not fully compatible with the native switch of Perl 5.10, and because,
793
as it's implemented as a source filter, it doesn't always work as intended
794
when complex syntax is involved.</p>
795
<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>
796 797
<p>The AUTOLOAD method, discussed in <a href="perlsub.html#Autoloading">Autoloading in perlsub</a> lets you capture
calls to undefined functions and methods.</p>
798
<p>When it comes to undefined variables that would trigger a warning
799
under <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">warnings</span></code>
800
, you can promote the warning to an error.</p>
801
<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>
802
<p>Some possible reasons: your inheritance is getting confused, you've
803
misspelled the method name, or the object is of the wrong type. Check
804
out <a href="perlootut.html">perlootut</a> for details about any of the above cases. You may
805 806 807 808
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>
809
<p>Another possible reason for problems is that you've used the
810
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>
811
) on a class name
812
before Perl has seen that such a package exists. It's wisest to make
813 814
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
815
<code class="inline"><a class="l_k" href="functions/require.html">require</a></code>. If not, make sure to use arrow notation (eg.,
816
<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>
817
) instead. Object notation is explained in
818 819
<a href="perlobj.html">perlobj</a>.</p>
<p>Make sure to read about creating modules in <a href="perlmod.html">perlmod</a> and
820
the perils of indirect objects in <a href="perlobj.html#Method-Invocation">Method Invocation in perlobj</a>.</p>
821 822 823 824 825
<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
826
special literals as separate tokens, so you can't interpolate them
827
into strings like you can with variables:</p>
828 829 830
<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 <a href="Carp.html">Carp</a> 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><a name="foo"></a>    sub <span class="m">foo</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/my.html">my</a> <span class="i">@args</span> = ...<span class="sc">;</span></li><li>        <a class="l_k" href="functions/my.html">my</a><span class="s">(</span> <span class="i">$package</span><span class="cm">,</span> <span class="i">$filename</span><span class="cm">,</span> <span class="i">$line</span> <span class="s">)</span> = <a class="l_k" href="functions/caller.html">caller</a><span class="sc">;</span></li><li></li><li>        <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;I was called from package $package\n&quot;</span><span class="sc">;</span></li><li>        <span class="p">)</span><span class="sc">;</span></li></ol></pre><p>By default, your program starts in package <code class="inline"><span class="w">main</span></code>
831 832 833
, so you will
always be in some package.</p>
<p>This is different from finding out the package an object is blessed
834 835
into, which might not be the current package. For that, use <code class="inline"><span class="w">blessed</span></code>

836 837
from <a href="Scalar/Util.html">Scalar::Util</a>, 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
838
into, however, as long as it claims to inherit from that class:</p>
839
<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
840 841 842 843
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>
844
<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>
845 846
 with <code class="inline"><span class="w">DOES</span></code>
 (although the converse is not true).</p>
847 848 849 850 851
<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
852 853
expects a new statement (so not in the middle of statements like the <code class="inline"><span class="c">#</span></code>

854 855
comments). You end the comment with <code class="inline"><span class="pd">=cut</span></code>
, ending the Pod section:</p>
856
<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
857
leave the commented code in the source. If a Pod parser comes along,
858
your multiline comment is going to show up in the Pod translation.
859 860 861
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.
862
If the Pod parser doesn't want to handle it, it just ignores it. Label
863 864 865 866 867 868
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>
869
<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>
870 871
<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>
872
<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">&#39;refs&#39;</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&#39;t delete main package&quot;</span></li><li>            <a class="l_k" href="functions/if.html">if</a> <span class="i">$pack</span> <a class="l_k" href="functions/eq.html">eq</a> <span class="q">&quot;&quot;</span> || <span class="i">$pack</span> <a class="l_k" href="functions/eq.html">eq</a> <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">&#39;::&#39;</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>        <a class="l_k" href="functions/foreach.html">foreach</a> <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">&#39;::&#39;</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
873 874 875 876
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>
877
<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>
878
<p>The first reason is that this technique <i>only works on global
879
variables</i>. That means that if $fred is a lexical variable created
880
with my() in the above example, the code wouldn't work at all: you'd
881
accidentally access the global and skip right over the private lexical
882
altogether. Global variables are bad because they can easily collide
883
accidentally and in general make for non-scalable and confusing code.</p>
884
<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>
885
 pragma.
886 887
They are not true references and consequently are not reference-counted
or garbage-collected.</p>
888 889
<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
890
understanding of Perl data structures, particularly hashes. By using
891
symbolic references, you are just using the package's symbol-table hash
892
(like <code class="inline"><span class="i">%main::</span></code>
893
) instead of a user-defined hash. The solution is to
894
use your own hash or a real reference instead.</p>
895
<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>    <a class="l_k" href="functions/my.html">my</a> <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.
896 897
Sometimes this comes up in reading strings from the user with variable
references and wanting to expand them to the values of your perl
898 899
program's variables. This is also a bad idea because it conflates the
program-addressable namespace and the user-addressable one. Instead of
900
reading a string and expanding it to the actual contents of your program's
901
own variables:</p>
902
<pre class="verbatim"><ol><li>    <span class="i">$str</span> = <span class="q">&#39;this has a $fred and $barney in it&#39;</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
903
variable references actually refer to entries in that hash:</p>
904 905
<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
906
make it less confusing, like bracketed percent symbols, etc.</p>
907
<pre class="verbatim"><ol><li>    <span class="i">$str</span> = <span class="q">&#39;this has a %fred% and %barney% in it&#39;</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
908
contain the name of a variable is that they don't know how to build
909
proper data structures using hashes. For example, let's say they
910 911
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>
912 913 914
<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>
915
<p>The only times that you absolutely <i>must</i> use symbolic references are
916
when you really must refer to the symbol table. This may be because it's
917
something that one can't take a real reference to, such as a format name.
918 919
Doing so may also be important for method calls, since these always go
through the symbol table for resolution.</p>
920
<p>In those cases, you would turn off <code class="inline"><span class="w">strict</span> <span class="q">&#39;refs&#39;</span></code>
921
 temporarily so you
922 923
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">&#39;refs&#39;</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=&#39;$name&#39;&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,
924
but the real code in the closure actually was compiled only once.</p>
925
<p>So, sometimes you might want to use symbolic references to manipulate
926
the symbol table directly. This doesn't matter for formats, handles, and
927
subroutines, because they are always global--you can't use my() on them.
928 929
For scalars, arrays, and hashes, though--and usually for subroutines--
you probably only want to use hard references.</p>
930
<a name="What-does-%22bad-interpreter%22-mean%3f"></a><h2>What does "bad interpreter" mean?</h2>
931
<p>(contributed by brian d foy)</p>
932
<p>The "bad interpreter" message comes from the shell, not perl. The
933 934
actual message may vary depending on your platform, shell, and locale
settings.</p>
935 936
<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
937 938
right path to perl (or any other program capable of running scripts).
Sometimes this happens when you move the script from one machine to
939
another and each machine has a different path to perl--/usr/bin/perl
940 941 942
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
943 944
/usr/bin/perl&lt;CR&gt;, but can't.</p>
<p>If you see "bad interpreter: Permission denied", you need to make your
945 946 947
script executable.</p>
<p>In either case, you should still be able to run the scripts with perl
explicitly:</p>
948
<pre class="verbatim"><ol><li>    <span class="i">% perl</span> <span class="w">script</span>.<span class="w">pl</span></li></ol></pre><p>If you get a message like "perl: command not found", perl is not in
949 950
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>
951 952 953 954 955
<a name="Do-I-need-to-recompile-XS-modules-when-there-is-a-change-in-the-C-library%3f"></a><h2>Do I need to recompile XS modules when there is a change in the C library?</h2>
<p>(contributed by Alex Beamish)</p>
<p>If the new version of the C library is ABI-compatible (that's Application
Binary Interface compatible) with the version you're upgrading from, and if the
shared library version didn't change, no re-compilation should be necessary.</p>
956
<a name="AUTHOR-AND-COPYRIGHT"></a><h1>AUTHOR AND COPYRIGHT</h1>
957
<p>Copyright (c) 1997-2013 Tom Christiansen, Nathan Torkington, and
958 959 960 961
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
962
are hereby placed into the public domain. You are permitted and
963
encouraged to use this code in your own programs for fun
964
or for profit as you see fit. A simple comment in the code giving
965
credit would be courteous but is not required.</p>
966 967 968 969




970 971 972 973 974
  <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>
975
    </div>
976
    <div id="page_index_content" class="hud_content">
977
      <ul><li><a href="#NAME">NAME</a><li><a href="#VERSION">VERSION</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?
978
 </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-%3c%24fh%3e%3b%22-work-right%3f">Why doesn't "my($foo) = <$fh>;" 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><li><a href="#Do-I-need-to-recompile-XS-modules-when-there-is-a-change-in-the-C-library%3f">Do I need to recompile XS modules when there is a change in the C library?</a></ul><li><a href="#AUTHOR-AND-COPYRIGHT">AUTHOR AND COPYRIGHT</a></ul>
979
    </div>
980 981 982 983
    <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>
984 985 986
    </div>
  </div>

987 988 989 990 991 992 993

	    &nbsp;
          </div>
          <div id="content_footer">
          </div>
        </div>
        <div class="clear"></div>
994
      </div>
995
      
996 997 998 999 1000 1001 1002 1003 1004
    <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">
1005
	      Site maintained by <a href="mailto:jj@jonallen.info">Jon Allen (JJ)</a><br>
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
	    </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"