title: Read contents of text file from offset to end in Perl
body: <p>I'm teaching myself Perl and Regex by reading Jeffrey Friedl's excellent <a href="https://rads.stackoverflow.com/amzn/click/com/0596528124" rel="nofollow noreferrer" rel="nofollow noreferrer">Mastering Regular Expressions</a>.</p>
  
  <p>While trying to solve the "A Small Mail Utility" exercise starting on page 53 I stumbled upon the problem of not knowing how to save the content of file into a variable <em>starting from an offset</em>.</p>
  
  <p>So here's my (shortened) script.</p>
  
  <pre><code>my ($body, $line, $subject); 
  $body = $line = $subject = "";
  
  open(MYFILE, "king.in") || die("Could not open file!");    
  # Read the file's content line by line
  while ($line = &lt;MYFILE&gt;)
  {   
      # An empty line marks the beginning of the body
      if ($line =~ m/^\s+$/ ) {
          # HERE IS THE ISSUE
          # Save the file content starting from the current line
          # to the end of the file into $body
          last; 
      }
  
      if ($line =~ m/^subject: (.*)/i) {
          $subject = $1;
      }
      # Parse additional data from the mail header
  }
  close(MYFILE);
  
  print "Subject: Re: $subject\n";
  print "\n" ;
  print $body;
  </code></pre>
  
  <p>I did some online research but couldn't figure out how to put the remainder of the file (i.e., the email body) into the variable $body.</p>
  
  <p>I figured out that I could get the current position within the file in bytes using <code>$pos = tell(MYFILE);</code></p>
  
  <p>Eventually I ended up with the working but unsatisfactory <a href="http://pastebin.com/wbVSmQ7B" rel="nofollow noreferrer">solution</a> of putting the file's lines first into an array.</p>
  
  <p>How do I save the file content starting from an offset (either as a line number or bytes) into $body?</p>
  
  <p><strong>Edit:</strong>
  My solution -as provided by vstm- is to use <code>$body = join("", &lt;MYFILE&gt;)</code> to read in the rest of the file when encountering the empty line that marks the beginning of the body.
  The whole script I wrote can be found <a href="http://pastebin.com/jA1mXZ6p" rel="nofollow noreferrer">here</a>.</p>
  
  <p>Although this works great for me now, I would still like to know how to say (elegantly) in Perl "give me lines x to z of this file".</p>
  
  <p>Thanks for your advice everybody.</p>