sachinmkulkarni has asked for the wisdom of the Perl Monks concerning the following question:

I have some code like this.. //Code to grep filenames for some patterns and get matching //files
foreach my $file(@files) { open (FH, $file); my @fileContents = <FH>; close(FH); //Do something with @fileContents }
I get the following error message.. readLine() on closed filehandle..

Replies are listed 'Best First'.
Re: File read errors
by jwkrahn (Abbot) on Jan 14, 2010 at 04:24 UTC

    You should only attempt to use a filehandle if open returned a true value.    Try something like this instead:

    foreach my $file ( @files ) { open FH, '<', $file or do { warn "Cannot open '$file' $!"; next; }; my @fileContents = <FH>; close FH; //Do something with @fileContents }
      A reply falls below the community's threshold of quality. You may see it by logging in.