title: Perl file upload. How to access filehandle multiple times?
body: <p>I am trying to detect if uploaded file is a valid UTF-8 and only after that do some operations with its content. It detects non-UTF-8 files ok, but if file is a valid UTF-8 there is no content to process. There is no data to process in a <code>while(){}</code> loop. Where is my mistake? </p>
  
  <pre><code>use utf8;
  use CGI qw(:all -utf8);
  use Encode;
  
  my $q           = new CGI;
  
  my $file        = $q-&gt;param('importfile');
  my $file_handle = $q-&gt;upload('importfile');
  my $fhtest      = do {
          local $/;
          &lt;$file_handle&gt;;
  };
  
  my $utf8;
  eval { $utf8 = decode( "utf8", $fhtest, Encode::FB_CROAK ) };
  if ($@) {
          die 'Not a valid UTF-8 file';
  }
  
  binmode $file_handle, ':encoding(UTF-8)';
  while (&lt;$file_handle&gt;) {
          chomp();
          # my code here
  }
  </code></pre>