title: Perl - Best practices when sending blocks to subs
body: <p>I'm a big fan of functional programming, so when I discovered block references in Perl I started using them a lot. </p>
  
  <p>However, the functions I've written that take blocks as arguments are written in this style: </p>
  
  <pre><code>sub mygrep (&amp;@) {
      my $code = shift;
      my @result;
      foreach $_ (@_) {
          push(@result, $_) if &amp;$code;
      }
      @result;
  }
  </code></pre>
  
  <p>(From <a href="http://perldoc.perl.org/perlsub.html#Prototypes" rel="nofollow">http://perldoc.perl.org/perlsub.html#Prototypes</a>)</p>
  
  <p>In essence, most of my functions set <code>$_</code> in order for the code block to gain access to data in my sub. I guess my question can be split into three subquestions:</p>
  
  <ol>
  <li>Are there some major pitfalls in this approach?  </li>
  <li>Is it a better idea to <code>local</code>ize <code>$_</code> before setting it? </li>
  <li>Should i use partially applied functions instead?</li>
  </ol>
  
  <p>I'm still a Perl newbie so any answers and suggestions are appreciated - thanks in advance! :)</p>