yonyon100 yonyon100 - 10 days ago 5
Perl Question

open error - No such file or directory

I am using

File::Find
to run through a directory tree and when I try to open the current file for reading I get
No such file or directory
. This happens with ALL files in the directory tree.

Here's the sub I use in the
find()
:

sub {
if (-d) {
return;
}
if (-f) {
my $file = ${File::Find::name};
open (my $IN, '<', '$file') or die "$!\n";
while (<$IN>) {
### Do some formatting.
}
close $IN;
}
}


It fails in the line:

open (my $IN, '<', '$file') or die "$!\n";


I thought it's a matter of links maybe, but even with
follow => 1
option I get this error.

By the way, without
follow
the error I get is on the first file of the first directory I find and with it, the error is on the last file of the last directory (but in both cases, it's on the first file inspected by
File::Find
).

Answer

Problem solved. Apparently, replacing the single quotes with double quotes in the open line, or even better, not using any quotes, did the trick. The string literal '$file' produces the string $file, and there's clearly no file with this name.

Comments