title: Finding files with Perl
body: <h2><a href="http://perldoc.perl.org/File/Find.html" rel="noreferrer"><code>File::Find</code></a> <strong>and the</strong> <code>wanted</code> <strong>subroutine</strong></h2>
  
  <p>This question is much simpler than the original title ("prototypes and forward declaration of subroutines"!) lets on. I'm hoping the answer, however simple, will help me understand subroutines/functions, prototypes and scoping and the <code>File::Find</code> module.</p>
  
  <p>With Perl, subroutines can appear pretty much anywhere and you normally  don't need to make forward declarations (except if the sub declares a prototype, which I'm not sure how to do in a "standard" way in Perl). For what I usually do with Perl there's little difference between these different ways of running <code>somefunction</code>:</p>
  
  <pre><code>sub somefunction;  # Forward declares the function
  &amp;somefunction; 
  somefunction();
  somefunction;   # Bare word warning under `strict subs`
  </code></pre>
  
  <p>I often use <a href="https://metacpan.org/pod/distribution/perl/x2p/find2perl.PL" rel="noreferrer"><code>find2perl</code></a> to generate code which I crib/hack into parts of scripts. This could well be bad style and now my dirty laundry is public, but so be it :-)  For <code>File::Find</code> the <code>wanted</code> function is a required subroutine - <code>find2perl</code> creates it and adds <code>sub wanted;</code> to the resulting script it creates. Sometimes, when I edit the script I'll remove the "<code>sub</code>" from <code>sub wanted</code> and it ends up as <code>&amp;wanted;</code> or <code>wanted();</code>. But without the <code>sub wanted;</code> forward declaration form I get this warning:</p>
  
  <pre><code>Use of uninitialized value $_ in lstat at findscript.pl line 29
  </code></pre>
  
  <p>My question is: why does this happen and is it a real problem? It is "just a warning", but I want to understand it better.</p>
  
  <ul>
  <li>The documentation and code say <code>$_</code> is localized inside of <code>sub wanted {}</code>. Why would it be undefined if I use <code>wanted();</code> instead of <code>sub wanted;</code>? </li>
  <li>Is <code>wanted</code> using prototypes somewhere? Am I missing something obvious in <code>Find/File.pm</code>?</li>
  <li>Is it because <code>wanted</code> returns a code reference? (???)</li>
  </ul>
  
  <p>My guess is that the forward declaration form "initializes" <code>wanted</code> in some way so that the first use doesn't have an empty default variable. I guess this would be how prototypes - even Perl prototypes, such as they exist - would work as well. I tried grepping through the Perl source code to get a sense of what <code>sub</code> is doing when a function is called using <code>sub function</code> instead of <code>function()</code>, but that may be beyond me at this point.</p>
  
  <p>Any help deepening (and speeding up) my understanding of this is much appreciated.</p>
  
  <p><strong>EDIT:</strong> Here's a <a href="https://stackoverflow.com/a/17734132/2019415">recent example script here on Stack&nbsp;Overflow</a> that I created using <code>find2perl</code>'s output. If you remove the <code>sub</code>  from <code>sub wanted;</code> you should get the same error.</p>
  
  <p><strong>EDIT:</strong> As I noted in a comment below (but I'll flag it here too): for several months I've been using <a href="https://metacpan.org/pod/Path::Iterator::Rule" rel="noreferrer"><code>Path::Iterator::Rule</code></a> instead of <code>File::Find</code>. It requires <code>perl &gt;5.10</code>, but I never have to deploy production code at sites with odd, "never upgrade", <code>5.8.*</code> only policies so <code>Path::Iterator::Rule</code> has become one of those modules I never want to do with out.  Also useful is <a href="https://metacpan.org/pod/Path::Class" rel="noreferrer"><code>Path::Class</code></a>. Cheers.</p>