title: Unable to open text file in Perl
body: <p>I am fetching some log files (which are in txt format) from another server and trying to parse them using my Perl script. The logs are being fetched correctly after which I set permissions to 777 for the log directory.</p>
  
  <p>After this I attempt to open the log files, one by one for parsing, via my Perl script. Now, the strange thing and the <em>problem</em> which happens is, my script is sometimes able to open the file and sometimes NOT. To put it simply, it's unable to open the log files for parsing at times.</p>
  
  <p>Also, I have cronned this perl script and the chances of file open failing are greater when it runs via cron rather than manually, although they have run successfully in both cases previously. I don't understand where the issue lies.</p>
  
  <p>Here is the code which I use for opening the files,</p>
  
  <pre><code>$inputDir = "/path/to/dir";
  @inputFiles = &lt;$inputDir/*&gt;;
  
  # inputFiles array is list of files in the log directory
  foreach my $logFile(@inputFiles)
  {
      # just to ensure file name is text
      $logFile = $logFile."";
  
      # process file only if filename contains "NOK"
      if(index($logFile,"NOK") &gt; -1)
      {
          # opens the file
          open($ifile, '&lt;', $logFile) or die "Error: Unable to open file for processing.";
  
          # file parsing takes place
      }
  }
  
  close($ifile);
  </code></pre>
  
  <p>I want to re-iterate that this code HAS run successfully and I haven't changed any part of it. Yet, it does not run every time without fail, because its unable to open the log file at times. Any ideas?</p>