I have been messing with my code for an hour now and still no progress. Please hear me out:
This code worked early today, what it does is open the directory I give to the program, read it, and open each file in the directory and read them individually to perform some actions .
This is the way I do the file opening and reading:
use strict;
use warnings;
use File::Slurp;
my $dir = shift @ARGV;
opendir(DH, $dir) or die "Directory Open Error!";
my @filelist = grep !/^\.\.?$/, readdir(DH);
my $filename;
foreach $filename(@filelist){
open(FH, $filename) or die "File Open Error!";
my $readFile = read_file($filename);
#more code
}
So this works earlier and I have seen the code perform what I want it to do just fine, but when I ran the code on an other directory it returned
File Open Error! at program.pl line 15
After that I can’t make it work anymore, no matter how much I checked I still couldn’t figure out what went wrong.
Since the error is on the open() line, I first made sure correct file name was passed down, so I printed the list of file names after the directory was open, everything looks good.
Then I got rid of the open directory part, pass the file name manually:
$filename = shift @ARGV;
open(FH, $filename) or die "File Open Error!";
my $readFile = read_file($filename);
The program works, file opens just fine. So I got rid of the error message and return to the first code and this is what I saw:
read_file ‘somefile’ – sysopen: no such file or directory at
program.pl line 17
Line 17 is the line where I used read_file to read the content of a file, this happens after the file is open, I don’t understand what’s going on now. Again, this code was working earlier just fine and I really didn’t change anything except input and now it completely stop working
I think you need to filter-out directories and add $dir to filenames: