title: efficiently reading N lines at a time from an input file in perl
body: <p>The structure of my input data file is such that it is more logical to read the data in blocks of <code>N</code> lines rather than a single line at a time. Of course, I could use something straightforward like</p>
  
  <pre><code>my @lines=();
  while(!eof($FH)) {
    for(my $i=0;$i&lt;$N;$i++)
     $lines[$i]=&lt;FH&gt;;
     chomp();
    }
    # proceed with analysis of N-size block ##
  }
  </code></pre>
  
  <p>Because the input files are very large (GB), however, I wonder whether there is a more efficient solution than a <code>for</code> loop. For instance, I found another solution <a href="http://www.perlmonks.org/?node_id=1007560" rel="nofollow">online</a> that uses the <code>map</code> function, although when I try to implement it in my script, it results in an error (<code>"my" variable @lines masks earlier declaration in same statement</code>):</p>
  
  <pre><code>while(( my @lines = map $_ = &lt;&gt;, 1 .. 4 )[0]) {
    print @lines;
    print "\n";
  }
  </code></pre>
  
  <p>Admittedly, I don't understand the significance of the <code>[0]</code> in the while block of this code, and another solution suggested using <code>[-1]</code> instead.</p>
  
  <p>Given the I/O intensiveness of the operation, I wonder about what would be the most computationally efficient solution to this problem (within the bounds of the Perl programming language).</p>