Reputation: 23
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
Upvotes: 1
Views: 970
Reputation: 97938
I think you need to filter-out directories and add $dir to filenames:
use strict;
use warnings;
use File::Slurp;
my $dir = shift @ARGV;
opendir(DH, $dir) or die "Directory Open Error!";
# here, use grep { !-d "$dir/$_"}
my @filelist = grep {!-d "$dir/$_"} readdir(DH);
my $filename;
foreach $filename(@filelist){
$filename = "$dir/$filename"; # also add dir
print $filename, "\n";
my $readFile = read_file($filename);
}
Upvotes: 3
Reputation: 6204
Here's an option using File::Find::Rule to get the file names from a directory. Omit ->maxdepth(1)
if you want directory recursion. In this case, ->name(qr/.+\.txt$/i)
filters file names for only text files; omit this option if you don't want the file names filtered. squiguy is correct that read_file
handles open
ing files.
use strict;
use warnings;
use File::Slurp qw/read_file/;
use File::Find::Rule;
my $dir = shift @ARGV or die 'No directory sent.';
-d $dir or die "Directory doesn't exist.";
my @filelist = File::Find::Rule->file()
->maxdepth(1)
->name(qr/.+\.txt$/i)
->in($dir);
for my $fileName (@filelist) {
my $fileContents = read_file $fileName;
#more code
}
The full file path is provided to read_file
.
Hope this helps!
Upvotes: 1