Prepend your directory name to the filename so as to give the path to the file, in your case an absolute path so it wouldn't matter where your script was. If you are using a relative path for your directory then that must be the path to the directory from where your script is running or you must do a chdir in your script to the place from where that relative path would succeed.
Here is a trivial example.
$ ls test/dir1/
fileA fileB
$ head -99 test/dir1/file*
==> test/dir1/fileA <==
fileA - line 1
fileA - line 2
==> test/dir1/fileB <==
fileB - line 1
fileB - line 2
fileB - line 3
This script, run from the parent directory of "test"
use strict;
use warnings;
use 5.010;
my $dir = q{test/dir1};
opendir my $dirHandle, $dir
or die qq{opendir: $dir: $!\n};
while ( my $file = readdir $dirHandle )
{
next unless -f qq{$dir/$file};
say qq{*** $file ***};
print do {
open my $fh, q{<}, qq{$dir/$file}
or die qq{open: < $dir/$file: $!\n};
<$fh>;
};
}
Gives this output
*** fileA ***
fileA - line 1
fileA - line 2
*** fileB ***
fileB - line 1
fileB - line 2
fileB - line 3
I hope this is helpful.
|