0

I have a script that reads the contents of a directory. But the script can't open the directory because permission is denied. I am working on Windows, and I've tried to run the script as administrator, but that didn't help.

Here's the code:

sub dir_put {
  my $dir_name = shift;

  open DIR, $dir_name or die "Error reading directory: $!";
  my @array;
  my @return;

  while ($_ = readdir(DIR)){
    next if $_ eq "." or $_ eq "..";
    if (-d $_) {
      @return = dir_put($_);
      unshift(@array, @return);
      next;
    }
    unshift (@array, "$dir_name\\$_");
  }

  @array;
}

How should I fix it?

4

2 回答 2

4

我想你想要opendir,不是open

于 2013-11-09T10:02:34.310 回答
1

你不能用 . 打开目录open,它不是文件。perl 中有打开目录的opendir功能。

尝试:

opendir my $dir, $dir_name or die "Error reading directory: $!";
my @array;
my @return;
while ( readdir $dir ) {
...

此外,您最好使用来自 cpanFile::FindFile::Find::Rule的模块

perl -MFile::Find::Rule -E "say $_ for File::Find::Rule->in('F:\\Films');"
于 2013-11-09T10:05:52.993 回答