Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Can';t打开目录,或者什么';我的(Perl)代码有什么问题?_Perl_Opendir - Fatal编程技术网

Can';t打开目录,或者什么';我的(Perl)代码有什么问题?

Can';t打开目录,或者什么';我的(Perl)代码有什么问题?,perl,opendir,Perl,Opendir,我有一个文件名logspath.txt,它包含在机器上的日志目录路径中。 每行一个目录。尽管目录存在,Perl说它不存在 #!/usr/bin/perl -w open FILE,"logspath.txt" or die $!; while (<FILE>){ print $_; opendir ($DIR,chomp($_)) or die $!; 有什么建议吗?chomp没有任何有意义的返回值,您可以传递给opendir。您需要在opendir上方的单独语句中选

我有一个文件名
logspath.txt
,它包含在机器上的日志目录路径中。 每行一个目录。尽管目录存在,Perl说它不存在

#!/usr/bin/perl -w

open FILE,"logspath.txt" or die $!;
while (<FILE>){
   print $_;
   opendir ($DIR,chomp($_)) or die $!;

有什么建议吗?

chomp
没有任何有意义的返回值,您可以传递给
opendir
。您需要在
opendir
上方的单独语句中选择字符串

 chomp;
 opendir DIR, $_ or die ...

这应该是一个评论,但在评论中发布代码并没有真正的工作,所以我把它CW

以下是如何为better的某些价值编写“better”:

#!/usr/bin/perl

use strict; use warnings;

my $logs_file = 'logspath.txt';

open my $FILE, '<', $logs_file
    or die "Cannot open '$logs_file': $!";

while ( my $dir = <$FILE> ) {
    print $dir and chomp $dir;
    opendir my $dir_h, $dir
        or die "Cannot open directory '$dir': $!";
    # do something with $dir_h
}
#/usr/bin/perl
严格使用;使用警告;
我的$logs_文件='logspath.txt';
打开我的$FILE,“使用strict;
使用警告

    open my $FILE, '<logspath.txt'
     or die "Cannot open '$logs_file': $!";

    while ( my $dir = <$FILE> ) {
        print $dir and chomp $dir;
            if( -d $dir)
            {
        opendir my $dir_h, $dir
            or die "Cannot open directory '$dir': $!";
        # do something with $dir_h
    }
     else
{
     print " Can't open directory $!\n";
}
}

打开我的$FILE,'From:
“它返回从其所有参数中删除的字符总数。”
或者您可以
使用autodie并获得有用的错误消息,而不必到处写“或死”。我认为以这种方式使用-d不是很有用。您所做的只是使代码更加复杂,没有任何好处。只需尝试opendir,如果失败,则处理该失败。
#!/usr/bin/perl

use strict; use warnings;

my $logs_file = 'logspath.txt';

open my $FILE, '<', $logs_file
    or die "Cannot open '$logs_file': $!";

while ( my $dir = <$FILE> ) {
    print $dir and chomp $dir;
    opendir my $dir_h, $dir
        or die "Cannot open directory '$dir': $!";
    # do something with $dir_h
}
    open my $FILE, '<logspath.txt'
     or die "Cannot open '$logs_file': $!";

    while ( my $dir = <$FILE> ) {
        print $dir and chomp $dir;
            if( -d $dir)
            {
        opendir my $dir_h, $dir
            or die "Cannot open directory '$dir': $!";
        # do something with $dir_h
    }
     else
{
     print " Can't open directory $!\n";
}
}