【问题标题】:readdir() attempted on invalid dirhandle DIRreaddir() 尝试在无效的 dirhandle DIR 上
【发布时间】:2015-07-05 06:05:54
【问题描述】:

我想打开目录并获取该目录中的所有文件名,而不是子目录。请帮我纠正这段代码

#!/usr/bin/perl

use strict;
use warnings;
use Getopt::Long;
my $opt_section;
my $opt_content;
my $opt_help = 0;

 &GetOptions (
"help"          => \$opt_help,
"section:s"        => \$opt_section,
"content:s"        => \$opt_content,
);

if ($opt_help) {
print "USAGE: file.pl -section <section> -content <content> \n ";

    exit;}

my $dir ="/home/priya/scripts/test/${opt_section}/${opt_content}/latest";                             #base directory to look up for the files

print"$dir \n";

opendir(DIR, $dir) or die "Unable to read Directory : $!";
while (my $file = readdir(DIR)) {
        #Only files not subdirectories
        next unless (-f "$dir/$file");
        #Use a regular expression to find files ending in .txt
        $file =~ m/^${opt_section}.+txt$/i;
print "$file \n";

closedir(DIR);
}
exit 0;

我在执行时遇到这个错误

它正在读取第一个文件并将其命名为输出,然后给出此错误

readdir() 尝试在无效的目录句柄 DIR 上

【问题讨论】:

    标签: perl regex-negation


    【解决方案1】:

    如果您正确缩进了代码,错误就会很明显。 closedir 在您的循环中!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-02-26
      • 2013-10-30
      • 1970-01-01
      • 2020-04-18
      • 2015-11-01
      • 2021-05-03
      • 1970-01-01
      相关资源
      最近更新 更多