title: How can I reset a file handle receiving standard input in Perl?
body: <p>When I issue a Perl script some standard input, e.g.</p>
  
  <pre><code>$ awk '{print $1"\t"$2}' foo.txt | myScript.pl
  </code></pre>
  
  <p>I have a script which contains a bug. It reads the first line of standard input, gets something from that first line, and then only parses lines 2 through n of standard input on subsequent read:</p>
  
  <pre><code>open (FH, "&lt; $input") or die $?;
  my $firstLine = &lt;FH&gt;; // reads first line
  
  ...
  
  while (my $line = &lt;FH&gt;) {
      // reads lines 2 through n
  }
  close (FH);
  </code></pre>
  
  <p>So I added a <code>seek</code> statement to this script to try to reset the file handle to the beginning of the file:</p>
  
  <pre><code>use Fcntl qw(:seek);
  
  ...
  
  open (FH, "&lt; $input") or die $?;
  my $firstLine = &lt;FH&gt;; // reads first line
  seek (FH, 0, SEEK_SET) or die "error: could not reset file handle\n"; // should reset file handle
  
  ...
  
  while (my $line = &lt;FH&gt;) {
      // reads lines 1 through n (in theory)
  }
  close (FH);
  </code></pre>
  
  <p>Adding this <code>seek</code> statement works for file input, but not for standard input. The script crashes with the error:</p>
  
  <pre><code> error: could not reset file handle 
  </code></pre>
  
  <p>How can I correctly read the first line of standard input at the start of the script, reset the file handle and then read all lines 1 through n of the standard input?</p>
  
  <p>I guess I can write a special case for storing and processing the first line before the <code>while</code> loop, but I'm hoping there is a cleaner solution to this problem that allows me to handle both standard input and file input.</p>